diff --git a/audformat/core/utils.py b/audformat/core/utils.py index 1b6814c0..da113188 100644 --- a/audformat/core/utils.py +++ b/audformat/core/utils.py @@ -8,6 +8,7 @@ import typing as typing import iso639 +from iso639.exceptions import InvalidLanguageValue import iso3166 import numpy as np import pandas as pd @@ -1288,32 +1289,13 @@ def map_language(language: str) -> str: 'eng' """ - result = None - - if len(language) == 2: - try: - result = iso639.languages.get(alpha2=language.lower()) - except KeyError: - pass - elif len(language) == 3: - try: - result = iso639.languages.get(part3=language.lower()) - except KeyError: - pass - else: - try: - result = iso639.languages.get(name=language.title()) - except KeyError: - pass - - if result is not None: - result = result.part3 - - if not result: + try: + return iso639.Lang( + language.title() if len(language) > 3 else language.lower() + ).pt3 + except InvalidLanguageValue: raise ValueError(f"'{language}' is not supported by ISO 639-3.") - return result - def read_csv( *args, diff --git a/pyproject.toml b/pyproject.toml index 37637e36..c699f2c5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,7 @@ requires-python = '>=3.9' # pandas >=2.1.0 dependencies = [ 'audeer >=2.0.0', 'audiofile >=0.4.0', - 'iso-639', + 'iso639-lang', 'iso3166', 'oyaml', 'pandas >=2.1.0', # for pyarrow -> timedelta conversion