forked from chefferk/latex_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic_to_latex.py
195 lines (159 loc) · 5.7 KB
/
logic_to_latex.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from logicDB import rules
from logicDB import conversions
import re
# If in test mode, file is already selected vs user entering file name
# (e.g. terminal interface)
# DEBUG
test_mode = True
'''
Known problems:
- The program changes 'v's in annotations to ' \vee '
FIX: just change the LaTex back to 'v'
- Cannot have spaces in sentences
FIX: delete spaces in sentences in text file prior to import
- Need to have space between annotations and numbers
- Cannot have empty assumption set (e.g. axioms)
FIX: add a character place holder (e.g. 'x') and remove it later
'''
# Description: If test mode is False, this will ask user for file name and loop
# until valid file is given
# Input: none, user input
# Output: the opened file - infile
def get_file():
flag = True
while flag is True:
try:
file = input('Enter the name of the txt file: ')
infile = open(file, 'r')
flag = False
except Exception:
flag = True
return infile
# Description: Creates a new 2D list with each element split
# Input: content_list - a 1D list with *correct* spacing
# Output: split list
def split_list(content_list):
# Split elements in list
new_list = []
for i in range(len(content_list)):
new_list.append(re.findall(r'([^\s]+)', content_list[i]))
return new_list
# Description: Prints LaTex formatting to the console
# Input: content_list
# Output: none, output to screen
# TODO : this could be simplified a lot
def latex_print(content_list):
assumption_set = []
line_number = []
sentence = []
annotation_set = []
annotation = []
for i in range(len(content_list)):
assumption_set.append(content_list[i][0])
line_number.append(content_list[i][1])
sentence.append(content_list[i][2])
# Some rows have 5 columns due to annotation sets
# TODO : this seems like a poor way to handle this
if len(content_list[i]) == 5:
annotation_set.append(content_list[i][3])
annotation.append(content_list[i][4])
else:
annotation_set.append('')
annotation.append(content_list[i][3])
# TODO : Prints to console, could return or save to file in future
print('{} & {} ${}$ & {} & {}\\\\' .format(assumption_set[i],
line_number[i],
sentence[i],
annotation_set[i],
annotation[i]))
# Description: Swaps symbols for sentence column from logic to LaTex
# Input: conversions list, content list
# Output: updated list
# TODO(1) : only want this to apply to content_list[i][2]
# BUG
def sentence_swap(conversions, content_list):
for i in range(len(content_list)):
for j in range(len(conversions)):
content_list[i] = [x.replace(conversions[j][2], conversions[j][1]) for x in content_list[i]]
return content_list
# Description: Swaps annotation symbols for words
# Input: rules list, content list
# Output: updated list
# TODO(2) : only want this to apply to content_list[i][4]
# TODO : could use regex to parse two spaces as boundary for sentences
# BUG
def assumption_swap(rules, content_list):
for i in range(len(content_list)):
for j in range(len(rules)):
content_list[i] = [x.replace(rules[j][0], rules[j][2]) for x in content_list[i]]
return content_list
# Description: Performs swaps for first line
# Input: conversions list, line to be converted
# Output: converted line
def swap(conversions, logic):
logic = logic.replace(' ', '')
logic = logic.strip()
for i in range(len(conversions)):
logic = logic.replace(conversions[i][2], conversions[i][1])
return logic
# Description: Prints converted line
# Input: converted line
# Output: none, screen output
def latex_print_line(logic):
print('${}$\\\\' .format(logic))
def main():
# DEBUG
if test_mode is True:
infile = open('logic.txt', 'r')
else:
infile = get_file()
# Create list and close file
content_list = infile.readlines()
infile.close()
first_line = content_list[0]
del content_list[0]
# Split elements in list
content_list = split_list(content_list)
# DEBUG - 1
if test_mode is True:
print('-' * 20)
print('(1) [Assumption set, Line Number, Sentence, Annotation]')
print('-' * 20)
# [Assumption set, Line Number, Sentence, Annotation]
for i in content_list:
print(i)
# DEBUG - 2
if test_mode is True:
print('-' * 20)
print('(2) Initial LaTex print before any swaps')
print('-' * 20)
latex_print(content_list)
content_list = assumption_swap(rules, content_list)
# DEBUG - 3
if test_mode is True:
print('-' * 20)
print('(3) LaTex print after Annotations are swapped')
print('-' * 20)
latex_print(content_list)
content_list = sentence_swap(conversions, content_list)
logic = swap(conversions, first_line)
# DEBUG - 4
if test_mode is True:
print('-' * 20)
print('(4) LaTex print after sentences are swapped (final result)')
print('-' * 20)
latex_print_line(logic)
print('')
print('\\begin{tabular}{llll}')
# print final answer
latex_print(content_list)
print('\\end{tabular}')
else:
latex_print_line(logic)
print('')
print('\\begin{tabular}{llll}')
# print final answer
latex_print(content_list)
print('\\end{tabular}')
if __name__ == "__main__":
main()