Skip to content

Commit

Permalink
fix:standardize_lang (#283)
Browse files Browse the repository at this point in the history
* fix:standardize_lang

* fix:standardize_lang
  • Loading branch information
JarbasAl authored Oct 16, 2024
1 parent 924c2f2 commit 38c2bdf
Showing 1 changed file with 11 additions and 7 deletions.
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

0 comments on commit 38c2bdf

Please sign in to comment.