Repository navigation
More unified language support - #19
Conversation
rgaudin
left a comment
There was a problem hiding this comment.
Thank you, this is already a great improvement but my understanding was that we'd have a slightly smarter system.
First, you broke the API so it's important to mention it clearly in the PR cause we'll need to bump the minor release version.
Next, I don't understand why we lost the native language name. I think it's a very important information. We should add another dep. to fetch it if iso-639 doesn't provide it.
It's too bad because it bundles the info in a languages_utf-8.txt file it doesn't use…
now the implementation is good to get a result but we need to go the extra mile and provide as much as possible in order to not loose info.
we should look for the macro of a language and if it's there (it's a sub lang of a macro, like most chineses and arabics) fill the other info like 639-1 and such via the macro.
# french is fullfiled
get_language_details("fra")
{'iso-639-1': 'fr',
'iso-639-2b': 'fre',
'iso-639-2t': 'fra',
'iso-639-3': 'fra',
'iso-639-5': '',
'english': 'French',
'iso_types': ['part2t', 'part3'],
'querytype': 'purecode',
'query': 'fra'}
# Madarin is not
get_language_details("cmn")
{'iso-639-1': '',
'iso-639-2b': '',
'iso-639-2t': '',
'iso-639-3': 'cmn',
'iso-639-5': '',
'english': 'Mandarin Chinese',
'iso_types': ['part3'],
'querytype': 'purecode',
'query': 'cmn'}
# but if you query its macro
get_language_details("zho")
{'iso-639-1': 'zh',
'iso-639-2b': 'chi',
'iso-639-2t': 'zho',
'iso-639-3': 'zho',
'iso-639-5': '',
'english': 'Chinese',
'iso_types': ['part2t', 'part3'],
'querytype': 'purecode',
'query': 'zho'}I think we should fill missing info from cmn using it's macro's info.
Do we want to use und when we can't parse the source? I'm all for explicit fails it might make sense in some contexts (via a failsafe arg?)
Codecov Report
@@ Coverage Diff @@
## master #19 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 13 13
Lines 389 445 +56
=========================================
+ Hits 389 445 +56
Continue to review full report at Codecov.
|
Have implemented this. This should now work fine.
I did this due to basically 2 things -
So, IMO, what we should do is to implement our own list of native languages in this scraperlib as its important info (and we would be able to add other languages too. What do you say @rgaudin ?
It returns None if we don't get a result. IMO it's good to fail if we cannot parse the source query. It may be useful with a failsafe arg though. Will add this in next commit. |
I think we can find this elsewhere. I'd rather we don't have to do it ourselves.
OK, agrees. |
I see that this has a list of 609 languages (with both 2-lettered and 3-lettered codes) in 564 locales. However, all 609 languages are not named in all 564 locales. I also came across this from Mozilla that has over 200 languages named in native script (with locales) which would be definitely easier to extract as that's a valid JSON which is returned. |
|
I think we should go with It works with locale names and ISO codes but not all of them. For example, the codes for the subs of arabic macro don't work… but the country locales do ( In [2]: from babel import Locale
In [3]: Locale.parse("zh_TW").get_display_name()
Out[3]: '中文 (繁體, 台灣)'
In [4]: Locale.parse("fr").get_display_name()
Out[4]: 'français'
In [5]: Locale.parse("fra").get_display_name()
Out[5]: 'français (France)'
In [6]: Locale.parse("bam").get_display_name()
Out[6]: 'bamanakan (Mali)'
In [7]: Locale.parse("bm").get_display_name()
Out[7]: 'bamanakan'
In [8]: Locale.parse("ara").get_display_name()
Out[8]: 'العربية (مصر)'
In [9]: Locale.parse("ar").get_display_name()
Out[9]: 'العربية'
In [10]: Locale.parse("cmn").get_display_name()
Out[10]: '中文 (简体, 中国)'
In [11]: Locale.parse("arq").get_display_name()
---------------------------------------------------------------------------
UnknownLocaleError Traceback (most recent call last)
<ipython-input-11-2464e6e4c637> in <module>
----> 1 Locale.parse("arq").get_display_name()
~/src/envs/zimscraperlib/lib/python3.8/site-packages/babel/core.py in parse(cls, identifier, sep, resolve_likely_subtags)
329 return locale
330
--> 331 raise UnknownLocaleError(input_id)
332
333 def __eq__(self, other):
UnknownLocaleError: unknown locale 'arq'
In [12]: Locale.parse("arb").get_display_name()
Out[12]: 'العربية (مصر)'
In [13]: Locale.parse("ary").get_display_name()
---------------------------------------------------------------------------
UnknownLocaleError Traceback (most recent call last)
<ipython-input-13-de5bd4e24796> in <module>
----> 1 Locale.parse("ary").get_display_name()
~/src/envs/zimscraperlib/lib/python3.8/site-packages/babel/core.py in parse(cls, identifier, sep, resolve_likely_subtags)
329 return locale
330
--> 331 raise UnknownLocaleError(input_id)
332
333 def __eq__(self, other):
UnknownLocaleError: unknown locale 'ary'
In [14]: Locale.parse("ar-MA").get_display_name()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-14-39398b209e9b> in <module>
----> 1 Locale.parse("ar-MA").get_display_name()
~/src/envs/zimscraperlib/lib/python3.8/site-packages/babel/core.py in parse(cls, identifier, sep, resolve_likely_subtags)
266 raise TypeError('Unexpected value for identifier: %r' % (identifier,))
267
--> 268 parts = parse_locale(identifier, sep=sep)
269 input_id = get_locale_identifier(parts)
270
~/src/envs/zimscraperlib/lib/python3.8/site-packages/babel/core.py in parse_locale(identifier, sep)
1092 lang = parts.pop(0).lower()
1093 if not lang.isalpha():
-> 1094 raise ValueError('expected only letters, got %r' % lang)
1095
1096 script = territory = variant = None
ValueError: expected only letters, got 'ar-ma'
In [15]: Locale.parse("ar_MA").get_display_name()
Out[15]: 'العربية (المغرب)' |
Yup. Babel is in-fact the best solution we can have for this. Thanks for pointing that out. Implemented the native language part + tests |
- fixed imports ordering - changed iso639's import local name to avoid confusion - added a global `ISO_LEVELS` var to avoid duplicating `part` and `iso-639-` strings - added custom NotFound exception - exposed all sub-functions of get_language_details - fixed docstrings - refactored to simplify code: - find_native now uses only query and optionnaly lang_data - combine_lang_and_macro_data was renamed and simplifieg (using inspection) - get_iso_lang_data was simplified using ISO_LEVELS - get_language_details was refactored to share what can be between conditions - added `failsafe` param to get_language_details
babel's english names seems of better quality than those of iso639 use babel's version whenever it's present updated tests according to new (babel's) expected values
Fixes #18 by using iso-639 to basically return language details if available for any type of input. For example, the following inputs would give details for Hindi language -
The returned details are in the form of a dictionary similar to the one below -
The iso_types holds a list of ISO639 collections to which the query part matches
This breaks the current API