forked from MrLogarithm/stag-txt2tex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt2tex.py
245 lines (223 loc) · 7.2 KB
/
txt2tex.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# TODO
# - generate all trees in one forest environment? then adjunction arrows are easier to draw using tikz named nodes
# - for STAG pairs: find bottom-left corner of source, bottom right corner of target, and give them special labels. then draw STAG brackets/MCsets with a method like the following:
#
# \path[draw=none] let \p1=(bottomleft.south), \p2=(bottomright.south) in (bottomright.east|-\pgfmath{min(\y1, \y2)}) -- coordinate[midway](right1) (bottomright.east|-srcroot.north);
# \path[draw=none] let \p1=(bottomleft.south), \p2=(bottomright.south) in (bottomleft.west|-\pgfmath{min(\y1,\y2)}) -- coordinate[midway](left1) (bottomleft.west|-srcroot.north);
# \draw let \p1=(srcroot.north), \p2=(bottomleft.south), \p3=(bottomright.south), \n1={(\y1-min(\y2,\y3)) / 2} in (left1) node {\resizebox{\abw}{\n1}{$\biggl<$}};
# \draw let \p1=(srcroot.north), \p2=(bottomleft.south), \p3=(bottomright.south), \n1={(\y1-min(\y2,\y3)) / 2} in (right1) node {\resizebox{\abw}{\n1}{$\biggr>$}};
#
import sys, re
import argparse
parser = argparse.ArgumentParser(description='Generate LaTeX trees from bracketed representations.')
parser.add_argument('-p', dest='preamble', action='store_true', help='Include preamble (macro definitions and \\usepackage{}s)?')
parser.add_argument('-s', dest='scale', type=float, default=0.6, help='scale the box containing the entire figure by this much')
parser.add_argument('-c', dest='circle', action='store_true', help='Circle links? (default behavior puts links in boxes)')
parser.add_argument('-f', dest='path', help='Path to file containing trees.' )
args = parser.parse_args()
def parseNamedTree( s ):
s = s.strip()
if s == '':
raise ValueError("should not call parseNamedTree with an empty string")
if s[0] == '{':
return ( ('',) + parseMCSet( s ) )
elif s[0] == '[':
return ( ('',) + parseTree( s ) )
else:
name, rest = s.split(' ', 1)
#print "name:", name
#print "rest:", rest
return ( (name,) + parseTree( rest ) )
def parseMCSet( s ):
#print "entering parseMCSet"
if s[0] != '{':
raise ValueError("bad input for MCSet")
rest = s[1:]
(left_name, rest, left_tree) = parseNamedTree(rest)
(right_name, rest, right_tree) = parseNamedTree(rest)
rest = rest.strip()
if rest[0] != '}':
raise ValueError("bad input for MCSet")
rest = rest[1:] # remove closing curly bracket
mcset = ''
if left_name is not '' and right_name is not '':
mcset = r'\namedmcset{'+left_name+'}{'+left_tree+'}{'+right_name+'}{'+right_tree+'}'
else:
mcset = r'\mcset{'+left_tree+'}{'+right_tree+'}'
return (rest, mcset)
def parseTree( s ):
return parseTree_( s, 0, '' )
def parseTree_( s, openBraces, tree ):
char = s[0]
if char == '[':
return parseTree_( s[1:], openBraces+1, tree+char )
elif char == ']':
if openBraces-1 == 0:
return (s[1:], tree+char)
else:
return parseTree_( s[1:], openBraces-1, tree+char )
else:
return parseTree_( s[1:], openBraces, tree+char )
preamble = """
\\usepackage{adjustbox}
\\usepackage{forest}
\\usepackage{amsmath}
% Boxed/circled numbers, for drawing linked nodes:
\\newcommand{\\circled}[1]{\\textcircled{\\scalebox{0.8}{#1}}}
\\renewcommand{\\boxed}[1]{\\raisebox{1pt}{\\scalebox{0.65}{\\fbox{#1}}}}
% Draw the angled brackets around the STAG tree-pair
\\newlength{\\abw}
\\settowidth{\\abw}{$\\biggl<$}
\\newsavebox{\\mybox}
\\newcommand{\\bigabx}[1]{%
\\savebox{\\mybox}{#1}%
\\resizebox{\\abw}{\\ht\\mybox}{$\\biggl<$}%
\\raisebox{.25\\ht\\mybox}{\\usebox{\\mybox}}%
\\resizebox{\\abw}{\\ht\\mybox}{$\\biggr>$}%
}
% Draw the curly brackets around the STAG MC sets
\\newlength{\\acw}
\\settowidth{\\acw}{$\\biggl\\{$}
\\newsavebox{\\cbox}
\\newcommand{\\bigacx}[1]{%
\\savebox{\\cbox}{#1}%
\\resizebox{\\acw}{\\ht\\cbox}{$\\biggl\\{$}%
\\raisebox{.25\\ht\\cbox}{\\usebox{\\cbox}}%
\\resizebox{\\acw}{\\ht\\cbox}{$\\biggr\\}$}%
}
% STAG tree-pair
\\newcommand{\\stagrule}[2]{
\\bigabx{
\\begin{adjustbox}{valign=M}
\\begin{forest}
#1
\\end{forest}
\\end{adjustbox}
,
\\begin{adjustbox}{valign=M}
\\begin{forest}
#2
\\end{forest}
\\end{adjustbox}
}
}
% Named STAG tree-pair
\\newcommand{\\namedstagrule}[4]{
\\bigabx{
\\begin{adjustbox}{valign=M}
\\begin{forest}
[,phantom,s sep=0
[#1:]
#2
]
\\end{forest}
\\end{adjustbox}
,
\\begin{adjustbox}{valign=M}
\\begin{forest}
[,phantom,s sep=0
[#3:]
#4
]
\\end{forest}
\\end{adjustbox}
}
}
% Named STAG MCSet tree-pair
\\newcommand{\\namedstagmcset}[3]{
\\bigabx{
\\begin{adjustbox}{valign=M}
\\begin{forest}
[,phantom,s sep=0
[#1:]
#2
]
\\end{forest}
\\end{adjustbox}
,
\\begin{adjustbox}{valign=M}
#3
\\end{adjustbox}
}
}
% STAG MCSet pair
\\newcommand{\\mcset}[2]{
\\bigacx{
\\begin{adjustbox}{valign=M}
\\begin{forest}
#1
\\end{forest}
\\end{adjustbox}
,
\\begin{adjustbox}{valign=M}
\\begin{forest}
#2
\\end{forest}
\\end{adjustbox}
}
}
% Named STAG MCSet pair
\\newcommand{\\namedmcset}[4]{
\\bigacx{
\\begin{adjustbox}{valign=M}
\\begin{forest}
[,phantom,s sep=0
[#1:]
#2
]
\\end{forest}
\\end{adjustbox}
,
\\begin{adjustbox}{valign=M}
\\begin{forest}
[,phantom,s sep=0
[#3:]
#4
]
\\end{forest}
\\end{adjustbox}
}
}
% Forest format: makes branches connect at the bottom
% of a node:
\\forestset{
default preamble={
for tree={
parent anchor=south,
child anchor=north,
align=center,
}
}
}
"""
header = r'\begin{adjustbox}{scale=' + str(args.scale) + r'}'
footer = r'\end{adjustbox}'
f = open( args.path )
s = f.read()
s = s.replace("\t", " ")
s = s.replace("\n", " ")
f.close()
if args.preamble:
print( preamble )
print(header)
while s.strip() != '':
if s == '':
break
else:
#print "s:", s
print
try:
(src_name, s, src_tree) = parseNamedTree(s)
(tgt_name, s, tgt_tree) = parseNamedTree(s)
except IndexError:
print('Couldn\'t parse tree! Are there are even number of trees in the input file?')
link_annotation = r"\\circled{\1}" if args.circle else r"\\boxed{\1}"
src_tree = re.sub( r"\((\d+)\)", link_annotation, src_tree.strip() )
tgt_tree = re.sub( r"\((\d+)\)", link_annotation, tgt_tree.strip() )
if src_name != '' and tgt_name != '':
print( r'\namedstagrule{'+src_name+'}{'+src_tree+'}{'+tgt_name+'}{'+tgt_tree+'}' )
elif src_name != '' and tgt_name == '':
print( r'\namedstagmcset{'+src_name+'}{'+src_tree+'}{'+tgt_tree+'}' )
else:
print( r'\stagrule{'+src_tree+'}{'+tgt_tree+'}' )
print(footer)