Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:standardize_lang #283

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions ovos_utils/lang/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from os import listdir
from os.path import isdir, join
from langcodes import tag_distance, standardize_tag as std
from typing import Optional

from langcodes import tag_distance, standardize_tag as std

from ovos_utils.file_utils import resolve_resource_file


def standardize_lang_tag(lang_code, macro=True):
def standardize_lang_tag(lang_code: str, macro=True) -> str:
"""https://langcodes-hickford.readthedocs.io/en/sphinx/index.html"""
try:
return std(lang_code, macro=macro)
return str(std(lang_code, macro=macro))
except:
if macro:
return lang_code.split("-")[0].lower()
Expand All @@ -17,7 +20,7 @@ def standardize_lang_tag(lang_code, macro=True):
return lang_code.lower()


def get_language_dir(base_path, lang="en-US"):
def get_language_dir(base_path: str, lang: str ="en-US") -> Optional[str]:
""" checks for all language variations and returns best path """
lang = standardize_lang_tag(lang)

Expand All @@ -33,11 +36,12 @@ def get_language_dir(base_path, lang="en-US"):
# 1- 3 -> These codes indicate a minor regional difference.
# 4 - 10 -> These codes indicate a significant but unproblematic regional difference.
if score < 10:
candidates.append((f, score))

candidates.append((f"{base_path}/{f}", score))
if not candidates:
return None
# sort by distance to target lang code
candidates = sorted(candidates, key=lambda k: k[1])
return candidates[0]
return candidates[0][0]


def translate_word(name, lang='en-US'):
Expand Down
Loading