Skip to content

translations

translations

TranslationDictionaryPaths dataclass

Paths to the static two-column translation-dictionary spreadsheets build needs, one per A3Field that requires an A3→iMarina translation.

Source code in source/src/imarina_load_researchers/core/translations.py
@dataclass
class TranslationDictionaryPaths:
    """Paths to the static two-column translation-dictionary spreadsheets
    `build` needs, one per `A3Field` that requires an A3→iMarina translation."""

    countries_path: Path
    jobs_path: Path
    personal_web_path: Path
    unit_group_path: Path
    entity_type_path: Path
    job_description_entity_path: Path
    sex_path: Path

    def to_a3_field_map(self) -> dict[A3Field, Path]:
        """
        Maps each translated `A3Field` to its dictionary spreadsheet's path.

        Returns:
            dict[A3Field, Path]: The `{A3Field: dictionary path}` mapping.
        """
        return {
            A3Field.COUNTRY: self.countries_path,
            A3Field.JOB_DESCRIPTION: self.jobs_path,
            A3Field.PERSONAL_WEB: self.personal_web_path,
            A3Field.UNIT_GROUP: self.unit_group_path,
            A3Field.ENTITY_TYPE: self.entity_type_path,
            A3Field.JOB_DESCRIPTION_ENTITY: self.job_description_entity_path,
            A3Field.SEX: self.sex_path,
        }

to_a3_field_map

to_a3_field_map() -> dict[A3Field, Path]

Maps each translated A3Field to its dictionary spreadsheet's path.

Returns:

Type Description
dict[A3Field, Path]

dict[A3Field, Path]: The {A3Field: dictionary path} mapping.

Source code in source/src/imarina_load_researchers/core/translations.py
def to_a3_field_map(self) -> dict[A3Field, Path]:
    """
    Maps each translated `A3Field` to its dictionary spreadsheet's path.

    Returns:
        dict[A3Field, Path]: The `{A3Field: dictionary path}` mapping.
    """
    return {
        A3Field.COUNTRY: self.countries_path,
        A3Field.JOB_DESCRIPTION: self.jobs_path,
        A3Field.PERSONAL_WEB: self.personal_web_path,
        A3Field.UNIT_GROUP: self.unit_group_path,
        A3Field.ENTITY_TYPE: self.entity_type_path,
        A3Field.JOB_DESCRIPTION_ENTITY: self.job_description_entity_path,
        A3Field.SEX: self.sex_path,
    }

build_translations

build_translations(paths: TranslationDictionaryPaths) -> Translator

Loads every A3→iMarina translation dictionary into one Translator.

Parameters:

Name Type Description Default
paths TranslationDictionaryPaths

Paths to the dictionary spreadsheets to load.

required

Returns:

Name Type Description
Translator Translator

The {A3Field: {raw_value: translated_value}} dictionaries, with A3Field.COUNTRY pre-normalized (via normalize_country_name) so a3_mapper's per-row lookups don't have to re-normalize it on every researcher row.

Source code in source/src/imarina_load_researchers/core/translations.py
def build_translations(
    paths: TranslationDictionaryPaths,
) -> Translator:
    """
    Loads every A3→iMarina translation dictionary into one `Translator`.

    Args:
        paths (TranslationDictionaryPaths): Paths to the dictionary
            spreadsheets to load.

    Returns:
        Translator: The `{A3Field: {raw_value: translated_value}}`
            dictionaries, with `A3Field.COUNTRY` pre-normalized (via
            `normalize_country_name`) so `a3_mapper`'s per-row lookups don't
            have to re-normalize it on every researcher row.
    """
    translations = {
        field: dict(build_translator(path, 1))
        for field, path in paths.to_a3_field_map().items()
    }
    # Normalized once here so a3_mapper's per-row country lookups don't have to
    # rebuild this from the raw dict on every researcher row.
    translations[A3Field.COUNTRY] = {
        normalize_country_name(k): v.strip()
        for k, v in translations[A3Field.COUNTRY].items()
    }
    return translations

build_translator

build_translator(path: Path, skiprows: int = 0) -> dict[str, str]

Loads a two-column dictionary spreadsheet into a {raw: translated} dict.

Parameters:

Name Type Description Default
path Path

Path to the two-column dictionary spreadsheet.

required
skiprows int

Number of leading rows to skip (e.g. a header row).

0

Returns:

Type Description
dict[str, str]

dict[str, str]: The first column mapped to the second, as a dict.

Source code in source/src/imarina_load_researchers/core/translations.py
def build_translator(path: Path, skiprows: int = 0) -> dict[str, str]:
    """
    Loads a two-column dictionary spreadsheet into a `{raw: translated}` dict.

    Args:
        path (Path): Path to the two-column dictionary spreadsheet.
        skiprows (int): Number of leading rows to skip (e.g. a header row).

    Returns:
        dict[str, str]: The first column mapped to the second, as a dict.
    """
    excel = Excel(path, skiprows, None)
    return excel.parse_two_columns(0, 1)