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

batch translations #286

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
14 changes: 5 additions & 9 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def jsonify(weeks: Dict[int, Week], directory: str, canteen: Canteen, combine_di
year = week.year

# create dir: <year>/
json_dir = f"{str(directory)}/{str(year)}"
json_dir = f"{directory}/{year}"
if not os.path.exists(json_dir):
os.makedirs(json_dir)

Expand All @@ -52,7 +52,7 @@ def jsonify(weeks: Dict[int, Week], directory: str, canteen: Canteen, combine_di
if week_json is not None:
week_json["version"] = JSON_VERSION
# write JSON to file: <year>/<calendar_week>.json
with open(f"{str(json_dir)}/{str(calendar_week).zfill(2)}.json", "w", encoding="utf-8") as outfile:
with open(f"{json_dir}/{str(calendar_week).zfill(2)}.json", "w", encoding="utf-8") as outfile:
json.dump(week_json, outfile, separators=(",", ":"), ensure_ascii=False)

# check if combine parameter got set
Expand All @@ -62,9 +62,9 @@ def jsonify(weeks: Dict[int, Week], directory: str, canteen: Canteen, combine_di
combined_df_name = "combined"

# create directory for combined output
json_dir = f"{str(directory)}/{combined_df_name}"
json_dir = f"{directory}/{combined_df_name}"
if not os.path.exists(json_dir):
os.makedirs(f"{str(directory)}/{combined_df_name}")
os.makedirs(json_dir)

# convert all weeks to one JSON object
weeks_json_all = json.dumps(
Expand All @@ -78,7 +78,7 @@ def jsonify(weeks: Dict[int, Week], directory: str, canteen: Canteen, combine_di
)

# write JSON object to file
with open(f"{str(json_dir)}/{combined_df_name}.json", "w", encoding="utf-8") as outfile:
with open(f"{json_dir}/{combined_df_name}.json", "w", encoding="utf-8") as outfile:
json.dump(json.loads(weeks_json_all), outfile, separators=(",", ":"), ensure_ascii=False)


Expand All @@ -104,10 +104,6 @@ def main():
if menus is None:
sys.exit("Error. Could not retrieve menu(s)")

# sort dishes before translating to keep the order
for menu in menus.values():
menu.dishes.sort(key=lambda dish: dish.name)

# optionally translate the dish titles
if args.language is not None and args.language.upper() != "DE":
util.translate_dishes(menus, args.language)
Expand Down
12 changes: 6 additions & 6 deletions src/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def make_duplicates_unique(names_with_duplicates):

def translate_dishes(menus: Dict[date, Menu], language: str) -> None:
"""
Translate the dish titles of a menu
Translate the dish titles of a menu. Source language is always german.

:param menus: Menus dictionary as given by the menu parser, will be modified
:param language: Identifier for a language
Expand All @@ -49,9 +49,9 @@ def translate_dishes(menus: Dict[date, Menu], language: str) -> None:

translator = deepl.Translator(deepl_api_key)

# traverse through all dish titles
# batch dish names to prevent being rate limited
for menu in menus.values():
for dish in menu.dishes:
# source language is always german
result = translator.translate_text(dish.name, source_lang="DE", target_lang=language)
dish.name = result.text
batch = [dish.name for dish in menu.dishes]
results = translator.translate_text(batch, source_lang="DE", target_lang=language)
for dish, name in zip(menu.dishes, results):
dish.name = name
Loading