Package: name2nat
name2nat: a Python package for nationality prediction from a namename2natis a Python package that predicts the nationality of any name written in Roman letters.
For example, it returns the correct outputKoreanfor my name `Kyubyong Park'.
Needless to say, it is not possible to guess somebody's nationality 100% right from their name.
After all, nationality can change, you know.
However, it is also true that there is a tendency between names and nationality.
So it turns out statistical classifiers for this task works to some extent.
Details are explained below.NaNa DatasetConstructionI constructed a new dataset for this project because I failed to find any available dataset that is big and comprehensive enough.STEP 1. Downloaded and extracted the 20200601 English wiki dump (enwiki-20200601-pages-articles.xml).STEP 2. Iterated all pages and collected the title and the nationality.
I regarded the title as a person if the Category section at the bottom of each page included ... births (green rectangule),
and identified their nationality from the most frequent nationality word in the section (red rectangules).* STEP 3. Randomly split the data into train/dev/test in the ratio of 8:1:1 within each nationality group.StatsNationalityTrainDevTestTotal 1112902890248111286111368Afghan7789798Albanian2193274275Algerian1592199200American2417723022130222Andorran1882424Angolan5046363Argentine892611161116Armenian1600200201Aruban931212Australian4053650675067Austrian919211491149Azerbaijani1331166167Bahamian2332930Bahraini2373030Bangladeshi1636204205Barbadian3724747Basque961120121Belarusian2338292293Belgian7907988989Belizean1481919Beninese1992525Bermudian2703434Bhutanese1441818Bolivian6578283Bosniak811011Botswana2523132Brazilian1123414041405Breton1181515British4592257405741Bruneian1151415Bulgarian3926491491Burkinabé2893637Burmese944118118Burundian1401718Cambodian3604546Cameroonian1028129129Canadian3415242694270Catalan1717215215Chadian1391718Chilean2838355355Chinese949411871187Colombian2620328328Comorian5477Congolese3545Cuban1938242243Cypriot1016127128Czech7244906906Dane3245Djiboutian5477Dominican1580198198Dutch1491618641865Ecuadorian874109110Egyptian2776347348Emirati6217878English7715996459645Equatoguinean1932425Eritrean1331717Estonian2028254254Ethiopian7339292Faroese2843536Filipino3928491491Finn6889French4084151055106Gabonese1802323Gambian2202828Georgian2623333German4238852995299Ghanaian2036255255Gibraltarian981213Greek5975747747Grenadian1391718Guatemalan5637071Guinean5847374Guyanese3584545Haitian5617071Honduran5006363Hungarian7220903903I-Kiribati4056Indian2269228362837Indonesian2820352353Iranian5010626627Iraqi1252157157Irish1184414811481Israeli5149644644Italian2933636673668Jamaican1422178178Japanese2121626522652Jordanian4906162Kazakh2434Kenyan1609201202Korean7896987988Kuwaiti3965050Kyrgyz1622Lao2634Latvian1693212212Lebanese1246156156Liberian2943737Libyan2713434Lithuanian1979247248Macedonian1099137138Malagasy2322929Malawian2192728Malaysian2582323323Maldivian1521920Malian3854849Maltese6638383Manx1501919Marshallese3244Mauritanian961212Mauritian2633333Mexican864810811081Moldovan1000125125Mongolian5046364Montenegrin955119120Moroccan1457182183Mozambican2102627Namibian5887474Nauruan3244Nepalese7739797Nicaraguan2853636Nigerian4060507508Nigerien1431818Norwegian1351216891690Omani1972525Pakistani3762470471Palauan3545Palestinian5286666Panamanian4745960Paraguayan1012127127Peruvian1521190191Portuguese4734592592Qatari5486869Romanian6551819819Russian2127426592660Rwandan2693434Salvadoran5076364Sammarinese1982525Samoan5967575Saudi1496187188Senegalese823103103Serb4466Singaporean1316165165Slovak2867358359Slovene881112Somali1161415Sotho4967Sudanese3484444Surinamese2002525Swazi1141415Syriac781010Syrian1047131131Taiwanese1946243244Tajik6188Tamil1399175175Tanzanian6277879Thai2747343344Tibetan2653334Togolese2112627Tongan4565757Tunisian1072134134Turk791010Tuvaluan6689Ugandan1052132132Ukrainian6198775775Uruguayan2267283284Uzbek6288Vanuatuan1161515Venezuelan1937242243Vietnamese1257157158Vincentian811Welsh5270659659Yemeni3224041Zambian5106464Downloadable LinkYou can download the datasethere.name2natInstallationpip install name2natUsage>>> from name2nat import Name2nat

>>> my_nanat = Name2nat()

>>> names = ["Donald Trump", # American
         "Moon Jae-in", # Korean
         "Shinzo Abe", # Japanese
         "Xi Jinping", # Chinese
         "Joko Widodo", # Indonesian
         "Angela Merkel", # German
         "Emmanuel Macron", # French
         "Kyubyong Park", # Korean
         "Yamamoto Yu", # Japanese
         "Jing Xu"] # Chinese
>>> result = my_nanat(names, top_n=3)
>>> print(result)
# (name, [(nationality, prob), ...])
# Note that prob of 1.0 indicates the name exists
# in Wikipedia.
[
('Donald Trump', [('American', 1.0)])
('Moon Jae-in', [('Korean', 1.0)])
('Shinzo Abe', [('Japanese', 1.0)])
('Xi Jinping', [('Chinese', 1.0)])
('Joko Widodo', [('Indonesian', 1.0)])
('Angela Merkel', [('German', 1.0)])
('Emmanuel Macron', [('French', 1.0)])
('Kyubyong Park', [('Korean', 0.9985014200210571), ('American', 0.000289416522718966), ('Bhutanese', 0.00025851925602182746)])
('Yamamoto Yu', [('Japanese', 0.7050493359565735), ('Taiwanese', 0.12779785692691803), ('Chinese', 0.04263153299689293)])
('Jing Xu', [('Chinese', 0.8626819252967834), ('Taiwanese', 0.09901007264852524), ('American', 0.022995812818408012)])
]TrainingI use a powerful NLP libraryFlairto train a text classifier model.
A bidirectional GRU layer is employed.python train.pyEvaluationpython predict.py;
python eval.py --gt nana/test.tgt --pred test.predResultsKPrecision@K161310/111368=55.1277480/111368=69.6386703/111368=77.9492491/111368=83.0596697/111368=86.8ReferencesIf you use this code for research, please cite:@misc{park2018name2nat,
  author = {Park, Kyubyong},
  title = {name2nat: a Python package for nationality prediction from a name},
  year = {2020},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/Kyubyong/name2nat}}
}