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: language order as we dump taxonomies #512

Merged
merged 4 commits into from
Jul 17, 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
15 changes: 13 additions & 2 deletions parser/openfoodfacts_taxonomy_parser/unparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,30 @@ def list_tags_lc(self, node):
for property in node:
if property.startswith(key):
lc_list.append(property.split("_", dash_before_lc)[dash_before_lc])
lc_list.sort()
# we sort, but with a priority for xx and en language codes
priority = {"en": 1, "xx": 0}
lc_list.sort(key=lambda name: (priority.get(name[:2], 100), name))
return lc_list

def get_tags_line(self, node, lc):
"""return a string that should look like the original line"""
line = (", ").join(node["tags_" + lc])
return lc + ":" + line

@staticmethod
def property_sort_key(property):
name, lang_code, *_ = property.split("_", 2)
# give priority to xx and en language codes
priority = {"en": 1, "xx": 0}
return (name, priority.get(lang_code, 100), lang_code)

def list_property_and_lc(self, node):
"""return an ordered list of properties with their language code (lc)"""
# there is no rule for the order of properties
# properties will be arranged in alphabetical order
return sorted([property[5:] for property in node if property.startswith("prop_") and not property.endswith("_comments")])
values = [property[5:] for property in node if property.startswith("prop_") and not property.endswith("_comments")]
# note: using the fact that we are sure to find language code after the first underscore
return sorted(values, key=self.property_sort_key)

def get_property_line(self, node, property):
"""return a string that should look like the original property line"""
Expand Down