File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -207,14 +207,12 @@ def get_country_code(country_name):
207207 if country_name in COUNTRY_CODES .values ():
208208 return country_name
209209
210- codes = difflib .get_close_matches (
211- country_name , COUNTRY_CODES .keys (), 1 )
212- if codes :
213- return COUNTRY_CODES [codes [0 ]]
210+ matches = difflib .get_close_matches (country_name , COUNTRY_CODES , 1 )
211+ if matches :
212+ return COUNTRY_CODES [matches [0 ]]
214213
215- codes = difflib .get_close_matches (
216- country_name , LANGUAGE_TO_COUNTRY .keys (), 1 )
217- if codes :
218- return LANGUAGE_TO_COUNTRY [codes [0 ]]
214+ matches = difflib .get_close_matches (country_name , LANGUAGE_TO_COUNTRY , 1 )
215+ if matches :
216+ return LANGUAGE_TO_COUNTRY [matches [0 ]]
219217
220218 raise KeyError (country_name )
Original file line number Diff line number Diff line change 66import warnings
77
88import language_tool
9+ from language_tool .country_codes import get_country_code
910
1011
1112class TestLanguageTool (unittest .TestCase ):
@@ -51,6 +52,30 @@ class TestLanguageTool(unittest.TestCase):
5152 ],
5253 }
5354
55+ country_tests = {
56+ "Australian" : "AU" ,
57+ "Canadian" : "CA" ,
58+ "GB" : "GB" ,
59+ "New Zealand" : "NZ" ,
60+ "South African" : "ZA" ,
61+ "US" : "US" ,
62+ "Austria" : "AT" ,
63+ "Germany" : "DE" ,
64+ "Swiss" : "CH" ,
65+ "Brazil" : "BR" ,
66+ "Portugal" : "PT" ,
67+
68+ "Australia" : "AU" ,
69+ "Canada" : "CA" ,
70+ "United Kingdom" : "GB" ,
71+ "South Africa" : "ZA" ,
72+ "United States" : "US" ,
73+ "Switzerland" : "CH" ,
74+
75+ "Austrian" : "AT" ,
76+ "Brazilian" : "BR" ,
77+ }
78+
5479 def test_samples (self ):
5580 languages = language_tool .get_languages ()
5681 for language , tests in self .tests .items ():
@@ -84,6 +109,10 @@ def test_version(self):
84109 version = language_tool .get_version ()
85110 self .assertTrue (version )
86111
112+ def test_country_codes (self ):
113+ for country , code in self .country_tests .items ():
114+ self .assertEqual (get_country_code (country ), code )
115+
87116
88117if __name__ == "__main__" :
89118 unittest .main ()
You can’t perform that action at this time.
0 commit comments