-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractFragments.py
234 lines (209 loc) · 8.96 KB
/
extractFragments.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
import sys, re
from mapping import mapping
def importENabnf():
hm = {}
f_in = open("en.abnf","r")
for line in f_in:
line = line.replace("\n","")
line = line.replace("\"","")
if line == ",":
pass
else:
if "=" in line:
tmp = line.split("=")
name = tmp[0].replace("$","")
# I only want terminal to non-terminal relations and not rules, which point to another non-terminal.
if "$" in tmp[1]:
pass
else:
tmp_terminal = tmp[1].split("|")
for x in tmp_terminal:
hm[x.lower()] = name
# print hm
f_in.close()
return hm
#First simple approach for extracting pattern from an input file and additionoly substitution of words, e.g. Los Angeles to <CITYNAME>
def extractFromFile(input_file, output_file, groundtruthFile):
hm_terminal_enABNF = importENabnf()
f = open(input_file,"r")
f_out_overall = open("extracted_tmp","w")
counter_line = 0
original_sentence = []
statement_hm = {}
statement_overall = {}
for line in f:
line = line.replace("\n","")
flag = False
if line.startswith("<"):
tmp = line.split(" -> ")
if hm_terminal_enABNF.has_key(tmp[1].lower()):
replacement = hm_terminal_enABNF[tmp[1].lower()]
statement_hm[tmp[0]] = replacement
else:
statement_hm[tmp[0]] = tmp[1]
if line == "":
for key,value in statement_hm.iteritems():
if "Statement" in key:
while "Unknown" in value or "Individual" in value or "Statement" in value:
value = value.replace(" ","-")
value = value.replace("-<SGM>-","-")
# print key,value
tmp_terminals = re.findall(r'(<[A-Za-z\_0-9]*>)', value)
for y in tmp_terminals:
try:
if "Unknown" not in statement_hm[y] and "Individual" not in statement_hm[y] and "Statement" not in statement_hm[y]:
value = value.replace(y,"<"+statement_hm[y].upper().replace(" ","")+">")
else:
value = value.replace(y,statement_hm[y])
except:
pass
if statement_overall.has_key(key):
t_s = statement_overall[key]
if value in t_s:
pass
else:
t_s.append(value)
statement_overall[key] = t_s
else:
statement_overall[key] = [value]
statement_hm = {}
#Idee: Alle regeln fuer einen Block raussuchen, die mit < beginnen.
#Dann an " -> " trennen (speichern in tmp) und dann hm erzeugen, key= tmp[0] value = tmp[1]
#im letzten schritt dann laengste staetment regeln nehmen, die "unknowns" in der Liste nachschauen, und dann direkt matchen zu en.abnf.
#Danach dann normal weiter zum mapping, allerdings schauen, ob ich mir nicht einmal abspeichern sparen kann und direkt der function mapping ein array mit allen statement+
#fragment rules uebergebe.
#Hm, dran denken, dass ich mehrmals die variable Unknown haben kann, mit unterschiedlichen staedte Namen..
#Ich koennte natuerlich auch direkt in diesem Schritt, das mappen nach den eabnf regeln machen!!!
#und dann den Namen des Stadt erst garnicht abspeichern zu muessen.
#Beim Mapping so lange iterieren, bis kein Key mehr gefunden wird in der hm und das muesste dann die letzte moeglichkeit sein
for key in statement_overall:
write_string=""
if len(statement_overall[key]) > 0 and "statement" in key.lower():
write_string += key+","
for x in statement_overall[key]:
write_string += x+","
write_string = write_string[:-1]
write_string = write_string.replace(" ","")
if write_string.endswith(": \""):
pass
else:
write_string += "\n\n\n"
f_out_overall.write(write_string)
f_out_overall.close()
mapping("extracted_tmp",output_file,groundtruthFile)
#def extractFromFile_Old(input_file, output_file, groundtruthFile):
## if len(sys.argv) < 3:
## print "python extractFragments.py input.txt output.txt"
## exit(1)
##
# hm_terminal = importENabnf()
## input_file = sys.argv[1]
## output_file = sys.argv[2]
#
# f = open(input_file,"r")
# f_out_overall = open("extracted_tmp","w")
#
# counter_line = 0
# original_sentence = []
# statement_hm = {}
# statement_overall = {}
#
#
# for line in f:
# line = line.replace("\n","")
# flag = False
# if line == "":
# counter_line = 0
# pass
#
# else:
# if counter_line == 0:
# sentence = line.replace("0.000000","")
# sentence = sentence.replace(" "," ")
# counter_line += 1
# original_sentence = sentence.split(" ")
# tmp = line.split(" ")
#
# first_pos = 0
# last_pos = 0
# if "<SGM>" in tmp:
# counter = 0
# for x in tmp:
# if "<SGM>" == x and first_pos == 0:
# first_pos = counter
# elif "<SGM>" == x:
# last_pos = counter + 2
# counter += 1
# if "Statement" in x:
# flag = True
# if flag == True:
# statement = tmp[first_pos - 2]
# if statement_overall.has_key(statement):
# tmp = statement_overall[statement]
# write_string =""
# old_string = ""
# for x in original_sentence[(first_pos - 2)/2:last_pos/2]:
# blub = old_string + " "+ x
# if hm_terminal.has_key(blub.lower()):
# write_string += "<"+hm_terminal[blub.lower()].replace(" ","").upper()+">"+ "-"
# write_string = write_string.replace("-"+old_string+"-", "-")
#
# elif hm_terminal.has_key(x.lower()):
# write_string += "<"+hm_terminal[x.lower()].replace(" ","").upper()+">"+ "-"
# else:
# write_string += x+ "-"
# old_string = x
# write_string = write_string[:-1]
# write_string += ""
# if write_string not in tmp:
# tmp.append(write_string)
# statement_overall[statement] = tmp
#
# else:
# write_string =""
# old_string = ""
# for x in original_sentence[(first_pos - 2)/2:last_pos/2]:
# blub = old_string + " "+ x
# old_string = x
# if hm_terminal.has_key(blub.lower()):
# write_string += "<"+hm_terminal[blub.lower()].replace(" ","").upper()+">"+ "-"
#
# elif hm_terminal.has_key(x.lower()):
# write_string += "<"+hm_terminal[x.lower()].replace(" ","").upper()+">"+ "-"
# else:
# write_string += x+ "-"
# write_string = write_string[:-1]
# write_string += ""
# statement_overall[statement] = [write_string]
#
#
# #f_out.close()
#
## First only accept statements
# for key in statement_overall:
# write_string=""
# if len(statement_overall[key]) > 0 and "statement" in key.lower():
# write_string += key+","
# for x in statement_overall[key]:
# write_string += x+","
# write_string = write_string[:-1]
# while ", \"," in write_string:
# write_string = write_string.replace(", \",",",")
# write_string = write_string.replace("\", \",","\",")
# write_string = write_string.replace(": \",", ": ")
# write_string = write_string.replace(">: ,<",">: <")
# write_string = write_string.replace(">: ,",">: ")
# write_string = write_string.replace(",,",",")
#
#
#
# if write_string.endswith(": \""):
# pass
# else:
# write_string += "\n\n\n"
# f_out_overall.write(write_string)
#
# f_out_overall.close()
## print "Done"
#
# mapping("extracted_tmp",output_file,groundtruthFile)