Implement bingo generator
This commit is contained in:
51
bingo/generate_bingo.py
Normal file
51
bingo/generate_bingo.py
Normal file
@@ -0,0 +1,51 @@
|
||||
import random
|
||||
from functools import reduce
|
||||
from tex_template import latex_template # tex_template.py
|
||||
|
||||
output_dir = "build/"
|
||||
|
||||
num_total = 9
|
||||
num_hard = 3
|
||||
num_easy = num_total - num_hard
|
||||
|
||||
sentences = {"easy": [], "hard": []}
|
||||
with open("prompts.txt") as f:
|
||||
raw_sentences = f.readlines()
|
||||
stripped = "".join(raw_sentences).strip().split("\n")
|
||||
sentences["easy"] = [ line for line in stripped if "*" not in line ]
|
||||
sentences["hard"] = [ line.replace("*","") for line in stripped if "*" in line ]
|
||||
|
||||
print(f"[INFO] Found {len(sentences['easy'])} easy prompts")
|
||||
print(f"[INFO] Found {len(sentences['hard'])} hard prompts")
|
||||
|
||||
|
||||
""" Postprocess the content string to look nice in the latex tabular cell """
|
||||
def make_cell(content: str):
|
||||
return fr"""\vspace{{0.5cm}}
|
||||
\rule{{3cm}}{{0.15mm}} \, \newline \small {content}
|
||||
\vspace{{0.5cm}}"""
|
||||
|
||||
def substitute_texts(text: str, substitutions: list[str]):
|
||||
return reduce( \
|
||||
# reducer
|
||||
lambda acc, value: acc.replace(f"[[text{value}]]", make_cell(substitutions[value])), \
|
||||
# list
|
||||
range(len(substitutions)), \
|
||||
# initializer
|
||||
text \
|
||||
)
|
||||
|
||||
def gen_board(num: int = 1):
|
||||
print(f"Generating {num} boards")
|
||||
|
||||
for i in range(1, num+1):
|
||||
prompts = [ *random.sample(sentences["easy"], num_easy), *random.sample(sentences["hard"], num_hard) ]
|
||||
random.shuffle(prompts)
|
||||
|
||||
pick = substitute_texts(latex_template, prompts)
|
||||
|
||||
filename = "output.tex" if num == 1 else f"output{i}.tex"
|
||||
with open(output_dir + filename,"w") as f:
|
||||
f.writelines(pick)
|
||||
|
||||
gen_board(36)
|
||||
Reference in New Issue
Block a user