-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrow_length_calculator.py
140 lines (111 loc) · 5.04 KB
/
row_length_calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from talmudifier.word import Word
from talmudifier.pdf_writer import PDFWriter
from talmudifier.pdf_reader import PDFReader
from talmudifier.style import Style
from random import shuffle
from tqdm import tqdm
from talmudifier.talmudifier import Paracol
from argparse import ArgumentParser
from json import load
from pathlib import Path
class RowLengthCalculator:
"""
Calculate the average number of characters in a given number of rows.
"""
def __init__(self, columns: str, target: str, font: str, font_size: str, num_rows: int):
"""
:param columns: The columns included in this paracol environment as a string, e.g. "LC"
:param target: The target column, e.g. "left"
:param font: The font command, e.g. "\\leftfont"
:param font_size: The font size command, e.g. "\\fontsize{11}{13}"
:param num_rows: The number of rows to make.
"""
self.paracol = Paracol.get_paracol_header("L" in columns, "C" in columns, "R" in columns)
switch = Paracol.get_switch_from_left("L" in columns, "C" in columns, "R" in columns, target)
self.paracol += "\n\n" + switch
self.paracol += "\n\n" + font_size + font
self.writer = PDFWriter(Path("header.txt").read_text())
self.num_rows = num_rows
def _get_num_rows(self, line: str, word: Word) -> int:
"""
Returns the number of rows if the word was added to the line.
:param line: The line.
:param word: The new word.
"""
tex = r"\internallinenumbers \begin{linenumbers}" + line + " " + word.word + r"\end{linenumbers} \resetlinenumber[1]"
tex = self.paracol + tex + "\n\n\\end{paracol}"
self.writer.write(tex, "line_count")
output_path = str(Path("Output/line_count.pdf").resolve())
return PDFReader.get_num_rows(output_path)
def _get_num_characters_in_trial(self, josephus: list, style: Style) -> int:
"""
Fill a row with random words from Josephus' Antiquities, and return the number of characters.
Try to fill the row as much as possible by adding a hyphenated fragment at the end.
:param josephus: Random words from Antiquities.
:param style: The font style (regular).
"""
line = ""
random_words = josephus[:]
shuffle(random_words)
# Build a row of random words.
done = False
while not done:
word = Word(random_words.pop(), style, None, None)
# Filter out invalid words.
while not Word.is_valid(word.word):
word = Word(random_words.pop(), style, None, None)
num_lines = self._get_num_rows(line, word)
# We went over the end. Try to get a hyphenated fragment.
if num_lines > self.num_rows:
pairs = word.pairs
for pair in pairs:
for p in pair:
num_lines = self._get_num_rows(line, p)
if num_lines == self.num_rows:
line += " " + p.word
return len(line.strip())
done = True
else:
line += " " + word.word
return len(line.strip())
def get_num_chars(self, num_trials: int) -> int:
"""
Get the average number of characters over the course of many trials.
:param num_trials: The number of trials.
"""
num_chars_total = 0
pbar = tqdm(total=num_trials)
# Get some words.
with open("test/josephus.txt", "rt") as f:
josephus = f.read()
josephus = josephus.split(" ")
josephus = [j for j in josephus if j != ""]
style = Style(False, False, False)
for i in range(num_trials):
num_chars_total += self._get_num_characters_in_trial(josephus, style)
pbar.update(1)
pbar.set_description(str(round(num_chars_total / (i + 1))))
pbar.close()
return round(num_chars_total / num_trials)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("--columns", type=str)
parser.add_argument("--target", type=str)
parser.add_argument("--rows", nargs="?", default=1, type=int)
parser.add_argument("--trials", nargs="?", default=100, type=int)
parser.add_argument("--recipe", nargs="?", default="default.json")
args = parser.parse_args()
# Load the recipe
with open("recipes/" + args.recipe, "rt") as f:
recipe = load(f)
if args.target == "left":
font = r"\leftfont"
elif args.target == "center":
font = r"\centerfont"
elif args.target == "right":
font = r"\rightfont"
else:
raise Exception(f"Invalid target: {args.target}")
size = r"\fontsize{" + str(recipe["fonts"][args.target]["size"]) + "}{" + str(recipe["fonts"][args.target]["skip"]) + "}"
num_chars = RowLengthCalculator(args.columns, args.target, font, size, args.rows).get_num_chars(args.trials)
print(f"Cols: {args.columns}\nTarget: {args.target}\nRows: {args.rows}\nAVERAGE: {num_chars}")