-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsplit.py
94 lines (88 loc) · 3.59 KB
/
split.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
import json
def split_exprs(tokens,
l_dep_tokens=json.load(open('data/open_list.json')) + ['{'],
r_dep_tokens=json.load(open('data/close_list.json')) + ['}'],
break_tokens=json.load(open('data/punctuation_list.json')),
relations_list=json.load(open('data/relations_list.json')),
text_tol = 4):
"""@param: tokens: a list of tokens to split up
@param: l_dep_tokens: tokens that signify entering into a dependent. Default from KaTeX
@param: r_dep_tokens: tokens that signify exiting a dependent. Default from KaTeX
@param: break tokens: tokens like , and ; that should be broken on. Default from KaTeX
@param: text_tol: how many tokens to allow in a text tag before splitting
@returns a list of lists of tokens from seperate subexpressions.
Deals with, for example, aligned equations where the first line is
something like f = x + y and the second line is g = 6 + i, which should
not be considered aligned. It will consider f(x) = y \\ = y + 8 a single expression, however
"""
dep_depth = 0
prev_idx = 0
ret = [[]]
i = 0
skip = 0
for i, t in enumerate(tokens):
if i < skip:
continue
if t in l_dep_tokens:
dep_depth += 1
elif t in r_dep_tokens:
dep_depth -= 1
elif dep_depth == 0:
if t == '\\\\':
# folds in lines that are a continuation
if i < len(tokens) - 1 and tokens[i+1] in relations_list:
skip = i + 1
# otherwise it splits
else:
ret.append([])
skip = i + 1
elif t in break_tokens:
skip = i + 1
ret.append([])
# handle inline text
elif t in ['\\mbox', '\\text', '\\textrm', '\\textstyle', '\\hbox']:
skip = i
for _t in tokens[i:]:
skip += 1
if _t in r_dep_tokens:
break
if skip - i > text_tol:
ret.append([])
else:
skip = i
if i >= skip:
ret[-1].append(t)
return ret
def split_high_level_eqs(tokens,
l_dep_tokens=json.load(open('data/open_list.json')) + ['{'],
r_dep_tokens=json.load(open('data/close_list.json')) + ['}'],
split_tokens=json.load(open('data/relations_list.json'))):
"""@param: tokens: list of strings
@param: l_dep_tokens: characters that signify entering into a dependent. Default from KaTeX
@param: r_dep_tokens: characters that signify exiting a dependent. Default from KaTeX
@param: split_tokens: characters that signify exiting a dependent. Default from KaTeX
@returns list of lists of strings split on split_chars"""
dep_depth = 0
prev_idx = 0
ret = []
i = 0
for t in tokens:
if t in l_dep_tokens:
dep_depth += 1
elif t in r_dep_tokens:
dep_depth -= 1
elif t in split_tokens:
if dep_depth == 0:
ret.append(tokens[prev_idx:i])
prev_idx = i + 1
i += 1
if (prev_idx < i):
ret.append(tokens[prev_idx:])
if(dep_depth != 0):
return []
return ret
def split(tokens):
"""Wrapper for both splits.
@param tokens: list of tokens
return a list of lists of epxressions and tokens"""
return [split_high_level_eqs(eq) for eq in split_exprs(tokens)]