-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathtokenize_format.py
More file actions
177 lines (138 loc) · 6.01 KB
/
tokenize_format.py
File metadata and controls
177 lines (138 loc) · 6.01 KB
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
import copy
from typing import List, Dict
from atcodertools.fmtprediction.models.calculator import CalcNode, CalcParseError
from atcodertools.fmtprediction.models.variable_token import VariableToken, TokenizedFormat
from atcodertools.fmtprediction.token_manager import TokenManager
def _is_ascii(s):
return all(ord(c) < 128 for c in s)
DOTS_PATTERNS = ["ldots", "cdots", "vdots", "ddots", "dots"]
SPACE_PATTERNS = ["hspace", "vspace"]
def _is_noise(s):
if any(pattern in s for pattern in DOTS_PATTERNS):
return True
if any(pattern in s for pattern in SPACE_PATTERNS):
return True
return s == ":" or s == "...." or s == "..." or s == ".." or s == "."
def _normalize_index(text):
return text.replace("{(", "").replace(")}", "")
def _divide_consecutive_vars(text):
res_text = ""
i = 0
while i < len(text):
if text[i] == "_":
res_text += "_"
i += 1
if i < len(text) and text[i].isdigit():
while i < len(text) and text[i].isdigit():
res_text += text[i]
i += 1
elif i < len(text) and text[i].isalpha():
res_text += text[i]
i += 1
if i < len(text) and text[i].isalpha():
res_text += " "
else:
res_text += text[i]
i += 1
return res_text
def _remove_spaces_in_curly_brackets(input_format):
res = []
nest = 0
for c in input_format:
if c == '{':
nest += 1
elif c == '}':
nest -= 1
if c == ' ' and nest > 0:
continue
res.append(c)
return "".join(res)
def _sanitized_tokens(input_format: str) -> List[str]:
input_format = input_format.replace("\n", " ").replace("…", " ").replace("...", " ").replace(
"..", " ").replace("‥", " ").replace("\\ ", " ").replace("}", "} ").replace(" ", " ").replace(", ", ",")
input_format = input_format.replace(" _ ", "_") # 空白の添字を削除
input_format = _remove_spaces_in_curly_brackets(input_format)
input_format = _divide_consecutive_vars(input_format)
input_format = _normalize_index(input_format)
input_format = input_format.replace("{", "").replace("}", "")
tokens = [
x for x in input_format.split(
) if x != "" and _is_ascii(
x) and not _is_noise(
x)]
return tokens
class FormatSearcher:
def __init__(self, tokens):
self._token_manager = TokenManager(tokens)
self._answers = None
self._max_variables_count = None
def search(self, max_variables_count) -> List[TokenizedFormat]:
self._max_variables_count = max_variables_count
self._answers = []
self._inner_search([], {})
return self._answers
def _inner_search(self, var_token_seq, var_to_dim_num: Dict[str, int]):
if len(var_to_dim_num) > self._max_variables_count:
return
if self._token_manager.is_terminal():
self._answers.append(TokenizedFormat(copy.deepcopy(var_token_seq)))
return
for var_token in self._possible_var_tokens(self._token_manager.peek(), var_to_dim_num):
next_var_to_dim_num = copy.deepcopy(var_to_dim_num)
next_var_to_dim_num[var_token.var_name] = var_token.dim_num()
try:
var_token_seq.append(var_token)
self._token_manager.go_next()
self._inner_search(var_token_seq, next_var_to_dim_num)
finally:
self._token_manager.go_back()
var_token_seq.pop()
@staticmethod
def _possible_var_tokens(token: str, current_var_to_dim_num: Dict[str, int]) -> List[VariableToken]:
"""
Only considers to divide the given token into at most 3 pieces (that is, to assume at most 2 dimensional indexes).
:param token: e.g. "N", "abc_1_2" or "a_1 ... a_N"
:param current_var_to_dim_num: utilized to detect unknown variables (for pruning purpose)
"""
var_token_candidates = [VariableToken(token, None, None)]
var_token_candidates += [VariableToken(
token[:i],
token[i:],
None) for i in range(1, len(token))]
for i in range(1, len(token)):
for j in range(i + 1, len(token)):
var_token_candidates += [
VariableToken(token[:i], token[i:j], token[j:])]
def check_if_possible(var_token: VariableToken):
# check syntax error
if not var_token.is_valid():
return False
# check kind of synonym error using current_var_to_dim_num
for index in [var_token.first_index, var_token.second_index]:
if index is None:
continue
try:
for sub_var in CalcNode.parse(index).get_all_variables():
if sub_var not in current_var_to_dim_num:
return False
except CalcParseError:
return False
if var_token.var_name in current_var_to_dim_num \
and current_var_to_dim_num[var_token.var_name] != var_token.dim_num():
return False
return True
return [var_token for var_token in var_token_candidates if check_if_possible(var_token)]
def search_formats_with_minimum_vars(input_format: str) -> List[TokenizedFormat]:
"""
Fast enough for realistic instances.
This method returns possible formats with the smallest number of variables.
"""
tokens = _sanitized_tokens(input_format)
searcher = FormatSearcher(tokens)
for max_variable_length in range(1, 20):
result = searcher.search(max_variable_length)
if result:
return result
raise NoFormatFoundError
class NoFormatFoundError(Exception):
pass