Skip to content

More unified language support - #19

Merged
rgaudin merged 10 commits into
masterfrom
language_code_unification
May 21, 2020
Merged

rgaudin merged 10 commits into
masterfrom
language_code_unification

Conversation

@satyamtg

@satyamtg satyamtg commented May 15, 2020 •

Copy link
Copy Markdown
Contributor

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 -

  • hi
  • hin
  • Hindi

The returned details are in the form of a dictionary similar to the one below -

{
                "iso-639-1": "zh",
                "iso-639-2b": "chi",
                "iso-639-2t": "zho",
                "iso-639-3": "zho",
                "iso-639-5": "",
                "english": "Chinese",
                "iso_types": ["part1"],
                "querytype": "locale",
                "query": "zh-Hans",
}

The iso_types holds a list of ISO639 collections to which the query part matches

This breaks the current API

@satyamtg satyamtg self-assigned this May 15, 2020
@satyamtg
satyamtg requested review from dattaz and rgaudin May 15, 2020 22:28

@rgaudin rgaudin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?)

Comment thread src/zimscraperlib/i18n.py Resolved
@codecov

codecov Bot commented May 19, 2020 •

Copy link
Copy Markdown

Codecov Report

Merging #19 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master       #19   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           13        13           
  Lines          389       445   +56     
=========================================
+ Hits           389       445   +56     
Impacted Files Coverage Δ
src/zimscraperlib/zim.py 100.00% <ø> (ø)
src/zimscraperlib/i18n.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 900a983...c23c714. Read the comment docs.

@satyamtg

Copy link
Copy Markdown
Contributor Author

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.

Have implemented this. This should now work fine.

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…

I did this due to basically 2 things -

  1. The iso-639 library doesn't have native languages. The other one that we previously used i.e. iso639 has them in a languages_utf-8.txt file and that too doesn't have that for all languages listed there. It only has support for iso-639-2 languages.

  2. Both the dependencies have the same package name. So, they are not compatible with each other and pip would rewrite the one previously installed. Also, iso639 does everything in __init__.py and that's not good.

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 ?

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?)

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.

@satyamtg
satyamtg requested a review from rgaudin May 19, 2020 09:25
@rgaudin

rgaudin commented May 19, 2020

Copy link
Copy Markdown
Member

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 ?

I think we can find this elsewhere. I'd rather we don't have to do it ourselves. pycountry came to my mind but doesn't does this. We can search for another dep.
I stumbled upon https://github.com/umpirsky/language-list which have some interesting info but it's a slightly different usage.

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?)

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.

OK, agrees.

@satyamtg

Copy link
Copy Markdown
Contributor Author

I stumbled upon https://github.com/umpirsky/language-list which have some interesting info but it's a slightly different usage.

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.

@rgaudin

rgaudin commented May 20, 2020 •

Copy link
Copy Markdown
Member

I think we should go with babel. It seems quite complete.

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 (arq fails but ar_MA works).
For missing values, if it has a macro, we'd get the one of the macro and if it's not, we'd use the English string.

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]: 'العربية (المغرب)'

@satyamtg

satyamtg commented May 20, 2020 •

Copy link
Copy Markdown
Contributor Author

I think we should go with babel. It seems quite complete.

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 (arq fails but ar_MA works).
For missing values, if it has a macro, we'd get the one of the macro and if it's not, we'd use the English string.

Yup. Babel is in-fact the best solution we can have for this. Thanks for pointing that out. Implemented the native language part + tests

satyamtg and others added 5 commits May 20, 2020 20:11
- 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
@rgaudin
rgaudin merged commit 0ae7d75 into master May 21, 2020
@rgaudin
rgaudin deleted the language_code_unification branch May 21, 2020 16:19
This was referenced Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better support for different kinds of language codes

2 participants