-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_raw.py
executable file
·225 lines (205 loc) · 7.74 KB
/
get_raw.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
#!/usr/bin/env python
# coding: utf-8
from argparse import ArgumentParser
import os.path
import pickle
import sys
import get_dictionary
# tokens is a list of tokens, so no need to split it again
def unkify(tokens, words_dict):
final = []
for token in tokens:
# only process the train singletons and unknown words
if len(token.rstrip()) == 0:
final.append('UNK')
elif not(token.rstrip() in words_dict):
numCaps = 0
hasDigit = False
hasDash = False
hasLower = False
for char in token.rstrip():
if char.isdigit():
hasDigit = True
elif char == '-':
hasDash = True
elif char.isalpha():
if char.islower():
hasLower = True
elif char.isupper():
numCaps += 1
result = 'UNK'
lower = token.rstrip().lower()
ch0 = token.rstrip()[0]
if ch0.isupper():
if numCaps == 1:
result = result + '-INITC'
if lower in words_dict:
result = result + '-KNOWNLC'
else:
result = result + '-CAPS'
elif not(ch0.isalpha()) and numCaps > 0:
result = result + '-CAPS'
elif hasLower:
result = result + '-LC'
if hasDigit:
result = result + '-NUM'
if hasDash:
result = result + '-DASH'
if lower[-1] == 's' and len(lower) >= 3:
ch2 = lower[-2]
if not(ch2 == 's') and not(ch2 == 'i') and not(ch2 == 'u'):
result = result + '-s'
elif len(lower) >= 5 and not(hasDash) and not(hasDigit and numCaps > 0):
if lower[-2:] == 'ed':
result = result + '-ed'
elif lower[-3:] == 'ing':
result = result + '-ing'
elif lower[-3:] == 'ion':
result = result + '-ion'
elif lower[-2:] == 'er':
result = result + '-er'
elif lower[-3:] == 'est':
result = result + '-est'
elif lower[-2:] == 'ly':
result = result + '-ly'
elif lower[-3:] == 'ity':
result = result + '-ity'
elif lower[-1] == 'y':
result = result + '-y'
elif lower[-2:] == 'al':
result = result + '-al'
final.append(result)
else:
final.append(token.rstrip())
return final
def is_next_open_bracket(line, start_idx):
for char in line[(start_idx + 1):]:
if char == '(':
return True
elif char == ')':
return False
raise IndexError('Bracket possibly not balanced, open bracket not followed by closed bracket')
def get_between_brackets(line, start_idx):
output = []
for char in line[(start_idx + 1):]:
if char == ')':
break
assert not(char == '(')
output.append(char)
return ''.join(output)
# start_idx = open bracket
#def skip_terminals(line, start_idx):
# line_end_idx = len(line) - 1
# for i in range(start_idx + 1, line_end_idx):
# if line[i] == ')':
# assert line[i + 1] == ' '
# return (i + 2)
# raise IndexError('No close bracket found in a terminal')
def get_tags_tokens_lowercase(line):
output = []
#print 'curr line', line_strip
line_strip = line.rstrip()
#print 'length of the sentence', len(line_strip)
for i in range(len(line_strip)):
if i == 0:
assert line_strip[i] == '('
if line_strip[i] == '(' and not(is_next_open_bracket(line_strip, i)): # fulfilling this condition means this is a terminal symbol
output.append(get_between_brackets(line_strip, i))
#print 'output:',output
output_tags = []
output_tokens = []
output_lowercase = []
for terminal in output:
terminal_split = terminal.split()
assert len(terminal_split) == 2 # each terminal contains a POS tag and word
output_tags.append(terminal_split[0])
output_tokens.append(terminal_split[1])
output_lowercase.append(terminal_split[1].lower())
return [output_tags, output_tokens, output_lowercase]
def get_nonterminal(line, start_idx):
assert line[start_idx] == '(' # make sure it's an open bracket
output = []
for char in line[(start_idx + 1):]:
if char == ' ':
break
assert not(char == '(') and not(char == ')')
output.append(char)
return ''.join(output)
def get_actions(line):
output_actions = []
line_strip = line.rstrip()
i = 0
max_idx = (len(line_strip) - 1)
while i <= max_idx:
assert line_strip[i] == '(' or line_strip[i] == ')'
if line_strip[i] == '(':
if is_next_open_bracket(line_strip, i): # open non-terminal
curr_NT = get_nonterminal(line_strip, i)
output_actions.append('NT(' + curr_NT + ')')
i += 1
while line_strip[i] != '(': # get the next open bracket, which may be a terminal or another non-terminal
i += 1
else: # it's a terminal symbol
output_actions.append('SHIFT')
while line_strip[i] != ')':
i += 1
i += 1
while line_strip[i] != ')' and line_strip[i] != '(':
i += 1
else:
output_actions.append('REDUCE')
if i == max_idx:
break
i += 1
while line_strip[i] != ')' and line_strip[i] != '(':
i += 1
assert i == max_idx
return output_actions
def main(args):
words_list = None
if args.vocab_file is not None and os.path.exists(args.vocab_file):
# Load vocab.
with open(args.vocab_file, "rb") as vocab_f:
words_list = pickle.load(vocab_f)
elif args.train_file is not None:
with open(args.train_file, "r") as train_f:
train_lines = train_f.readlines()
words_list = get_dictionary.get_dict(train_lines)
if args.vocab_file is not None:
# Save.
with open(args.vocab_file, "wb") as vocab_f:
pickle.dump(words_list, vocab_f)
with open(args.input_file, "r") as input_f:
lines = input_f.readlines()
line_ctr = 0
# get the oracle for the train file
for line in lines:
line_ctr += 1
# assert that the parenthesis are balanced
if line.count('(') != line.count(')'):
raise NotImplementedError('Unbalanced number of parenthesis in line ' + str(line_ctr))
# first line: the bracketed tree itself itself
# print '# ' + line.rstrip()
# tags, tokens, lowercase = get_tags_tokens_lowercase(line)
# assert len(tags) == len(tokens)
# assert len(tokens) == len(lowercase)
# # print ' '.join(tags)
# print ' '.join(tokens)
# #print ' '.join(lowercase)
tokens = line.strip().split()
unkified = unkify(tokens, words_list)
print(' '.join(unkified))
# output_actions = get_actions(line)
# for action in output_actions:
# print action
# print ''
if __name__ == "__main__":
p = ArgumentParser()
p.add_argument("-t", "--train_file")
p.add_argument("-v", "--vocab_file")
p.add_argument("input_file")
args = p.parse_args()
if args.train_file is None and args.vocab_file is None:
print("One of --train_file, --vocab_file is required.")
sys.exit(1)
main(args)