Document utilities

This commit is contained in:
Andras Schmelczer 2022-07-10 19:37:45 +02:00
parent 2b378114aa
commit 00568ca6d3
No known key found for this signature in database
GPG key ID: 0EA1BC97D0AB076E
21 changed files with 371 additions and 29 deletions

View file

@ -1,3 +0,0 @@
from .english_name_of_language import english_name_of_language
from .is_english import is_english
from .predict_language import predict_language

View file

@ -4,6 +4,26 @@ from langcodes import Language
def english_name_of_language(language_code: Optional[str]) -> str:
"""Human-friendly English name of language from its `language_code`.
A thin wrapper over [langcodes](https://github.com/rspeer/langcodes) for convenient
language tagging.
Examples:
>>> english_name_of_language('en-US')
'English (United States)'
>>> english_name_of_language('und')
'Unknown language'
Args:
language_code: Language code, for example, returned by
[great_ai.utilities.predict_language][].
Returns:
English name of language.
"""
if not language_code:
language_code = "und"

View file

@ -4,6 +4,28 @@ from langcodes import standardize_tag, tag_distance
def is_english(language_code: Optional[str]) -> bool:
"""Decide whether the `language_code` is of an English language.
A thin wrapper over [langcodes](https://github.com/rspeer/langcodes) for convenient
language tagging.
Examples:
>>> is_english('en-US')
True
>>> is_english(None)
False
>>> is_english('und')
False
Args:
language_code: Language code, for example, returned by
`[great_ai.utilities.predict_language][].
Returns:
Boolean indicating whether it's English.
"""
if not language_code:
language_code = "und"

View file

@ -5,6 +5,23 @@ from langdetect import LangDetectException, detect
def predict_language(text: Optional[str]) -> str:
"""Predict the language code from text.
A thin wrapper over [langcodes](https://github.com/rspeer/langcodes) for convenient
language tagging.
Examples:
>>> predict_language('This is a sentence.')
'en'
Args:
text: Text used for prediction.
Returns:
The predicted language code (en, en-US) or `und` if a prediction could not be
made.
"""
if not text:
return Language.make().to_tag()