-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathextract_tabular_data.py
More file actions
254 lines (236 loc) · 10.5 KB
/
Copy pathextract_tabular_data.py
File metadata and controls
254 lines (236 loc) · 10.5 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
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
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
import codecs
import re
import pandas as pd
import argparse
from collections import defaultdict
import sys
import os
# gather arguments
parser = argparse.ArgumentParser(
description="Extract tabular paradigms from annotated templates."
)
parser.add_argument(
"-candidates_dir",
action="store",
dest="candidates_dir",
help="Location of candidate html pages.",
)
parser.add_argument(
"-annotation_dir",
action="store",
dest="annotation_dir",
help="Location of raw/annotated table templates.",
)
parser.add_argument(
"-language", action="store", dest="language", help="Language to grab."
)
args = parser.parse_args()
# regular expressions
lempat = r"<h1.*?>(.*?)</h1>"
locpat1 = r"</h2>.*?</h2>"
locpat2 = r"</h2>.*?</body>"
pospat = r">(.*?)</h3>"
# input tables
orig_dir = os.path.join(
args.annotation_dir, "raw_tables/"
) # original example tables for comparison #CHANGE
done_dir = os.path.join(
args.annotation_dir, "annotated_tables/"
) # annotated example tables #CHANGE
# output directory
out_dir = "./tabular_results/" # output data goes here
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# language
language = args.language
# output file
fout_name = out_dir + language + "_tabular_paradigms.txt"
fout = codecs.open(fout_name, "wb", "utf-8")
# get the table patterns
n_tables = {}
v_tables = {}
adj_tables = {}
# loop through annotated directory
for n in os.listdir(done_dir + language):
if n.endswith("example.csv"):
mod_set = {}
try:
odata = pd.read_csv(orig_dir + language + "/" + n, dtype="unicode").fillna(
""
)
ddata = pd.read_csv(done_dir + language + "/" + n, dtype="unicode").fillna(
""
)
except:
print(n)
raise
try:
assert odata.shape == ddata.shape # make sure we really got the same table
except:
print(n)
print(odata)
print(ddata)
print(odata.shape)
print(ddata.shape)
raise
for i in range(odata.shape[0]):
for j in range(odata.shape[1]):
if odata.iloc[i, j] != ddata.iloc[i, j]:
mod_set[(i, j)] = ddata.iloc[i, j]
# if there were some actual annotations store them
if len(mod_set) > 0:
if n.startswith("N_"):
n_tables[odata.shape] = mod_set
if n.startswith("ADJ_"):
adj_tables[odata.shape] = mod_set
if n.startswith("V_"):
v_tables[odata.shape] = mod_set
# #loop through languages
lnames = os.listdir(args.candidates_dir) # CHANGE
for ln in lnames:
if ln == language:
names = os.listdir(os.path.join(args.candidates_dir, ln)) # CHANGE
# loop through language pages
# count = 0
for n in names:
# if n.startswith('candidate_33623.html'):
if n.startswith("candidate"):
fin = codecs.open(
os.path.join(args.candidates_dir, ln, n), "rb", "utf-8"
) # CHANGE
page = fin.read().replace("<br>", "|")
fin.close()
# get the lemma from the page
match = re.search(lempat, page, flags=re.U | re.DOTALL)
if match:
lemma = match.group(1)
# print lemma
# adjectives
match = re.search(ln + locpat1, page, flags=re.U | re.DOTALL)
if not match:
match = re.search(ln + locpat2, page, flags=re.U | re.DOTALL)
if match:
text = match.group()
if u"Adjective</h3" in text:
try:
data = pd.read_html(text)
if len(data) >= 1:
data = pd.concat(data)
shape = data.shape
if shape in adj_tables:
for mod, feats in adj_tables[shape].items():
word = data.iloc[mod[0], mod[1]]
if not pd.isnull(word):
fout.write(
lemma
+ "\t"
+ word
+ "\t"
+ feats
+ "\n"
)
fout.write("\n")
except:
# if data.shape == (6,8):
# print data
# raise
fout.write("----\t----\t----\n")
fout.write("\n")
pass
# nouns
match = re.search(ln + locpat1, page, flags=re.U | re.DOTALL)
if not match:
match = re.search(ln + locpat2, page, flags=re.U | re.DOTALL)
if match:
text = match.group()
if u"Noun</h3>" in text:
try:
data = pd.read_html(text)
if len(data) >= 1:
data = pd.concat(data)
shape = data.shape
# SOME RUSSIAN HACKING
if language == "Russian" and lemma == u"дом":
print(data)
print(shape)
print(n)
# END RUSSIAN HACKING
# SOME ARMENIAN HACKING
if language == "Armenian" and shape == (22, 3):
if data.iloc[1, 1] == "singular":
shape = (23, 3)
# if 'Audio' in data.iloc[]
# SOME HUNGARIAN HACKING
if language == "Hungarian":
if data.iloc[2, 1] == "singular":
shape = (30, 3)
# END ARMENIAN HACKING
if shape in n_tables:
for mod, feats in n_tables[shape].items():
word = data.iloc[mod[0], mod[1]]
# TEMPORARY ARMENIAN HACK
# if word == 'plural':
# print data.iloc[1,1]
# print data.shape
# END TEMPORARY ARMENIAN HACK
# data.to_csv('armenian_tmp.csv',encoding='utf-8',index=False)
if not pd.isnull(word):
fout.write(
lemma
+ "\t"
+ word
+ "\t"
+ feats
+ "\n"
)
fout.write("\n")
except:
# raise
fout.write("----\t----\t----\n")
fout.write("\n")
# raise
pass
# verbs
match = re.search(ln + locpat1, page, flags=re.U | re.DOTALL)
if not match:
match = re.search(ln + locpat2, page, flags=re.U | re.DOTALL)
if match:
text = match.group()
if u"Verb</h3>" in text:
try:
data = pd.read_html(text)
if len(data) >= 1:
data = pd.concat(data)
shape = data.shape
# SOME RUSSIAN HACKING
if language == "Russian" and lemma == u"дом":
print(data)
print(shape)
print(n)
# END RUSSIAN HACKING
# SPANISH HACK START
if language == "Spanish" and lemma == "hablar":
shape = (23, 8)
# SPANISH HACK END
if shape in v_tables:
for mod, feats in v_tables[shape].items():
word = data.iloc[mod[0], mod[1]]
if not pd.isnull(word):
fout.write(
lemma
+ "\t"
+ word
+ "\t"
+ feats
+ "\n"
)
fout.write("\n")
except:
# print feats
fout.write("----\t----\t----\n")
fout.write("\n")
# raise
pass
# clean up
fout.close()