Skip to content

Commit

Permalink
update feature name lookups to support english names to translated fe…
Browse files Browse the repository at this point in the history
…atures
  • Loading branch information
adamsachs committed Jan 25, 2024
1 parent 6cb2313 commit 214b68c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/fideslang/gvl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
)
GVL_FEATURES: Dict[int, Dict[str, Feature]] = defaultdict(dict)
GVL_SPECIAL_FEATURES: Dict[int, Dict[str, Feature]] = defaultdict(dict)
FEATURES_BY_NAME: Dict[str, Feature] = {}
FEATURES_BY_NAME: Dict[str, Dict[str, Feature]] = defaultdict(dict)


### Data Categories
Expand Down Expand Up @@ -73,6 +73,23 @@ def _load_data() -> None:
special_purpose = MappedPurpose.parse_obj(raw_special_purpose)
special_purpose_to_data_uses[special_purpose.id] = special_purpose.data_uses

feature_id_to_english_name = {}
special_feature_id_to_english_name = {}
with open(
os.path.join(os.curdir, FEATURE_MAPPING_FILE), encoding="utf-8"
) as mapping_file:
data = load(mapping_file)
# first load feature ID -> feature english name map based on feature mapping file
for raw_feature in data["features"].values():
feature = Feature.parse_obj(raw_feature)
feature_id_to_english_name[feature.id] = feature.name

for raw_special_feature in data["specialFeatures"].values():
special_feature = Feature.parse_obj(raw_special_feature)
special_feature_id_to_english_name[
special_feature.id
] = special_feature.name

gvl_category_to_fides_categories = {}
with open(
os.path.join(os.curdir, DATA_CATEGORY_MAPPING_FILE), encoding="utf-8"
Expand Down Expand Up @@ -121,15 +138,15 @@ def _load_data() -> None:
for language, translation in translation_dict.items():
feature = Feature.parse_obj(translation)
GVL_FEATURES[feature.id][language] = feature
if language == DEFAULT_LANGUAGE_ID:
FEATURES_BY_NAME[feature.name] = feature
english_name = feature_id_to_english_name[feature.id]
FEATURES_BY_NAME[english_name][language] = feature

for translation_dict in data["specialFeatures"].values():
for language, translation in translation_dict.items():
special_feature = Feature.parse_obj(translation)
GVL_SPECIAL_FEATURES[special_feature.id][language] = special_feature
if language == DEFAULT_LANGUAGE_ID:
FEATURES_BY_NAME[special_feature.name] = special_feature
english_name = special_feature_id_to_english_name[special_feature.id]
FEATURES_BY_NAME[english_name][language] = special_feature

for translation_dict in data["dataCategories"].values():
for language, translation in translation_dict.items():
Expand Down Expand Up @@ -174,9 +191,11 @@ def data_use_to_purpose(
return MAPPED_PURPOSES_BY_DATA_USE.get(data_use, {}).get(language, None)


def feature_name_to_feature(feature_name: str) -> Optional[Feature]:
"""Utility function to return a GVL feature (or special feature) given the feature's name"""
return FEATURES_BY_NAME.get(feature_name, None)
def feature_name_to_feature(
feature_name: str, language: str = DEFAULT_LANGUAGE_ID
) -> Optional[Feature]:
"""Utility function to return a GVL feature (or special feature) given the feature's english name."""
return FEATURES_BY_NAME.get(feature_name, {}).get(language, None)


def feature_id_to_feature_name(
Expand Down
18 changes: 18 additions & 0 deletions tests/fideslang/gvl/test_gvl.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,24 @@ def test_features():
def test_feature_name_to_feature():
assert feature_name_to_feature("Link different devices").id == 2
assert feature_name_to_feature("Use precise geolocation data").id == 1
english_special_feature_1 = feature_name_to_feature(
"Use precise geolocation data", "en"
)
assert english_special_feature_1.id == 1
assert english_special_feature_1.name == "Use precise geolocation data"
chinese_special_feature_1 = feature_name_to_feature(
"Use precise geolocation data", "zh"
)
assert chinese_special_feature_1.id == 1
assert chinese_special_feature_1.name == "使用精确的地理位置数据"
spanish_feature_2 = feature_name_to_feature("Link different devices", "es")
assert spanish_feature_2.id == 2
assert spanish_feature_2.name == "Vincular diferentes dispositivos"
assert (
spanish_feature_2.description
== "En apoyo de las finalidades que se explican en este documento, tú dispositivo podría considerarse como vinculado a otros dispositivos que te pertenezcan o sean de tu hogar(por ejemplo, porque ha iniciado sesión en el mismo servicio tanto desde tu teléfono como desde tu ordenador, o porque podrías haber utilizado la misma conexión a Internet en ambos dispositivos)."
)

assert feature_name_to_feature("Name doesn't exist") is None


Expand Down

0 comments on commit 214b68c

Please sign in to comment.