regex-riddles/generate.py

72 lines
1.8 KiB
Python
Raw Permalink Normal View History

2024-05-17 19:42:50 +02:00
#!/usr/bin/python3
from random import randint, random, choice
from crime_campus_exam_data import greeting, network, hidden_data
2024-06-11 16:23:28 +02:00
def resolve_language(text, language):
if isinstance(text, str):
return text
if isinstance(text, dict):
return text[language]
german, english = text
if language == "de":
return german
else:
return english
def get_both_languages(text):
if isinstance(text, str):
return text, text
if isinstance(text, dict):
return text["de"], text["en"]
german, english = text
return german, english
2024-06-11 16:38:31 +02:00
def generate_texts(number_of_lines = 200):
""" language can be "en" or "de".
2024-06-05 12:09:19 +02:00
"""
local_hidden_data = hidden_data[:]
2024-06-11 16:38:31 +02:00
result = {
"de": "",
"en": "",
}
2024-06-03 11:02:01 +02:00
for i in range(number_of_lines):
emissions, _ = network.choose()
german = ""
english = ""
for emission in emissions:
2024-06-11 16:23:28 +02:00
ger, eng = get_both_languages(emission)
german += ger
english += eng
2024-06-11 16:38:31 +02:00
result["de"] += german + "\n"
result["en"] += english + "\n"
if random() < len(local_hidden_data) / (number_of_lines - i):
index = randint(0, len(local_hidden_data) - 1)
hidden_line = local_hidden_data.pop(index)
2024-06-11 16:38:31 +02:00
german, english = get_both_languages(hidden_line)
result["de"] += german + "\n"
result["en"] += english + "\n"
return result
def read_template(filename = "./template.html"):
with open(filename, "r") as file:
return file.read()
def write_index(text, filename = "./deploy/index.html"):
with open(filename, "w") as file:
file.write(text)
def main():
template = read_template()
2024-06-11 16:38:31 +02:00
texts = generate_texts()
2024-06-05 12:38:35 +02:00
for language in ["de", "en"]:
2024-06-11 16:38:31 +02:00
generated = resolve_language(greeting, language) + "\n"
generated += resolve_language(texts, language)
2024-06-05 12:09:19 +02:00
text = template.replace("<replace with text>", generated)
write_index(text, filename = f"./deploy/{language}/index.html")
if __name__ == "__main__":
main()