1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626
|
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import unittest
"""
Run this file to run the tests.
``python tests.py``
Or install nose and run nosetests.
``pip install nose``
then:
``nosetests``
Post a ticket and/or clone and fix it. Pull requests with tests gladly accepted.
https://github.com/derek73/python-nameparser/issues
https://github.com/derek73/python-nameparser/pulls
"""
import logging
import re
try:
import dill
except ImportError:
dill = False
from nameparser import HumanName
from nameparser.util import u
from nameparser.config import Constants, TupleManager
log = logging.getLogger('HumanName')
try:
unittest.expectedFailure
except AttributeError:
# Python 2.6 backport
import unittest2 as unittest
class HumanNameTestBase(unittest.TestCase):
def m(self, actual, expected, hn):
"""assertEqual with a better message and awareness of hn.C.empty_attribute_default"""
expected = expected or hn.C.empty_attribute_default
try:
self.assertEqual(actual, expected, "'%s' != '%s' for '%s'\n%r" % (
actual,
expected,
hn.original,
hn
))
except UnicodeDecodeError:
self.assertEqual(actual, expected)
class HumanNamePythonTests(HumanNameTestBase):
def test_utf8(self):
hn = HumanName("de la Véña, Jüan")
self.m(hn.first, "Jüan", hn)
self.m(hn.last, "de la Véña", hn)
def test_string_output(self):
hn = HumanName("de la Véña, Jüan")
def test_escaped_utf8_bytes(self):
hn = HumanName(b'B\xc3\xb6ck, Gerald')
self.m(hn.first, "Gerald", hn)
self.m(hn.last, "Böck", hn)
def test_len(self):
hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC")
self.m(len(hn), 5, hn)
hn = HumanName("John Doe")
self.m(len(hn), 2, hn)
@unittest.skipUnless(dill, "requires python-dill module to test pickling")
def test_config_pickle(self):
constants = Constants()
self.assertTrue(dill.pickles(constants))
@unittest.skipUnless(dill, "requires python-dill module to test pickling")
def test_name_instance_pickle(self):
hn = HumanName("Title First Middle Middle Last, Jr.")
self.assertTrue(dill.pickles(hn))
def test_comparison(self):
hn1 = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC")
hn2 = HumanName("Dr. John P. Doe-Ray, CLU, CFP, LUTC")
self.assertTrue(hn1 == hn2)
self.assertTrue(hn1 is not hn2)
self.assertTrue(hn1 == "Dr. John P. Doe-Ray CLU, CFP, LUTC")
hn1 = HumanName("Doe, Dr. John P., CLU, CFP, LUTC")
hn2 = HumanName("Dr. John P. Doe-Ray, CLU, CFP, LUTC")
self.assertTrue(not hn1 == hn2)
self.assertTrue(not hn1 == 0)
self.assertTrue(not hn1 == "test")
self.assertTrue(not hn1 == ["test"])
self.assertTrue(not hn1 == {"test": hn2})
def test_assignment_to_full_name(self):
hn = HumanName("John A. Kenneth Doe, Jr.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.suffix, "Jr.", hn)
hn.full_name = "Juan Velasquez y Garcia III"
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test_get_full_name_attribute_references_internal_lists(self):
hn = HumanName("John Williams")
hn.first_list = ["Larry"]
self.m(hn.full_name, "Larry Williams", hn)
def test_assignment_to_attribute(self):
hn = HumanName("John A. Kenneth Doe, Jr.")
hn.last = "de la Vega"
self.m(hn.last, "de la Vega", hn)
hn.title = "test"
self.m(hn.title, "test", hn)
hn.first = "test"
self.m(hn.first, "test", hn)
hn.middle = "test"
self.m(hn.middle, "test", hn)
hn.suffix = "test"
self.m(hn.suffix, "test", hn)
with self.assertRaises(TypeError):
hn.suffix = [['test']]
with self.assertRaises(TypeError):
hn.suffix = {"test": "test"}
def test_assign_list_to_attribute(self):
hn = HumanName("John A. Kenneth Doe, Jr.")
hn.title = ["test1", "test2"]
self.m(hn.title, "test1 test2", hn)
hn.first = ["test3", "test4"]
self.m(hn.first, "test3 test4", hn)
hn.middle = ["test5", "test6", "test7"]
self.m(hn.middle, "test5 test6 test7", hn)
hn.last = ["test8", "test9", "test10"]
self.m(hn.last, "test8 test9 test10", hn)
hn.suffix = ['test']
self.m(hn.suffix, "test", hn)
def test_comparison_case_insensitive(self):
hn1 = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC")
hn2 = HumanName("dr. john p. doe-Ray, CLU, CFP, LUTC")
self.assertTrue(hn1 == hn2)
self.assertTrue(hn1 is not hn2)
self.assertTrue(hn1 == "Dr. John P. Doe-ray clu, CFP, LUTC")
def test_slice(self):
hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC")
self.m(list(hn), ['Dr.', 'John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC'], hn)
self.m(hn[1:], ['John', 'P.', 'Doe-Ray', 'CLU, CFP, LUTC', hn.C.empty_attribute_default], hn)
self.m(hn[1:-2], ['John', 'P.', 'Doe-Ray'], hn)
def test_getitem(self):
hn = HumanName("Dr. John A. Kenneth Doe, Jr.")
self.m(hn['title'], "Dr.", hn)
self.m(hn['first'], "John", hn)
self.m(hn['last'], "Doe", hn)
self.m(hn['middle'], "A. Kenneth", hn)
self.m(hn['suffix'], "Jr.", hn)
def test_setitem(self):
hn = HumanName("Dr. John A. Kenneth Doe, Jr.")
hn['title'] = 'test'
self.m(hn['title'], "test", hn)
hn['last'] = ['test', 'test2']
self.m(hn['last'], "test test2", hn)
with self.assertRaises(TypeError):
hn["suffix"] = [['test']]
with self.assertRaises(TypeError):
hn["suffix"] = {"test": "test"}
def test_conjunction_names(self):
hn = HumanName("johnny y")
self.m(hn.first, "johnny", hn)
self.m(hn.last, "y", hn)
def test_prefix_names(self):
hn = HumanName("vai la")
self.m(hn.first, "vai", hn)
self.m(hn.last, "la", hn)
def test_blank_name(self):
hn = HumanName()
self.m(hn.first, "", hn)
self.m(hn.last, "", hn)
def test_surnames_list_attribute(self):
hn = HumanName("John Edgar Casey Williams III")
self.m(hn.surnames_list, ["Edgar", "Casey", "Williams"], hn)
def test_surnames_attribute(self):
hn = HumanName("John Edgar Casey Williams III")
self.m(hn.surnames, "Edgar Casey Williams", hn)
def test_is_prefix_with_list(self):
hn = HumanName()
items = ['firstname', 'lastname', 'del']
self.assertTrue(hn.is_prefix(items))
self.assertTrue(hn.is_prefix(items[1:]))
def test_is_conjunction_with_list(self):
hn = HumanName()
items = ['firstname', 'lastname', 'and']
self.assertTrue(hn.is_conjunction(items))
self.assertTrue(hn.is_conjunction(items[1:]))
def test_override_constants(self):
C = Constants()
hn = HumanName(constants=C)
self.assertTrue(hn.C is C)
def test_override_regex(self):
var = TupleManager([("spaces", re.compile(r"\s+", re.U)),])
C = Constants(regexes=var)
hn = HumanName(constants=C)
self.assertTrue(hn.C.regexes == var)
def test_override_titles(self):
var = ["abc","def"]
C = Constants(titles=var)
hn = HumanName(constants=C)
self.assertTrue(sorted(hn.C.titles) == sorted(var))
def test_override_first_name_titles(self):
var = ["abc","def"]
C = Constants(first_name_titles=var)
hn = HumanName(constants=C)
self.assertTrue(sorted(hn.C.first_name_titles) == sorted(var))
def test_override_prefixes(self):
var = ["abc","def"]
C = Constants(prefixes=var)
hn = HumanName(constants=C)
self.assertTrue(sorted(hn.C.prefixes) == sorted(var))
def test_override_suffix_acronyms(self):
var = ["abc","def"]
C = Constants(suffix_acronyms=var)
hn = HumanName(constants=C)
self.assertTrue(sorted(hn.C.suffix_acronyms) == sorted(var))
def test_override_suffix_not_acronyms(self):
var = ["abc","def"]
C = Constants(suffix_not_acronyms=var)
hn = HumanName(constants=C)
self.assertTrue(sorted(hn.C.suffix_not_acronyms) == sorted(var))
def test_override_conjunctions(self):
var = ["abc","def"]
C = Constants(conjunctions=var)
hn = HumanName(constants=C)
self.assertTrue(sorted(hn.C.conjunctions) == sorted(var))
def test_override_capitalization_exceptions(self):
var = TupleManager([("spaces", re.compile(r"\s+", re.U)),])
C = Constants(capitalization_exceptions=var)
hn = HumanName(constants=C)
self.assertTrue(hn.C.capitalization_exceptions == var)
class FirstNameHandlingTests(HumanNameTestBase):
def test_first_name(self):
hn = HumanName("Andrew")
self.m(hn.first, "Andrew", hn)
def test_assume_title_and_one_other_name_is_last_name(self):
hn = HumanName("Rev Andrews")
self.m(hn.title, "Rev", hn)
self.m(hn.last, "Andrews", hn)
# TODO: Seems "Andrews, M.D.", Andrews should be treated as a last name
# but other suffixes like "George Jr." should be first names. Might be
# related to https://github.com/derek73/python-nameparser/issues/2
@unittest.expectedFailure
def test_assume_suffix_title_and_one_other_name_is_last_name(self):
hn = HumanName("Andrews, M.D.")
self.m(hn.suffix, "M.D.", hn)
self.m(hn.last, "Andrews", hn)
def test_suffix_in_lastname_part_of_lastname_comma_format(self):
hn = HumanName("Smith Jr., John")
self.m(hn.last, "Smith", hn)
self.m(hn.first, "John", hn)
self.m(hn.suffix, "Jr.", hn)
def test_sir_exception_to_first_name_rule(self):
hn = HumanName("Sir Gerald")
self.m(hn.title, "Sir", hn)
self.m(hn.first, "Gerald", hn)
def test_king_exception_to_first_name_rule(self):
hn = HumanName("King Henry")
self.m(hn.title, "King", hn)
self.m(hn.first, "Henry", hn)
def test_queen_exception_to_first_name_rule(self):
hn = HumanName("Queen Elizabeth")
self.m(hn.title, "Queen", hn)
self.m(hn.first, "Elizabeth", hn)
def test_dame_exception_to_first_name_rule(self):
hn = HumanName("Dame Mary")
self.m(hn.title, "Dame", hn)
self.m(hn.first, "Mary", hn)
def test_first_name_is_not_prefix_if_only_two_parts(self):
"""When there are only two parts, don't join prefixes or conjunctions"""
hn = HumanName("Van Nguyen")
self.m(hn.first, "Van", hn)
self.m(hn.last, "Nguyen", hn)
def test_first_name_is_not_prefix_if_only_two_parts_comma(self):
hn = HumanName("Nguyen, Van")
self.m(hn.first, "Van", hn)
self.m(hn.last, "Nguyen", hn)
@unittest.expectedFailure
def test_first_name_is_prefix_if_three_parts(self):
"""Not sure how to fix this without breaking Mr and Mrs"""
hn = HumanName("Mr. Van Nguyen")
self.m(hn.first, "Van", hn)
self.m(hn.last, "Nguyen", hn)
class HumanNameBruteForceTests(HumanNameTestBase):
def test1(self):
hn = HumanName("John Doe")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
def test2(self):
hn = HumanName("John Doe, Jr.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Jr.", hn)
def test3(self):
hn = HumanName("John Doe III")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "III", hn)
def test4(self):
hn = HumanName("Doe, John")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
def test5(self):
hn = HumanName("Doe, John, Jr.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Jr.", hn)
def test6(self):
hn = HumanName("Doe, John III")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "III", hn)
def test7(self):
hn = HumanName("John A. Doe")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
def test8(self):
hn = HumanName("John A. Doe, Jr")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
self.m(hn.suffix, "Jr", hn)
def test9(self):
hn = HumanName("John A. Doe III")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
self.m(hn.suffix, "III", hn)
def test10(self):
hn = HumanName("Doe, John A.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
def test11(self):
hn = HumanName("Doe, John A., Jr.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
self.m(hn.suffix, "Jr.", hn)
def test12(self):
hn = HumanName("Doe, John A., III")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
self.m(hn.suffix, "III", hn)
def test13(self):
hn = HumanName("John A. Kenneth Doe")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A. Kenneth", hn)
def test14(self):
hn = HumanName("John A. Kenneth Doe, Jr.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.suffix, "Jr.", hn)
def test15(self):
hn = HumanName("John A. Kenneth Doe III")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.suffix, "III", hn)
def test16(self):
hn = HumanName("Doe, John. A. Kenneth")
self.m(hn.first, "John.", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A. Kenneth", hn)
def test17(self):
hn = HumanName("Doe, John. A. Kenneth, Jr.")
self.m(hn.first, "John.", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.suffix, "Jr.", hn)
def test18(self):
hn = HumanName("Doe, John. A. Kenneth III")
self.m(hn.first, "John.", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.suffix, "III", hn)
def test19(self):
hn = HumanName("Dr. John Doe")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.title, "Dr.", hn)
def test20(self):
hn = HumanName("Dr. John Doe, Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Jr.", hn)
def test21(self):
hn = HumanName("Dr. John Doe III")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "III", hn)
def test22(self):
hn = HumanName("Doe, Dr. John")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
def test23(self):
hn = HumanName("Doe, Dr. John, Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Jr.", hn)
def test24(self):
hn = HumanName("Doe, Dr. John III")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "III", hn)
def test25(self):
hn = HumanName("Dr. John A. Doe")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
def test26(self):
hn = HumanName("Dr. John A. Doe, Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
self.m(hn.suffix, "Jr.", hn)
def test27(self):
hn = HumanName("Dr. John A. Doe III")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
self.m(hn.suffix, "III", hn)
def test28(self):
hn = HumanName("Doe, Dr. John A.")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
def test29(self):
hn = HumanName("Doe, Dr. John A. Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
self.m(hn.suffix, "Jr.", hn)
def test30(self):
hn = HumanName("Doe, Dr. John A. III")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "A.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "III", hn)
def test31(self):
hn = HumanName("Dr. John A. Kenneth Doe")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
def test32(self):
hn = HumanName("Dr. John A. Kenneth Doe, Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Jr.", hn)
def test33(self):
hn = HumanName("Al Arnold Gore, Jr.")
self.m(hn.middle, "Arnold", hn)
self.m(hn.first, "Al", hn)
self.m(hn.last, "Gore", hn)
self.m(hn.suffix, "Jr.", hn)
def test34(self):
hn = HumanName("Dr. John A. Kenneth Doe III")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "III", hn)
def test35(self):
hn = HumanName("Doe, Dr. John A. Kenneth")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
def test36(self):
hn = HumanName("Doe, Dr. John A. Kenneth Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Jr.", hn)
def test37(self):
hn = HumanName("Doe, Dr. John A. Kenneth III")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "III", hn)
def test38(self):
hn = HumanName("Juan de la Vega")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
def test39(self):
hn = HumanName("Juan de la Vega, Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.suffix, "Jr.", hn)
def test40(self):
hn = HumanName("Juan de la Vega III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.suffix, "III", hn)
def test41(self):
hn = HumanName("de la Vega, Juan")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
def test42(self):
hn = HumanName("de la Vega, Juan, Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.suffix, "Jr.", hn)
def test43(self):
hn = HumanName("de la Vega, Juan III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.suffix, "III", hn)
def test44(self):
hn = HumanName("Juan Velasquez y Garcia")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test45(self):
hn = HumanName("Juan Velasquez y Garcia, Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test46(self):
hn = HumanName("Juan Velasquez y Garcia III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test47(self):
hn = HumanName("Velasquez y Garcia, Juan")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test48(self):
hn = HumanName("Velasquez y Garcia, Juan, Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test49(self):
hn = HumanName("Velasquez y Garcia, Juan III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test50(self):
hn = HumanName("Dr. Juan de la Vega")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
def test51(self):
hn = HumanName("Dr. Juan de la Vega, Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.suffix, "Jr.", hn)
def test52(self):
hn = HumanName("Dr. Juan de la Vega III")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.suffix, "III", hn)
def test53(self):
hn = HumanName("de la Vega, Dr. Juan")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
def test54(self):
hn = HumanName("de la Vega, Dr. Juan, Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.suffix, "Jr.", hn)
def test55(self):
hn = HumanName("de la Vega, Dr. Juan III")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.suffix, "III", hn)
def test56(self):
hn = HumanName("Dr. Juan Velasquez y Garcia")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test57(self):
hn = HumanName("Dr. Juan Velasquez y Garcia, Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test58(self):
hn = HumanName("Dr. Juan Velasquez y Garcia III")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test59(self):
hn = HumanName("Velasquez y Garcia, Dr. Juan")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test60(self):
hn = HumanName("Velasquez y Garcia, Dr. Juan, Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test61(self):
hn = HumanName("Velasquez y Garcia, Dr. Juan III")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test62(self):
hn = HumanName("Juan Q. de la Vega")
self.m(hn.first, "Juan", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.last, "de la Vega", hn)
def test63(self):
hn = HumanName("Juan Q. de la Vega, Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.suffix, "Jr.", hn)
def test64(self):
hn = HumanName("Juan Q. de la Vega III")
self.m(hn.first, "Juan", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.suffix, "III", hn)
def test65(self):
hn = HumanName("de la Vega, Juan Q.")
self.m(hn.first, "Juan", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.last, "de la Vega", hn)
def test66(self):
hn = HumanName("de la Vega, Juan Q., Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.suffix, "Jr.", hn)
def test67(self):
hn = HumanName("de la Vega, Juan Q. III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.suffix, "III", hn)
def test68(self):
hn = HumanName("Juan Q. Velasquez y Garcia")
self.m(hn.middle, "Q.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test69(self):
hn = HumanName("Juan Q. Velasquez y Garcia, Jr.")
self.m(hn.middle, "Q.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test70(self):
hn = HumanName("Juan Q. Velasquez y Garcia III")
self.m(hn.middle, "Q.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test71(self):
hn = HumanName("Velasquez y Garcia, Juan Q.")
self.m(hn.middle, "Q.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test72(self):
hn = HumanName("Velasquez y Garcia, Juan Q., Jr.")
self.m(hn.middle, "Q.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test73(self):
hn = HumanName("Velasquez y Garcia, Juan Q. III")
self.m(hn.middle, "Q.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test74(self):
hn = HumanName("Dr. Juan Q. de la Vega")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.last, "de la Vega", hn)
def test75(self):
hn = HumanName("Dr. Juan Q. de la Vega, Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.suffix, "Jr.", hn)
def test76(self):
hn = HumanName("Dr. Juan Q. de la Vega III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.suffix, "III", hn)
def test77(self):
hn = HumanName("de la Vega, Dr. Juan Q.")
self.m(hn.first, "Juan", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.title, "Dr.", hn)
def test78(self):
hn = HumanName("de la Vega, Dr. Juan Q., Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.suffix, "Jr.", hn)
self.m(hn.title, "Dr.", hn)
def test79(self):
hn = HumanName("de la Vega, Dr. Juan Q. III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.suffix, "III", hn)
self.m(hn.title, "Dr.", hn)
def test80(self):
hn = HumanName("Dr. Juan Q. Velasquez y Garcia")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test81(self):
hn = HumanName("Dr. Juan Q. Velasquez y Garcia, Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test82(self):
hn = HumanName("Dr. Juan Q. Velasquez y Garcia III")
self.m(hn.middle, "Q.", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test83(self):
hn = HumanName("Velasquez y Garcia, Dr. Juan Q.")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test84(self):
hn = HumanName("Velasquez y Garcia, Dr. Juan Q., Jr.")
self.m(hn.middle, "Q.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test85(self):
hn = HumanName("Velasquez y Garcia, Dr. Juan Q. III")
self.m(hn.middle, "Q.", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test86(self):
hn = HumanName("Juan Q. Xavier de la Vega")
self.m(hn.first, "Juan", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.last, "de la Vega", hn)
def test87(self):
hn = HumanName("Juan Q. Xavier de la Vega, Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.suffix, "Jr.", hn)
def test88(self):
hn = HumanName("Juan Q. Xavier de la Vega III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.suffix, "III", hn)
def test89(self):
hn = HumanName("de la Vega, Juan Q. Xavier")
self.m(hn.first, "Juan", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.last, "de la Vega", hn)
def test90(self):
hn = HumanName("de la Vega, Juan Q. Xavier, Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.suffix, "Jr.", hn)
def test91(self):
hn = HumanName("de la Vega, Juan Q. Xavier III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.suffix, "III", hn)
def test92(self):
hn = HumanName("Dr. Juan Q. Xavier de la Vega")
self.m(hn.first, "Juan", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.last, "de la Vega", hn)
def test93(self):
hn = HumanName("Dr. Juan Q. Xavier de la Vega, Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.suffix, "Jr.", hn)
def test94(self):
hn = HumanName("Dr. Juan Q. Xavier de la Vega III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.suffix, "III", hn)
def test95(self):
hn = HumanName("de la Vega, Dr. Juan Q. Xavier")
self.m(hn.first, "Juan", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.last, "de la Vega", hn)
def test96(self):
hn = HumanName("de la Vega, Dr. Juan Q. Xavier, Jr.")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.suffix, "Jr.", hn)
def test97(self):
hn = HumanName("de la Vega, Dr. Juan Q. Xavier III")
self.m(hn.first, "Juan", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.last, "de la Vega", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.suffix, "III", hn)
def test98(self):
hn = HumanName("Juan Q. Xavier Velasquez y Garcia")
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test99(self):
hn = HumanName("Juan Q. Xavier Velasquez y Garcia, Jr.")
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test100(self):
hn = HumanName("Juan Q. Xavier Velasquez y Garcia III")
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test101(self):
hn = HumanName("Velasquez y Garcia, Juan Q. Xavier")
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test102(self):
hn = HumanName("Velasquez y Garcia, Juan Q. Xavier, Jr.")
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test103(self):
hn = HumanName("Velasquez y Garcia, Juan Q. Xavier III")
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test104(self):
hn = HumanName("Dr. Juan Q. Xavier Velasquez y Garcia")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test105(self):
hn = HumanName("Dr. Juan Q. Xavier Velasquez y Garcia, Jr.")
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test106(self):
hn = HumanName("Dr. Juan Q. Xavier Velasquez y Garcia III")
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test107(self):
hn = HumanName("Velasquez y Garcia, Dr. Juan Q. Xavier")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
def test108(self):
hn = HumanName("Velasquez y Garcia, Dr. Juan Q. Xavier, Jr.")
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "Jr.", hn)
def test109(self):
hn = HumanName("Velasquez y Garcia, Dr. Juan Q. Xavier III")
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.first, "Juan", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.last, "Velasquez y Garcia", hn)
self.m(hn.suffix, "III", hn)
def test110(self):
hn = HumanName("John Doe, CLU, CFP, LUTC")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "CLU, CFP, LUTC", hn)
def test111(self):
hn = HumanName("John P. Doe, CLU, CFP, LUTC")
self.m(hn.first, "John", hn)
self.m(hn.middle, "P.", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "CLU, CFP, LUTC", hn)
def test112(self):
hn = HumanName("Dr. John P. Doe-Ray, CLU, CFP, LUTC")
self.m(hn.first, "John", hn)
self.m(hn.middle, "P.", hn)
self.m(hn.last, "Doe-Ray", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.suffix, "CLU, CFP, LUTC", hn)
def test113(self):
hn = HumanName("Doe-Ray, Dr. John P., CLU, CFP, LUTC")
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "P.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe-Ray", hn)
self.m(hn.suffix, "CLU, CFP, LUTC", hn)
def test115(self):
hn = HumanName("Hon. Barrington P. Doe-Ray, Jr.")
self.m(hn.title, "Hon.", hn)
self.m(hn.middle, "P.", hn)
self.m(hn.first, "Barrington", hn)
self.m(hn.last, "Doe-Ray", hn)
def test116(self):
hn = HumanName("Doe-Ray, Hon. Barrington P. Jr., CFP, LUTC")
self.m(hn.title, "Hon.", hn)
self.m(hn.middle, "P.", hn)
self.m(hn.first, "Barrington", hn)
self.m(hn.last, "Doe-Ray", hn)
self.m(hn.suffix, "Jr., CFP, LUTC", hn)
def test117(self):
hn = HumanName("Rt. Hon. Paul E. Mary")
self.m(hn.title, "Rt. Hon.", hn)
self.m(hn.first, "Paul", hn)
self.m(hn.middle, "E.", hn)
self.m(hn.last, "Mary", hn)
def test119(self):
hn = HumanName("Lord God Almighty")
self.m(hn.title, "Lord", hn)
self.m(hn.first, "God", hn)
self.m(hn.last, "Almighty", hn)
class HumanNameConjunctionTestCase(HumanNameTestBase):
# Last name with conjunction
def test_last_name_with_conjunction(self):
hn = HumanName('Jose Aznar y Lopez')
self.m(hn.first, "Jose", hn)
self.m(hn.last, "Aznar y Lopez", hn)
def test_multiple_conjunctions(self):
hn = HumanName("part1 of The part2 of the part3 and part4")
self.m(hn.first, "part1 of The part2 of the part3 and part4", hn)
def test_multiple_conjunctions2(self):
hn = HumanName("part1 of and The part2 of the part3 And part4")
self.m(hn.first, "part1 of and The part2 of the part3 And part4", hn)
def test_ends_with_conjunction(self):
hn = HumanName("Jon Dough and")
self.m(hn.first, "Jon", hn)
self.m(hn.last, "Dough and", hn)
def test_ends_with_two_conjunctions(self):
hn = HumanName("Jon Dough and of")
self.m(hn.first, "Jon", hn)
self.m(hn.last, "Dough and of", hn)
def test_starts_with_conjunction(self):
hn = HumanName("and Jon Dough")
self.m(hn.first, "and Jon", hn)
self.m(hn.last, "Dough", hn)
def test_starts_with_two_conjunctions(self):
hn = HumanName("the and Jon Dough")
self.m(hn.first, "the and Jon", hn)
self.m(hn.last, "Dough", hn)
# Potential conjunction/prefix treated as initial (because uppercase)
def test_uppercase_middle_initial_conflict_with_conjunction(self):
hn = HumanName('John E Smith')
self.m(hn.first, "John", hn)
self.m(hn.middle, "E", hn)
self.m(hn.last, "Smith", hn)
def test_lowercase_middle_initial_with_period_conflict_with_conjunction(self):
hn = HumanName('john e. smith')
self.m(hn.first, "john", hn)
self.m(hn.middle, "e.", hn)
self.m(hn.last, "smith", hn)
# The conjunction "e" can also be an initial
def test_lowercase_first_initial_conflict_with_conjunction(self):
hn = HumanName('e j smith')
self.m(hn.first, "e", hn)
self.m(hn.middle, "j", hn)
self.m(hn.last, "smith", hn)
def test_lowercase_middle_initial_conflict_with_conjunction(self):
hn = HumanName('John e Smith')
self.m(hn.first, "John", hn)
self.m(hn.middle, "e", hn)
self.m(hn.last, "Smith", hn)
def test_lowercase_middle_initial_and_suffix_conflict_with_conjunction(self):
hn = HumanName('John e Smith, III')
self.m(hn.first, "John", hn)
self.m(hn.middle, "e", hn)
self.m(hn.last, "Smith", hn)
self.m(hn.suffix, "III", hn)
def test_lowercase_middle_initial_and_nocomma_suffix_conflict_with_conjunction(self):
hn = HumanName('John e Smith III')
self.m(hn.first, "John", hn)
self.m(hn.middle, "e", hn)
self.m(hn.last, "Smith", hn)
self.m(hn.suffix, "III", hn)
def test_lowercase_middle_initial_comma_lastname_and_suffix_conflict_with_conjunction(self):
hn = HumanName('Smith, John e, III, Jr')
self.m(hn.first, "John", hn)
self.m(hn.middle, "e", hn)
self.m(hn.last, "Smith", hn)
self.m(hn.suffix, "III, Jr", hn)
@unittest.expectedFailure
def test_two_initials_conflict_with_conjunction(self):
# Supporting this seems to screw up titles with periods in them like M.B.A.
hn = HumanName('E.T. Smith')
self.m(hn.first, "E.", hn)
self.m(hn.middle, "T.", hn)
self.m(hn.last, "Smith", hn)
def test_couples_names(self):
hn = HumanName('John and Jane Smith')
self.m(hn.first, "John and Jane", hn)
self.m(hn.last, "Smith", hn)
def test_couples_names_with_conjunction_lastname(self):
hn = HumanName('John and Jane Aznar y Lopez')
self.m(hn.first, "John and Jane", hn)
self.m(hn.last, "Aznar y Lopez", hn)
def test_couple_titles(self):
hn = HumanName('Mr. and Mrs. John and Jane Smith')
self.m(hn.title, "Mr. and Mrs.", hn)
self.m(hn.first, "John and Jane", hn)
self.m(hn.last, "Smith", hn)
def test_title_with_three_part_name_last_initial_is_suffix_uppercase_no_period(self):
hn = HumanName("King John Alexander V")
self.m(hn.title, "King", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Alexander", hn)
self.m(hn.suffix, "V", hn)
def test_four_name_parts_with_suffix_that_could_be_initial_lowercase_no_period(self):
hn = HumanName("larry james edward johnson v")
self.m(hn.first, "larry", hn)
self.m(hn.middle, "james edward", hn)
self.m(hn.last, "johnson", hn)
self.m(hn.suffix, "v", hn)
def test_four_name_parts_with_suffix_that_could_be_initial_uppercase_no_period(self):
hn = HumanName("Larry James Johnson I")
self.m(hn.first, "Larry", hn)
self.m(hn.middle, "James", hn)
self.m(hn.last, "Johnson", hn)
self.m(hn.suffix, "I", hn)
def test_roman_numeral_initials(self):
hn = HumanName("Larry V I")
self.m(hn.first, "Larry", hn)
self.m(hn.middle, "V", hn)
self.m(hn.last, "I", hn)
self.m(hn.suffix, "", hn)
# tests for Rev. title (Reverend)
def test124(self):
hn = HumanName("Rev. John A. Kenneth Doe")
self.m(hn.title, "Rev.", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
def test125(self):
hn = HumanName("Rev John A. Kenneth Doe")
self.m(hn.title, "Rev", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
def test126(self):
hn = HumanName("Doe, Rev. John A. Jr.")
self.m(hn.title, "Rev.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
self.m(hn.suffix, "Jr.", hn)
def test127(self):
hn = HumanName("Buca di Beppo")
self.m(hn.first, "Buca", hn)
self.m(hn.last, "di Beppo", hn)
def test_le_as_last_name(self):
hn = HumanName("Yin Le")
self.m(hn.first, "Yin", hn)
self.m(hn.last, "Le", hn)
def test_le_as_last_name_with_middle_initial(self):
hn = HumanName("Yin a Le")
self.m(hn.first, "Yin", hn)
self.m(hn.middle, "a", hn)
self.m(hn.last, "Le", hn)
def test_conjunction_in_an_address_with_a_title(self):
hn = HumanName("His Excellency Lord Duncan")
self.m(hn.title, "His Excellency Lord", hn)
self.m(hn.last, "Duncan", hn)
@unittest.expectedFailure
def test_conjunction_in_an_address_with_a_first_name_title(self):
hn = HumanName("Her Majesty Queen Elizabeth")
self.m(hn.title, "Her Majesty Queen", hn)
# if you want to be technical, Queen is in FIRST_NAME_TITLES
self.m(hn.first, "Elizabeth", hn)
def test_name_is_conjunctions(self):
hn = HumanName("e and e")
self.m(hn.first, "e and e", hn)
class ConstantsCustomization(HumanNameTestBase):
def test_add_title(self):
hn = HumanName("Te Awanui-a-Rangi Black", constants=None)
start_len = len(hn.C.titles)
self.assertTrue(start_len > 0)
hn.C.titles.add('te')
self.assertEqual(start_len + 1, len(hn.C.titles))
hn.parse_full_name()
self.m(hn.title, "Te", hn)
self.m(hn.first, "Awanui-a-Rangi", hn)
self.m(hn.last, "Black", hn)
def test_remove_title(self):
hn = HumanName("Hon Solo", constants=None)
start_len = len(hn.C.titles)
self.assertTrue(start_len > 0)
hn.C.titles.remove('hon')
self.assertEqual(start_len - 1, len(hn.C.titles))
hn.parse_full_name()
self.m(hn.first, "Hon", hn)
self.m(hn.last, "Solo", hn)
def test_add_multiple_arguments(self):
hn = HumanName("Assoc Dean of Chemistry Robert Johns", constants=None)
hn.C.titles.add('dean', 'Chemistry')
hn.parse_full_name()
self.m(hn.title, "Assoc Dean of Chemistry", hn)
self.m(hn.first, "Robert", hn)
self.m(hn.last, "Johns", hn)
def test_instances_can_have_own_constants(self):
hn = HumanName("", None)
hn2 = HumanName("")
hn.C.titles.remove('hon')
self.assertEqual('hon' in hn.C.titles, False)
self.assertEqual(hn.has_own_config, True)
self.assertEqual('hon' in hn2.C.titles, True)
self.assertEqual(hn2.has_own_config, False)
def test_can_change_global_constants(self):
hn = HumanName("")
hn2 = HumanName("")
hn.C.titles.remove('hon')
self.assertEqual('hon' in hn.C.titles, False)
self.assertEqual('hon' in hn2.C.titles, False)
self.assertEqual(hn.has_own_config, False)
self.assertEqual(hn2.has_own_config, False)
# clean up so we don't mess up other tests
hn.C.titles.add('hon')
def test_remove_multiple_arguments(self):
hn = HumanName("Ms Hon Solo", constants=None)
hn.C.titles.remove('hon', 'ms')
hn.parse_full_name()
self.m(hn.first, "Ms", hn)
self.m(hn.middle, "Hon", hn)
self.m(hn.last, "Solo", hn)
def test_chain_multiple_arguments(self):
hn = HumanName("Dean Ms Hon Solo", constants=None)
hn.C.titles.remove('hon', 'ms').add('dean')
hn.parse_full_name()
self.m(hn.title, "Dean", hn)
self.m(hn.first, "Ms", hn)
self.m(hn.middle, "Hon", hn)
self.m(hn.last, "Solo", hn)
def test_empty_attribute_default(self):
from nameparser.config import CONSTANTS
_orig = CONSTANTS.empty_attribute_default
CONSTANTS.empty_attribute_default = None
hn = HumanName("")
self.m(hn.title, None, hn)
self.m(hn.first, None, hn)
self.m(hn.middle, None, hn)
self.m(hn.last, None, hn)
self.m(hn.suffix, None, hn)
self.m(hn.nickname, None, hn)
CONSTANTS.empty_attribute_default = _orig
def test_empty_attribute_on_instance(self):
hn = HumanName("", None)
hn.C.empty_attribute_default = None
self.m(hn.title, None, hn)
self.m(hn.first, None, hn)
self.m(hn.middle, None, hn)
self.m(hn.last, None, hn)
self.m(hn.suffix, None, hn)
self.m(hn.nickname, None, hn)
def test_none_empty_attribute_string_formatting(self):
hn = HumanName("", None)
hn.C.empty_attribute_default = None
self.assertEqual('', str(hn), hn)
def test_add_constant_with_explicit_encoding(self):
c = Constants()
c.titles.add_with_encoding(b'b\351ck', encoding='latin_1')
self.assertIn('béck', c.titles)
class NicknameTestCase(HumanNameTestBase):
# https://code.google.com/p/python-nameparser/issues/detail?id=33
def test_nickname_in_parenthesis(self):
hn = HumanName("Benjamin (Ben) Franklin")
self.m(hn.first, "Benjamin", hn)
self.m(hn.middle, "", hn)
self.m(hn.last, "Franklin", hn)
self.m(hn.nickname, "Ben", hn)
def test_two_word_nickname_in_parenthesis(self):
hn = HumanName("Benjamin (Big Ben) Franklin")
self.m(hn.first, "Benjamin", hn)
self.m(hn.middle, "", hn)
self.m(hn.last, "Franklin", hn)
self.m(hn.nickname, "Big Ben", hn)
def test_two_words_in_quotes(self):
hn = HumanName('Benjamin "Big Ben" Franklin')
self.m(hn.first, "Benjamin", hn)
self.m(hn.middle, "", hn)
self.m(hn.last, "Franklin", hn)
self.m(hn.nickname, "Big Ben", hn)
def test_nickname_in_parenthesis_with_comma(self):
hn = HumanName("Franklin, Benjamin (Ben)")
self.m(hn.first, "Benjamin", hn)
self.m(hn.middle, "", hn)
self.m(hn.last, "Franklin", hn)
self.m(hn.nickname, "Ben", hn)
def test_nickname_in_parenthesis_with_comma_and_suffix(self):
hn = HumanName("Franklin, Benjamin (Ben), Jr.")
self.m(hn.first, "Benjamin", hn)
self.m(hn.middle, "", hn)
self.m(hn.last, "Franklin", hn)
self.m(hn.suffix, "Jr.", hn)
self.m(hn.nickname, "Ben", hn)
def test_nickname_in_single_quotes(self):
hn = HumanName("Benjamin 'Ben' Franklin")
self.m(hn.first, "Benjamin", hn)
self.m(hn.middle, "", hn)
self.m(hn.last, "Franklin", hn)
self.m(hn.nickname, "Ben", hn)
def test_nickname_in_double_quotes(self):
hn = HumanName("Benjamin \"Ben\" Franklin")
self.m(hn.first, "Benjamin", hn)
self.m(hn.middle, "", hn)
self.m(hn.last, "Franklin", hn)
self.m(hn.nickname, "Ben", hn)
def test_single_quotes_on_first_name_not_treated_as_nickname(self):
hn = HumanName("Brian Andrew O'connor")
self.m(hn.first, "Brian", hn)
self.m(hn.middle, "Andrew", hn)
self.m(hn.last, "O'connor", hn)
self.m(hn.nickname, "", hn)
def test_single_quotes_on_both_name_not_treated_as_nickname(self):
hn = HumanName("La'tanya O'connor")
self.m(hn.first, "La'tanya", hn)
self.m(hn.middle, "", hn)
self.m(hn.last, "O'connor", hn)
self.m(hn.nickname, "", hn)
def test_single_quotes_on_end_of_last_name_not_treated_as_nickname(self):
hn = HumanName("Mari' Aube'")
self.m(hn.first, "Mari'", hn)
self.m(hn.middle, "", hn)
self.m(hn.last, "Aube'", hn)
self.m(hn.nickname, "", hn)
def test_okina_inside_name_not_treated_as_nickname(self):
hn = HumanName("Harrieta Keōpūolani Nāhiʻenaʻena")
self.m(hn.first, "Harrieta", hn)
self.m(hn.middle, "Keōpūolani", hn)
self.m(hn.last, "Nāhiʻenaʻena", hn)
self.m(hn.nickname, "", hn)
def test_single_quotes_not_treated_as_nickname_Hawaiian_example(self):
hn = HumanName("Harietta Keopuolani Nahi'ena'ena")
self.m(hn.first, "Harietta", hn)
self.m(hn.middle, "Keopuolani", hn)
self.m(hn.last, "Nahi'ena'ena", hn)
self.m(hn.nickname, "", hn)
def test_single_quotes_not_treated_as_nickname_Kenyan_example(self):
hn = HumanName("Naomi Wambui Ng'ang'a")
self.m(hn.first, "Naomi", hn)
self.m(hn.middle, "Wambui", hn)
self.m(hn.last, "Ng'ang'a", hn)
self.m(hn.nickname, "", hn)
def test_single_quotes_not_treated_as_nickname_Samoan_example(self):
hn = HumanName("Va'apu'u Vitale")
self.m(hn.first, "Va'apu'u", hn)
self.m(hn.middle, "", hn)
self.m(hn.last, "Vitale", hn)
self.m(hn.nickname, "", hn)
# http://code.google.com/p/python-nameparser/issues/detail?id=17
def test_parenthesis_are_removed_from_name(self):
hn = HumanName("John Jones (Unknown)")
self.m(hn.first, "John", hn)
self.m(hn.last, "Jones", hn)
# not testing the nicknames because we don't actually care
# about Google Docs here
def test_duplicate_parenthesis_are_removed_from_name(self):
hn = HumanName("John Jones (Google Docs), Jr. (Unknown)")
self.m(hn.first, "John", hn)
self.m(hn.last, "Jones", hn)
self.m(hn.suffix, "Jr.", hn)
def test_nickname_and_last_name(self):
hn = HumanName('"Rick" Edmonds')
self.m(hn.first, "", hn)
self.m(hn.last, "Edmonds", hn)
self.m(hn.nickname, "Rick", hn)
@unittest.expectedFailure
def test_nickname_and_last_name_with_title(self):
hn = HumanName('Senator "Rick" Edmonds')
self.m(hn.title, "Senator", hn)
self.m(hn.first, "", hn)
self.m(hn.last, "Edmonds", hn)
self.m(hn.nickname, "Rick", hn)
# class MaidenNameTestCase(HumanNameTestBase):
#
# def test_parenthesis_and_quotes_together(self):
# hn = HumanName("Jennifer 'Jen' Jones (Duff)")
# self.m(hn.first, "Jennifer", hn)
# self.m(hn.last, "Jones", hn)
# self.m(hn.nickname, "Jen", hn)
# self.m(hn.maiden, "Duff", hn)
#
# def test_maiden_name_with_nee(self):
# # https://en.wiktionary.org/wiki/née
# hn = HumanName("Mary Toogood nee Johnson")
# self.m(hn.first, "Mary", hn)
# self.m(hn.last, "Toogood", hn)
# self.m(hn.maiden, "Johnson", hn)
#
# def test_maiden_name_with_accented_nee(self):
# # https://en.wiktionary.org/wiki/née
# hn = HumanName("Mary Toogood née Johnson")
# self.m(hn.first, "Mary", hn)
# self.m(hn.last, "Toogood", hn)
# self.m(hn.maiden, "Johnson", hn)
#
# def test_maiden_name_with_nee_and_comma(self):
# # https://en.wiktionary.org/wiki/née
# hn = HumanName("Mary Toogood, née Johnson")
# self.m(hn.first, "Mary", hn)
# self.m(hn.last, "Toogood", hn)
# self.m(hn.maiden, "Johnson", hn)
#
# def test_maiden_name_with_nee_with_parenthesis(self):
# hn = HumanName("Mary Toogood (nee Johnson)")
# self.m(hn.first, "Mary", hn)
# self.m(hn.last, "Toogood", hn)
# self.m(hn.maiden, "Johnson", hn)
#
# def test_maiden_name_with_parenthesis(self):
# hn = HumanName("Mary Toogood (Johnson)")
# self.m(hn.first, "Mary", hn)
# self.m(hn.last, "Toogood", hn)
# self.m(hn.maiden, "Johnson", hn)
#
class PrefixesTestCase(HumanNameTestBase):
def test_prefix(self):
hn = HumanName("Juan del Sur")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "del Sur", hn)
def test_prefix_with_period(self):
hn = HumanName("Jill St. John")
self.m(hn.first, "Jill", hn)
self.m(hn.last, "St. John", hn)
def test_prefix_before_two_part_last_name(self):
hn = HumanName("pennie von bergen wessels")
self.m(hn.first, "pennie", hn)
self.m(hn.last, "von bergen wessels", hn)
def test_prefix_is_first_name(self):
hn = HumanName("Van Johnson")
self.m(hn.first, "Van", hn)
self.m(hn.last, "Johnson", hn)
def test_prefix_is_first_name_with_middle_name(self):
hn = HumanName("Van Jeremy Johnson")
self.m(hn.first, "Van", hn)
self.m(hn.middle, "Jeremy", hn)
self.m(hn.last, "Johnson", hn)
def test_prefix_before_two_part_last_name_with_suffix(self):
hn = HumanName("pennie von bergen wessels III")
self.m(hn.first, "pennie", hn)
self.m(hn.last, "von bergen wessels", hn)
self.m(hn.suffix, "III", hn)
def test_prefix_before_two_part_last_name_with_acronym_suffix(self):
hn = HumanName("pennie von bergen wessels M.D.")
self.m(hn.first, "pennie", hn)
self.m(hn.last, "von bergen wessels", hn)
self.m(hn.suffix, "M.D.", hn)
def test_two_part_last_name_with_suffix_comma(self):
hn = HumanName("pennie von bergen wessels, III")
self.m(hn.first, "pennie", hn)
self.m(hn.last, "von bergen wessels", hn)
self.m(hn.suffix, "III", hn)
def test_two_part_last_name_with_suffix(self):
hn = HumanName("von bergen wessels, pennie III")
self.m(hn.first, "pennie", hn)
self.m(hn.last, "von bergen wessels", hn)
self.m(hn.suffix, "III", hn)
def test_last_name_two_part_last_name_with_two_suffixes(self):
hn = HumanName("von bergen wessels MD, pennie III")
self.m(hn.first, "pennie", hn)
self.m(hn.last, "von bergen wessels", hn)
self.m(hn.suffix, "MD, III", hn)
def test_comma_two_part_last_name_with_acronym_suffix(self):
hn = HumanName("von bergen wessels, pennie MD")
self.m(hn.first, "pennie", hn)
self.m(hn.last, "von bergen wessels", hn)
self.m(hn.suffix, "MD", hn)
def test_comma_two_part_last_name_with_suffix_in_first_part(self):
# I'm kinda surprised this works, not really sure if this is a
# realistic place for a suffix to be.
hn = HumanName("von bergen wessels MD, pennie")
self.m(hn.first, "pennie", hn)
self.m(hn.last, "von bergen wessels", hn)
self.m(hn.suffix, "MD", hn)
def test_title_two_part_last_name_with_suffix_in_first_part(self):
hn = HumanName("pennie von bergen wessels MD, III")
self.m(hn.first, "pennie", hn)
self.m(hn.last, "von bergen wessels", hn)
self.m(hn.suffix, "MD, III", hn)
def test_portuguese_dos(self):
hn = HumanName("Rafael Sousa dos Anjos")
self.m(hn.first, "Rafael", hn)
self.m(hn.middle, "Sousa", hn)
self.m(hn.last, "dos Anjos", hn)
def test_portuguese_prefixes(self):
hn = HumanName("Joao da Silva do Amaral de Souza")
self.m(hn.first, "Joao", hn)
self.m(hn.middle, "da Silva do Amaral", hn)
self.m(hn.last, "de Souza", hn)
def test_three_conjunctions(self):
hn = HumanName("Dr. Juan Q. Xavier de la dos Vega III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la dos Vega", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.suffix, "III", hn)
def test_lastname_three_conjunctions(self):
hn = HumanName("de la dos Vega, Dr. Juan Q. Xavier III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la dos Vega", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.suffix, "III", hn)
def test_comma_three_conjunctions(self):
hn = HumanName("Dr. Juan Q. Xavier de la dos Vega, III")
self.m(hn.first, "Juan", hn)
self.m(hn.last, "de la dos Vega", hn)
self.m(hn.title, "Dr.", hn)
self.m(hn.middle, "Q. Xavier", hn)
self.m(hn.suffix, "III", hn)
class SuffixesTestCase(HumanNameTestBase):
def test_suffix(self):
hn = HumanName("Joe Franklin Jr")
self.m(hn.first, "Joe", hn)
self.m(hn.last, "Franklin", hn)
self.m(hn.suffix, "Jr", hn)
def test_suffix_with_periods(self):
hn = HumanName("Joe Dentist D.D.S.")
self.m(hn.first, "Joe", hn)
self.m(hn.last, "Dentist", hn)
self.m(hn.suffix, "D.D.S.", hn)
def test_two_suffixes(self):
hn = HumanName("Kenneth Clarke QC MP")
self.m(hn.first, "Kenneth", hn)
self.m(hn.last, "Clarke", hn)
# NOTE: this adds a comma when the original format did not have one.
# not ideal but at least its in the right bucket
self.m(hn.suffix, "QC, MP", hn)
def test_two_suffixes_lastname_comma_format(self):
hn = HumanName("Washington Jr. MD, Franklin")
self.m(hn.first, "Franklin", hn)
self.m(hn.last, "Washington", hn)
# NOTE: this adds a comma when the original format did not have one.
self.m(hn.suffix, "Jr., MD", hn)
def test_two_suffixes_suffix_comma_format(self):
hn = HumanName("Franklin Washington, Jr. MD")
self.m(hn.first, "Franklin", hn)
self.m(hn.last, "Washington", hn)
self.m(hn.suffix, "Jr. MD", hn)
def test_suffix_containing_periods(self):
hn = HumanName("Kenneth Clarke Q.C.")
self.m(hn.first, "Kenneth", hn)
self.m(hn.last, "Clarke", hn)
self.m(hn.suffix, "Q.C.", hn)
def test_suffix_containing_periods_lastname_comma_format(self):
hn = HumanName("Clarke, Kenneth, Q.C. M.P.")
self.m(hn.first, "Kenneth", hn)
self.m(hn.last, "Clarke", hn)
self.m(hn.suffix, "Q.C. M.P.", hn)
def test_suffix_containing_periods_suffix_comma_format(self):
hn = HumanName("Kenneth Clarke Q.C., M.P.")
self.m(hn.first, "Kenneth", hn)
self.m(hn.last, "Clarke", hn)
self.m(hn.suffix, "Q.C., M.P.", hn)
def test_suffix_with_single_comma_format(self):
hn = HumanName("John Doe jr., MD")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "jr., MD", hn)
def test_suffix_with_double_comma_format(self):
hn = HumanName("Doe, John jr., MD")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "jr., MD", hn)
def test_phd_with_erroneous_space(self):
hn = HumanName("John Smith, Ph. D.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Smith", hn)
self.m(hn.suffix, "Ph. D.", hn)
def test_phd_conflict(self):
hn = HumanName("Adolph D")
self.m(hn.first, "Adolph", hn)
self.m(hn.last, "D", hn)
# http://en.wikipedia.org/wiki/Ma_(surname)
def test_potential_suffix_that_is_also_last_name(self):
hn = HumanName("Jack Ma")
self.m(hn.first, "Jack", hn)
self.m(hn.last, "Ma", hn)
def test_potential_suffix_that_is_also_last_name_comma(self):
hn = HumanName("Ma, Jack")
self.m(hn.first, "Jack", hn)
self.m(hn.last, "Ma", hn)
def test_potential_suffix_that_is_also_last_name_with_suffix(self):
hn = HumanName("Jack Ma Jr")
self.m(hn.first, "Jack", hn)
self.m(hn.last, "Ma", hn)
self.m(hn.suffix, "Jr", hn)
def test_potential_suffix_that_is_also_last_name_with_suffix_comma(self):
hn = HumanName("Ma III, Jack Jr")
self.m(hn.first, "Jack", hn)
self.m(hn.last, "Ma", hn)
self.m(hn.suffix, "III, Jr", hn)
# https://github.com/derek73/python-nameparser/issues/27
@unittest.expectedFailure
def test_king(self):
hn = HumanName("Dr King Jr")
self.m(hn.title, "Dr", hn)
self.m(hn.last, "King", hn)
self.m(hn.suffix, "Jr", hn)
def test_multiple_letter_suffix_with_periods(self):
hn = HumanName("John Doe Msc.Ed.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Msc.Ed.", hn)
def test_suffix_with_periods_with_comma(self):
hn = HumanName("John Doe, Msc.Ed.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Msc.Ed.", hn)
def test_suffix_with_periods_with_lastname_comma(self):
hn = HumanName("Doe, John Msc.Ed.")
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.suffix, "Msc.Ed.", hn)
class TitleTestCase(HumanNameTestBase):
def test_last_name_is_also_title(self):
hn = HumanName("Amy E Maid")
self.m(hn.first, "Amy", hn)
self.m(hn.middle, "E", hn)
self.m(hn.last, "Maid", hn)
def test_last_name_is_also_title_no_comma(self):
hn = HumanName("Dr. Martin Luther King Jr.")
self.m(hn.title, "Dr.", hn)
self.m(hn.first, "Martin", hn)
self.m(hn.middle, "Luther", hn)
self.m(hn.last, "King", hn)
self.m(hn.suffix, "Jr.", hn)
def test_last_name_is_also_title_with_comma(self):
hn = HumanName("Dr Martin Luther King, Jr.")
self.m(hn.title, "Dr", hn)
self.m(hn.first, "Martin", hn)
self.m(hn.middle, "Luther", hn)
self.m(hn.last, "King", hn)
self.m(hn.suffix, "Jr.", hn)
def test_last_name_is_also_title3(self):
hn = HumanName("John King")
self.m(hn.first, "John", hn)
self.m(hn.last, "King", hn)
def test_title_with_conjunction(self):
hn = HumanName("Secretary of State Hillary Clinton")
self.m(hn.title, "Secretary of State", hn)
self.m(hn.first, "Hillary", hn)
self.m(hn.last, "Clinton", hn)
def test_compound_title_with_conjunction(self):
hn = HumanName("Cardinal Secretary of State Hillary Clinton")
self.m(hn.title, "Cardinal Secretary of State", hn)
self.m(hn.first, "Hillary", hn)
self.m(hn.last, "Clinton", hn)
def test_title_is_title(self):
hn = HumanName("Coach")
self.m(hn.title, "Coach", hn)
# TODO: fix handling of U.S.
@unittest.expectedFailure
def test_chained_title_first_name_title_is_initials(self):
hn = HumanName("U.S. District Judge Marc Thomas Treadwell")
self.m(hn.title, "U.S. District Judge", hn)
self.m(hn.first, "Marc", hn)
self.m(hn.middle, "Thomas", hn)
self.m(hn.last, "Treadwell", hn)
def test_conflict_with_chained_title_first_name_initial(self):
hn = HumanName("U. S. Grant")
self.m(hn.first, "U.", hn)
self.m(hn.middle, "S.", hn)
self.m(hn.last, "Grant", hn)
def test_chained_title_first_name_initial_with_no_period(self):
hn = HumanName("US Magistrate Judge T Michael Putnam")
self.m(hn.title, "US Magistrate Judge", hn)
self.m(hn.first, "T", hn)
self.m(hn.middle, "Michael", hn)
self.m(hn.last, "Putnam", hn)
def test_chained_hyphenated_title(self):
hn = HumanName("US Magistrate-Judge Elizabeth E Campbell")
self.m(hn.title, "US Magistrate-Judge", hn)
self.m(hn.first, "Elizabeth", hn)
self.m(hn.middle, "E", hn)
self.m(hn.last, "Campbell", hn)
def test_chained_hyphenated_title_with_comma_suffix(self):
hn = HumanName("Mag-Judge Harwell G Davis, III")
self.m(hn.title, "Mag-Judge", hn)
self.m(hn.first, "Harwell", hn)
self.m(hn.middle, "G", hn)
self.m(hn.last, "Davis", hn)
self.m(hn.suffix, "III", hn)
@unittest.expectedFailure
def test_title_multiple_titles_with_apostrophe_s(self):
hn = HumanName("The Right Hon. the President of the Queen's Bench Division")
self.m(hn.title, "The Right Hon. the President of the Queen's Bench Division", hn)
def test_title_starts_with_conjunction(self):
hn = HumanName("The Rt Hon John Jones")
self.m(hn.title, "The Rt Hon", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Jones", hn)
def test_conjunction_before_title(self):
hn = HumanName('The Lord of the Universe')
self.m(hn.title, "The Lord of the Universe", hn)
def test_double_conjunction_on_title(self):
hn = HumanName('Lord of the Universe')
self.m(hn.title, "Lord of the Universe", hn)
def test_triple_conjunction_on_title(self):
hn = HumanName('Lord and of the Universe')
self.m(hn.title, "Lord and of the Universe", hn)
def test_multiple_conjunctions_on_multiple_titles(self):
hn = HumanName('Lord of the Universe and Associate Supreme Queen of the World Lisa Simpson')
self.m(hn.title, "Lord of the Universe and Associate Supreme Queen of the World", hn)
self.m(hn.first, "Lisa", hn)
self.m(hn.last, "Simpson", hn)
def test_title_with_last_initial_is_suffix(self):
hn = HumanName("King John V.")
self.m(hn.title, "King", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "V.", hn)
def test_initials_also_suffix(self):
hn = HumanName("Smith, J.R.")
self.m(hn.first, "J.R.", hn)
# self.m(hn.middle, "R.", hn)
self.m(hn.last, "Smith", hn)
def test_two_title_parts_separated_by_periods(self):
hn = HumanName("Lt.Gen. John A. Kenneth Doe IV")
self.m(hn.title, "Lt.Gen.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.suffix, "IV", hn)
def test_two_part_title(self):
hn = HumanName("Lt. Gen. John A. Kenneth Doe IV")
self.m(hn.title, "Lt. Gen.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.suffix, "IV", hn)
def test_two_part_title_with_lastname_comma(self):
hn = HumanName("Doe, Lt. Gen. John A. Kenneth IV")
self.m(hn.title, "Lt. Gen.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.suffix, "IV", hn)
def test_two_part_title_with_suffix_comma(self):
hn = HumanName("Lt. Gen. John A. Kenneth Doe, Jr.")
self.m(hn.title, "Lt. Gen.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A. Kenneth", hn)
self.m(hn.suffix, "Jr.", hn)
def test_possible_conflict_with_middle_initial_that_could_be_suffix(self):
hn = HumanName("Doe, Rev. John V, Jr.")
self.m(hn.title, "Rev.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "V", hn)
self.m(hn.suffix, "Jr.", hn)
def test_possible_conflict_with_suffix_that_could_be_initial(self):
hn = HumanName("Doe, Rev. John A., V, Jr.")
self.m(hn.title, "Rev.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
self.m(hn.middle, "A.", hn)
self.m(hn.suffix, "V, Jr.", hn)
# 'ben' is removed from PREFIXES in v0.2.5
# this test could re-enable this test if we decide to support 'ben' as a prefix
@unittest.expectedFailure
def test_ben_as_conjunction(self):
hn = HumanName("Ahmad ben Husain")
self.m(hn.first, "Ahmad", hn)
self.m(hn.last, "ben Husain", hn)
def test_ben_as_first_name(self):
hn = HumanName("Ben Johnson")
self.m(hn.first, "Ben", hn)
self.m(hn.last, "Johnson", hn)
def test_ben_as_first_name_with_middle_name(self):
hn = HumanName("Ben Alex Johnson")
self.m(hn.first, "Ben", hn)
self.m(hn.middle, "Alex", hn)
self.m(hn.last, "Johnson", hn)
def test_ben_as_middle_name(self):
hn = HumanName("Alex Ben Johnson")
self.m(hn.first, "Alex", hn)
self.m(hn.middle, "Ben", hn)
self.m(hn.last, "Johnson", hn)
# http://code.google.com/p/python-nameparser/issues/detail?id=13
def test_last_name_also_prefix(self):
hn = HumanName("Jane Doctor")
self.m(hn.first, "Jane", hn)
self.m(hn.last, "Doctor", hn)
def test_title_with_periods(self):
hn = HumanName("Lt.Gov. John Doe")
self.m(hn.title, "Lt.Gov.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
def test_title_with_periods_lastname_comma(self):
hn = HumanName("Doe, Lt.Gov. John")
self.m(hn.title, "Lt.Gov.", hn)
self.m(hn.first, "John", hn)
self.m(hn.last, "Doe", hn)
def test_mac_with_spaces(self):
hn = HumanName("Jane Mac Beth")
self.m(hn.first, "Jane", hn)
self.m(hn.last, "Mac Beth", hn)
def test_mac_as_first_name(self):
hn = HumanName("Mac Miller")
self.m(hn.first, "Mac", hn)
self.m(hn.last, "Miller", hn)
def test_multiple_prefixes(self):
hn = HumanName("Mike van der Velt")
self.m(hn.first, "Mike", hn)
self.m(hn.last, "van der Velt", hn)
def test_2_same_prefixes_in_the_name(self):
hh = HumanName("Vincent van Gogh van Beethoven")
self.m(hh.first, "Vincent", hh)
self.m(hh.middle, "van Gogh", hh)
self.m(hh.last, "van Beethoven", hh)
class HumanNameCapitalizationTestCase(HumanNameTestBase):
def test_capitalization_exception_for_III(self):
hn = HumanName('juan q. xavier velasquez y garcia iii')
hn.capitalize()
self.m(str(hn), 'Juan Q. Xavier Velasquez y Garcia III', hn)
# FIXME: this test does not pass due to a known issue
# http://code.google.com/p/python-nameparser/issues/detail?id=22
@unittest.expectedFailure
def test_capitalization_exception_for_already_capitalized_III_KNOWN_FAILURE(self):
hn = HumanName('juan garcia III')
hn.capitalize()
self.m(str(hn), 'Juan Garcia III', hn)
def test_capitalize_title(self):
hn = HumanName('lt. gen. john a. kenneth doe iv')
hn.capitalize()
self.m(str(hn), 'Lt. Gen. John A. Kenneth Doe IV', hn)
def test_capitalize_title_to_lower(self):
hn = HumanName('LT. GEN. JOHN A. KENNETH DOE IV')
hn.capitalize()
self.m(str(hn), 'Lt. Gen. John A. Kenneth Doe IV', hn)
# Capitalization with M(a)c and hyphenated names
def test_capitalization_with_Mac_as_hyphenated_names(self):
hn = HumanName('donovan mcnabb-smith')
hn.capitalize()
self.m(str(hn), 'Donovan McNabb-Smith', hn)
def test_capitization_middle_initial_is_also_a_conjunction(self):
hn = HumanName('scott e. werner')
hn.capitalize()
self.m(str(hn), 'Scott E. Werner', hn)
# Leaving already-capitalized names alone
def test_no_change_to_mixed_chase(self):
hn = HumanName('Shirley Maclaine')
hn.capitalize()
self.m(str(hn), 'Shirley Maclaine', hn)
def test_force_capitalization(self):
hn = HumanName('Shirley Maclaine')
hn.capitalize(force=True)
self.m(str(hn), 'Shirley MacLaine', hn)
def test_capitalize_diacritics(self):
hn = HumanName('matthëus schmidt')
hn.capitalize()
self.m(u(hn), 'Matthëus Schmidt', hn)
# http://code.google.com/p/python-nameparser/issues/detail?id=15
def test_downcasing_mac(self):
hn = HumanName('RONALD MACDONALD')
hn.capitalize()
self.m(str(hn), 'Ronald MacDonald', hn)
# http://code.google.com/p/python-nameparser/issues/detail?id=23
def test_downcasing_mc(self):
hn = HumanName('RONALD MCDONALD')
hn.capitalize()
self.m(str(hn), 'Ronald McDonald', hn)
def test_short_names_with_mac(self):
hn = HumanName('mack johnson')
hn.capitalize()
self.m(str(hn), 'Mack Johnson', hn)
def test_portuguese_prefixes(self):
hn = HumanName("joao da silva do amaral de souza")
hn.capitalize()
self.m(str(hn), 'Joao da Silva do Amaral de Souza', hn)
def test_capitalize_prefix_clash_on_first_name(self):
hn = HumanName("van nguyen")
hn.capitalize()
self.m(str(hn), 'Van Nguyen', hn)
class HumanNameOutputFormatTests(HumanNameTestBase):
def test_formatting_init_argument(self):
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)",
string_format="TEST1")
self.assertEqual(u(hn), "TEST1")
def test_formatting_constants_attribute(self):
from nameparser.config import CONSTANTS
_orig = CONSTANTS.string_format
CONSTANTS.string_format = "TEST2"
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
self.assertEqual(u(hn), "TEST2")
CONSTANTS.string_format = _orig
def test_capitalize_name_constants_attribute(self):
from nameparser.config import CONSTANTS
CONSTANTS.capitalize_name = True
hn = HumanName("bob v. de la macdole-eisenhower phd")
self.assertEqual(str(hn), "Bob V. de la MacDole-Eisenhower Ph.D.")
CONSTANTS.capitalize_name = False
def test_force_mixed_case_capitalization_constants_attribute(self):
from nameparser.config import CONSTANTS
CONSTANTS.force_mixed_case_capitalization = True
hn = HumanName('Shirley Maclaine')
hn.capitalize()
self.assertEqual(str(hn), "Shirley MacLaine")
CONSTANTS.force_mixed_case_capitalization = False
def test_capitalize_name_and_force_mixed_case_capitalization_constants_attributes(self):
from nameparser.config import CONSTANTS
CONSTANTS.capitalize_name = True
CONSTANTS.force_mixed_case_capitalization = True
hn = HumanName('Shirley Maclaine')
self.assertEqual(str(hn), "Shirley MacLaine")
def test_quote_nickname_formating(self):
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} '{nickname}'"
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III 'Kenny'")
hn.string_format = "{last}, {title} {first} {middle}, {suffix} '{nickname}'"
self.assertEqual(u(hn), "Doe, Rev John A. Kenneth, III 'Kenny'")
def test_formating_removing_keys_from_format_string(self):
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} '{nickname}'"
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III 'Kenny'")
hn.string_format = "{last}, {title} {first} {middle}, {suffix}"
self.assertEqual(u(hn), "Doe, Rev John A. Kenneth, III")
hn.string_format = "{last}, {title} {first} {middle}"
self.assertEqual(u(hn), "Doe, Rev John A. Kenneth")
hn.string_format = "{last}, {first} {middle}"
self.assertEqual(u(hn), "Doe, John A. Kenneth")
hn.string_format = "{last}, {first}"
self.assertEqual(u(hn), "Doe, John")
hn.string_format = "{first} {last}"
self.assertEqual(u(hn), "John Doe")
def test_formating_removing_pieces_from_name_buckets(self):
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} '{nickname}'"
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III 'Kenny'")
hn.string_format = "{title} {first} {middle} {last} {suffix}"
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III")
hn.middle = ''
self.assertEqual(u(hn), "Rev John Doe III")
hn.suffix = ''
self.assertEqual(u(hn), "Rev John Doe")
hn.title = ''
self.assertEqual(u(hn), "John Doe")
def test_formating_of_nicknames_with_parenthesis(self):
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} ({nickname})"
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III (Kenny)")
hn.nickname = ''
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III")
def test_formating_of_nicknames_with_single_quotes(self):
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} '{nickname}'"
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III 'Kenny'")
hn.nickname = ''
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III")
def test_formating_of_nicknames_with_double_quotes(self):
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} {middle} {last} {suffix} \"{nickname}\""
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III \"Kenny\"")
hn.nickname = ''
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III")
def test_formating_of_nicknames_in_middle(self):
hn = HumanName("Rev John A. Kenneth Doe III (Kenny)")
hn.string_format = "{title} {first} ({nickname}) {middle} {last} {suffix}"
self.assertEqual(u(hn), "Rev John (Kenny) A. Kenneth Doe III")
hn.nickname = ''
self.assertEqual(u(hn), "Rev John A. Kenneth Doe III")
def test_remove_emojis(self):
hn = HumanName("Sam Smith 😊")
self.m(hn.first, "Sam", hn)
self.m(hn.last, "Smith", hn)
self.assertEqual(u(hn), "Sam Smith")
def test_keep_non_emojis(self):
hn = HumanName("∫≜⩕ Smith 😊")
self.m(hn.first, "∫≜⩕", hn)
self.m(hn.last, "Smith", hn)
self.assertEqual(u(hn), "∫≜⩕ Smith")
def test_keep_emojis(self):
from nameparser.config import Constants
constants = Constants()
constants.regexes.emoji = False
hn = HumanName("∫≜⩕ Smith😊", constants)
self.m(hn.first, "∫≜⩕", hn)
self.m(hn.last, "Smith😊", hn)
self.assertEqual(u(hn), "∫≜⩕ Smith😊")
# test cleanup
class InitialsTestCase(HumanNameTestBase):
def test_initials(self):
hn = HumanName("Andrew Boris Petersen")
self.m(hn.initials(), "A. B. P.", hn)
def test_initials_simple_name(self):
hn = HumanName("John Doe")
self.m(hn.initials(), "J. D.", hn)
hn = HumanName("John Doe", initials_format="{first} {last}")
self.m(hn.initials(), "J. D.", hn)
hn = HumanName("John Doe", initials_format="{last}")
self.m(hn.initials(), "D.", hn)
hn = HumanName("John Doe", initials_format="{first}")
self.m(hn.initials(), "J.", hn)
hn = HumanName("John Doe", initials_format="{middle}")
self.m(hn.initials(), "", hn)
def test_initials_complex_name(self):
hn = HumanName("Doe, John A. Kenneth, Jr.")
self.m(hn.initials(), "J. A. K. D.", hn)
def test_initials_format(self):
hn = HumanName("Doe, John A. Kenneth, Jr.", initials_format="{first} {middle}")
self.m(hn.initials(), "J. A. K.", hn)
hn = HumanName("Doe, John A. Kenneth, Jr.", initials_format="{first} {last}")
self.m(hn.initials(), "J. D.", hn)
hn = HumanName("Doe, John A. Kenneth, Jr.", initials_format="{middle} {last}")
self.m(hn.initials(), "A. K. D.", hn)
hn = HumanName("Doe, John A. Kenneth, Jr.", initials_format="{first}, {last}")
self.m(hn.initials(), "J., D.", hn)
def test_initials_format_constants(self):
from nameparser.config import CONSTANTS
_orig = CONSTANTS.initials_format
CONSTANTS.initials_format = "{first} {last}"
hn = HumanName("Doe, John A. Kenneth, Jr.")
self.m(hn.initials(), "J. D.", hn)
CONSTANTS.initials_format = "{first} {last}"
hn = HumanName("Doe, John A. Kenneth, Jr.")
self.m(hn.initials(), "J. D.", hn)
CONSTANTS.initials_format = _orig
def test_initials_delimiter(self):
hn = HumanName("Doe, John A. Kenneth, Jr.", initials_delimiter=";")
self.m(hn.initials(), "J; A; K; D;", hn)
def test_initials_delimiter_constants(self):
from nameparser.config import CONSTANTS
_orig = CONSTANTS.initials_delimiter
CONSTANTS.initials_delimiter = ";"
hn = HumanName("Doe, John A. Kenneth, Jr.")
self.m(hn.initials(), "J; A; K; D;", hn)
CONSTANTS.initials_delimiter = _orig
def test_initials_list(self):
hn = HumanName("Andrew Boris Petersen")
self.m(hn.initials_list(), ["A", "B", "P"], hn)
def test_initials_list_complex_name(self):
hn = HumanName("Doe, John A. Kenneth, Jr.")
self.m(hn.initials_list(), ["J", "A", "K", "D"], hn)
def test_initials_with_prefix_firstname(self):
hn = HumanName("Van Jeremy Johnson")
self.m(hn.initials_list(), ["V", "J", "J"], hn)
def test_initials_with_prefix(self):
hn = HumanName("Alex van Johnson")
self.m(hn.initials_list(), ["A", "J"], hn)
def test_constructor_first(self):
hn = HumanName(first="TheName")
self.assertFalse(hn.unparsable)
self.m(hn.first, "TheName", hn)
def test_constructor_middle(self):
hn = HumanName(middle="TheName")
self.assertFalse(hn.unparsable)
self.m(hn.middle, "TheName", hn)
def test_constructor_last(self):
hn = HumanName(last="TheName")
self.assertFalse(hn.unparsable)
self.m(hn.last, "TheName", hn)
def test_constructor_title(self):
hn = HumanName(title="TheName")
self.assertFalse(hn.unparsable)
self.m(hn.title, "TheName", hn)
def test_constructor_suffix(self):
hn = HumanName(suffix="TheName")
self.assertFalse(hn.unparsable)
self.m(hn.suffix, "TheName", hn)
def test_constructor_nickname(self):
hn = HumanName(nickname="TheName")
self.assertFalse(hn.unparsable)
self.m(hn.nickname, "TheName", hn)
def test_constructor_multiple(self):
hn = HumanName(first="TheName", last="lastname", title="mytitle", full_name="donotparse")
self.assertFalse(hn.unparsable)
self.m(hn.first, "TheName", hn)
self.m(hn.last, "lastname", hn)
self.m(hn.title, "mytitle", hn)
TEST_NAMES = (
"John Doe",
"John Doe, Jr.",
"John Doe III",
"Doe, John",
"Doe, John, Jr.",
"Doe, John III",
"John A. Doe",
"John A. Doe, Jr.",
"John A. Doe III",
"Doe, John A.",
"Doe, John A., Jr.",
"Doe, John A. III",
"John A. Kenneth Doe",
"John A. Kenneth Doe, Jr.",
"John A. Kenneth Doe III",
"Doe, John A. Kenneth",
"Doe, John A. Kenneth, Jr.",
"Doe, John A. Kenneth III",
"Dr. John Doe",
"Dr. John Doe, Jr.",
"Dr. John Doe III",
"Doe, Dr. John",
"Doe, Dr. John, Jr.",
"Doe, Dr. John III",
"Dr. John A. Doe",
"Dr. John A. Doe, Jr.",
"Dr. John A. Doe III",
"Doe, Dr. John A.",
"Doe, Dr. John A. Jr.",
"Doe, Dr. John A. III",
"Dr. John A. Kenneth Doe",
"Dr. John A. Kenneth Doe, Jr.",
"Dr. John A. Kenneth Doe III",
"Doe, Dr. John A. Kenneth",
"Doe, Dr. John A. Kenneth Jr.",
"Doe, Dr. John A. Kenneth III",
"Juan de la Vega",
"Juan de la Vega, Jr.",
"Juan de la Vega III",
"de la Vega, Juan",
"de la Vega, Juan, Jr.",
"de la Vega, Juan III",
"Juan Velasquez y Garcia",
"Juan Velasquez y Garcia, Jr.",
"Juan Velasquez y Garcia III",
"Velasquez y Garcia, Juan",
"Velasquez y Garcia, Juan, Jr.",
"Velasquez y Garcia, Juan III",
"Dr. Juan de la Vega",
"Dr. Juan de la Vega, Jr.",
"Dr. Juan de la Vega III",
"de la Vega, Dr. Juan",
"de la Vega, Dr. Juan, Jr.",
"de la Vega, Dr. Juan III",
"Dr. Juan Velasquez y Garcia",
"Dr. Juan Velasquez y Garcia, Jr.",
"Dr. Juan Velasquez y Garcia III",
"Velasquez y Garcia, Dr. Juan",
"Velasquez y Garcia, Dr. Juan, Jr.",
"Velasquez y Garcia, Dr. Juan III",
"Juan Q. de la Vega",
"Juan Q. de la Vega, Jr.",
"Juan Q. de la Vega III",
"de la Vega, Juan Q.",
"de la Vega, Juan Q., Jr.",
"de la Vega, Juan Q. III",
"Juan Q. Velasquez y Garcia",
"Juan Q. Velasquez y Garcia, Jr.",
"Juan Q. Velasquez y Garcia III",
"Velasquez y Garcia, Juan Q.",
"Velasquez y Garcia, Juan Q., Jr.",
"Velasquez y Garcia, Juan Q. III",
"Dr. Juan Q. de la Vega",
"Dr. Juan Q. de la Vega, Jr.",
"Dr. Juan Q. de la Vega III",
"de la Vega, Dr. Juan Q.",
"de la Vega, Dr. Juan Q., Jr.",
"de la Vega, Dr. Juan Q. III",
"Dr. Juan Q. Velasquez y Garcia",
"Dr. Juan Q. Velasquez y Garcia, Jr.",
"Dr. Juan Q. Velasquez y Garcia III",
"Velasquez y Garcia, Dr. Juan Q.",
"Velasquez y Garcia, Dr. Juan Q., Jr.",
"Velasquez y Garcia, Dr. Juan Q. III",
"Juan Q. Xavier de la Vega",
"Juan Q. Xavier de la Vega, Jr.",
"Juan Q. Xavier de la Vega III",
"de la Vega, Juan Q. Xavier",
"de la Vega, Juan Q. Xavier, Jr.",
"de la Vega, Juan Q. Xavier III",
"Juan Q. Xavier Velasquez y Garcia",
"Juan Q. Xavier Velasquez y Garcia, Jr.",
"Juan Q. Xavier Velasquez y Garcia III",
"Velasquez y Garcia, Juan Q. Xavier",
"Velasquez y Garcia, Juan Q. Xavier, Jr.",
"Velasquez y Garcia, Juan Q. Xavier III",
"Dr. Juan Q. Xavier de la Vega",
"Dr. Juan Q. Xavier de la Vega, Jr.",
"Dr. Juan Q. Xavier de la Vega III",
"de la Vega, Dr. Juan Q. Xavier",
"de la Vega, Dr. Juan Q. Xavier, Jr.",
"de la Vega, Dr. Juan Q. Xavier III",
"Dr. Juan Q. Xavier Velasquez y Garcia",
"Dr. Juan Q. Xavier Velasquez y Garcia, Jr.",
"Dr. Juan Q. Xavier Velasquez y Garcia III",
"Velasquez y Garcia, Dr. Juan Q. Xavier",
"Velasquez y Garcia, Dr. Juan Q. Xavier, Jr.",
"Velasquez y Garcia, Dr. Juan Q. Xavier III",
"John Doe, CLU, CFP, LUTC",
"John P. Doe, CLU, CFP, LUTC",
"Dr. John P. Doe-Ray, CLU, CFP, LUTC",
"Doe-Ray, Dr. John P., CLU, CFP, LUTC",
"Hon. Barrington P. Doe-Ray, Jr.",
"Doe-Ray, Hon. Barrington P. Jr.",
"Doe-Ray, Hon. Barrington P. Jr., CFP, LUTC",
"Jose Aznar y Lopez",
"John E Smith",
"John e Smith",
"John and Jane Smith",
"Rev. John A. Kenneth Doe",
"Donovan McNabb-Smith",
"Rev John A. Kenneth Doe",
"Doe, Rev. John A. Jr.",
"Buca di Beppo",
"Lt. Gen. John A. Kenneth Doe, Jr.",
"Doe, Lt. Gen. John A. Kenneth IV",
"Lt. Gen. John A. Kenneth Doe IV",
'Mr. and Mrs. John Smith',
'John Jones (Google Docs)',
'john e jones',
'john e jones, III',
'jones, john e',
'E.T. Smith',
'E.T. Smith, II',
'Smith, E.T., Jr.',
'A.B. Vajpayee',
'Rt. Hon. Paul E. Mary',
'Maid Marion',
'Amy E. Maid',
'Jane Doctor',
'Doctor, Jane E.',
'dr. ben alex johnson III',
'Lord of the Universe and Supreme King of the World Lisa Simpson',
'Benjamin (Ben) Franklin',
'Benjamin "Ben" Franklin',
"Brian O'connor",
"Sir Gerald",
"Magistrate Judge John F. Forster, Jr",
# "Magistrate Judge Joaquin V.E. Manibusan, Jr", Intials seem to mess this up
"Magistrate-Judge Elizabeth Todd Campbell",
"Mag-Judge Harwell G Davis, III",
"Mag. Judge Byron G. Cudmore",
"Chief Judge J. Leon Holmes",
"Chief Judge Sharon Lovelace Blackburn",
"Judge James M. Moody",
"Judge G. Thomas Eisele",
# "Judge Callie V. S. Granade",
"Judge C Lynwood Smith, Jr",
"Senior Judge Charles R. Butler, Jr",
"Senior Judge Harold D. Vietor",
"Senior Judge Virgil Pittman",
"Honorable Terry F. Moorer",
"Honorable W. Harold Albritton, III",
"Honorable Judge W. Harold Albritton, III",
"Honorable Judge Terry F. Moorer",
"Honorable Judge Susan Russ Walker",
"Hon. Marian W. Payson",
"Hon. Charles J. Siragusa",
"US Magistrate Judge T Michael Putnam",
"Designated Judge David A. Ezra",
"Sr US District Judge Richard G Kopf",
"U.S. District Judge Marc Thomas Treadwell",
"Dra. Andréia da Silva",
"Srta. Andréia da Silva",
)
class HumanNameVariationTests(HumanNameTestBase):
# test automated variations of names in TEST_NAMES.
# Helps test that the 3 code trees work the same
TEST_NAMES = TEST_NAMES
def test_variations_of_TEST_NAMES(self):
for name in self.TEST_NAMES:
hn = HumanName(name)
if len(hn.suffix_list) > 1:
hn = HumanName("{title} {first} {middle} {last} {suffix}".format(**hn.as_dict()).split(',')[0])
hn.C.empty_attribute_default = '' # format strings below require empty string
hn_dict = hn.as_dict()
attrs = [
'title',
'first',
'middle',
'last',
'suffix',
'nickname',
]
nocomma = HumanName("{title} {first} {middle} {last} {suffix}".format(**hn_dict))
lastnamecomma = HumanName("{last}, {title} {first} {middle} {suffix}".format(**hn_dict))
if hn.suffix:
suffixcomma = HumanName("{title} {first} {middle} {last}, {suffix}".format(**hn_dict))
if hn.nickname:
nocomma = HumanName("{title} {first} {middle} {last} {suffix} ({nickname})".format(**hn_dict))
lastnamecomma = HumanName("{last}, {title} {first} {middle} {suffix} ({nickname})".format(**hn_dict))
if hn.suffix:
suffixcomma = HumanName("{title} {first} {middle} {last}, {suffix} ({nickname})".format(**hn_dict))
for attr in hn._members:
self.m(getattr(hn, attr), getattr(nocomma, attr), hn)
self.m(getattr(hn, attr), getattr(lastnamecomma, attr), hn)
if hn.suffix:
self.m(getattr(hn, attr), getattr(suffixcomma, attr), hn)
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
log.setLevel(logging.ERROR)
log.addHandler(logging.StreamHandler())
name_string = sys.argv[1]
hn_instance = HumanName(name_string, encoding=sys.stdout.encoding)
print((repr(hn_instance)))
hn_instance.capitalize()
print((repr(hn_instance)))
print("Initials: " + hn_instance.initials())
else:
print("-"*80)
print("Running tests")
unittest.main(exit=False)
print("-"*80)
print("Running tests with empty_attribute_default = None")
from nameparser.config import CONSTANTS
CONSTANTS.empty_attribute_default = None
unittest.main()
|