-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessing.py
395 lines (333 loc) · 11.7 KB
/
preprocessing.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import os
import sys
import re
import queue
from bs4 import BeautifulSoup
import bs4
import copy
from shutil import copyfile
import torch
#from model import DomTreeLSTM
from tree import Tree
from nltk import word_tokenize
from vocab import Vocab
def tag_visible(element):
if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
return False
if isinstance(element, Comment):
return False
return True
def text_from_html(body):
soup = BeautifulSoup(body, 'html.parser')
texts = soup.findAll(text=True)
visible_texts = filter(tag_visible, texts)
return u" ".join(t.strip() for t in visible_texts)
def dfs(root):
if root is not None:
print(root.words)
for child in root.children:
dfs(child)
def clean_tree(root):
unhandled_children=queue.Queue()
for child in root.children:
unhandled_children.put(child)
handled_children=[]
while unhandled_children.qsize()>0:
child=unhandled_children.get() # not processed
if len(child.words)>0:
handled_children.append(child) # it is okay to keep
else:
for grandchild in child.children:
unhandled_children.put(grandchild)
root.clear_children()
#root.children=handled_children
for child in handled_children:
root.add_child(child)
for child in root.children:
clean_tree(child)
return root
def build_tree(soup):
q_text=queue.Queue()
q_tree=queue.Queue()
if soup.name is None:
return None
root= Tree(words=str(soup.string).lower().split(' '))
q_text.put(soup)
q_tree.put(root)
while q_text.qsize()>0:
current_dom_node=q_text.get()
current_tree_node=q_tree.get()
if not isinstance(current_dom_node, bs4.element.NavigableString) and len(list(current_dom_node.children))>0 :
for child in current_dom_node.children:
block_words=[]
if child.string is not None and not (child.parent.string== child.string):
print(type(child))
new_child=Tree(words=block_words)
current_tree_node.add_child(new_child)
q_text.put(child)
q_tree.put(new_child)
return root
#printprint(root.depth())
#dfs(root)
def generate_line_reprensentation(root):
block_texts=[]
parent_idx=[]
q_tree=queue.Queue()
q_tree.put(root)
idx=0
while q_tree.qsize()>0:
idx+=1
node=q_tree.get()
node.idx=idx
block_texts.append(" ".join(node.words))
for child in node.children:
q_tree.put(child)
parent_idx=[0]*idx
q_tree.put(root)
#print(idx)
while q_tree.qsize()>0:
node=q_tree.get()
parent_idx[node.idx-1]=0 # assume it is root node
if node.parent is not None:
parent_idx[node.idx-1]=node.parent.idx
for child in node.children:
q_tree.put(child)
return parent_idx,block_texts
def length_compare():
dirs=os.listdir('htmls/faculty')
for dir in dirs:
if dir.find('Jessica.K.Hodgins.html') > -1:
path=os.path.join('plain-data-c-5/faculty',dir)
break
text1=open('tree-text.txt','r',encoding='latin-1').read()
print(text1)
return 0
text1=text1.replace('\n',' ')
text1=re.sub(' +',' ',text1)
text2=open(path+'.txt','r').read()
text2=text1.replace('\n',' ')
text2=re.sub(' +',' ',text2)
#print(len(text2.split(' ')))
#print(len(text1.split(' ')))
def get_soup(path):
html=open(path,'r',encoding='latin-1').read()
soup = BeautifulSoup(html,"html5lib")
return soup
def htmls2trees():
path='data/raw-data/train/'
all_cate=os.listdir(path)
idx=-1
fp=open('data/train/parents.txt','w',encoding='latin-1')
ft=open('data/train/texts.txt','w',encoding='latin-1')
fl=open('data/train/labels.txt','w')
for cate in all_cate:
dirs=os.listdir(os.path.join(path,cate))
if len(dirs)>=0:
idx+=1
print("Processing {}, cate ID {}".format(cate,idx))
for fn in dirs:
html_path=os.path.join(path,cate,fn)
#print(html_path)
soup=get_soup(html_path)
tree=build_tree(soup)
tree=clean_tree(tree)
#print(tree.depth())
parents_idx,block_text=generate_line_reprensentation(tree)
block_str="|||".join(block_text)+'\n'
fp.write(" ".join(map(str,parents_idx))+"\n")
ft.write(block_str)
fl.write(str(idx)+'\n')
fp.close()
ft.close()
fl.close()
path='data/raw-data/val/'
all_cate=os.listdir(path)
idx=-1
fp=open('data/val/parents.txt','w',encoding='latin-1')
ft=open('data/val/texts.txt','w',encoding='latin-1')
fl=open('data/val/labels.txt','w')
for cate in all_cate:
dirs=os.listdir(os.path.join(path,cate))
if len(dirs)>=0:
idx+=1
print("Processing {}, cate ID {}".format(cate,idx))
for fn in dirs:
html_path=os.path.join(path,cate,fn)
print(html_path)
soup=get_soup(html_path)
tree=build_tree(soup)
tree=clean_tree(tree)
#print(tree.depth())
parents_idx,block_text=generate_line_reprensentation(tree)
fp.write(" ".join(map(str,parents_idx))+"\n")
ft.write("|||".join(block_text)+'\n')
fl.write(str(idx)+'\n')
fp.close()
ft.close()
fl.close()
def getvocab():
lines1=open('data/train/texts.txt','r',encoding='latin-1').readlines()
lines2=open('data/val/texts.txt','r',encoding='latin-1').readlines()
lines=lines1+lines2
lines= [line.split('|||') for line in lines]
word_set=set()
for line in lines:
for block in line:
words=block.split(' ')
for word in words:
word_set.add(word)
others=['<blank>','<unk>','<s>','</s>']
for word in others:
if word not in word_set:
word_set.add(word)
print(len(word_set))
f=open('data/vocab.txt','w',encoding='latin-1')
for word in word_set:
f.write(word+'\n')
'''
none---[]--[]
---[]-date,tue...
-jessica k, hodgins,information,page...-[]
-[]
-wa0
-[]-wa1
-[]
-[]
-[]-graphics visualizatio
-jessica k hodgins
-[]
-[]
-[]
-[wa2]
-[]
-[]-jessica is an assitant prof..
-wa3...
-the leg lab...
-at the mit lab
-wa4
-computer animation
-by using comtrol alg
-[]
-[]
-contact information..-[]
-[]
-jessica k hodgins-[]
-[]
-graphics visual
-[]
-college of computing
-[]
-801 atlantic dirve
-[]
-georgia institue of tech
-[]
-atlanta ga 303332
-[]
-404 894-..
-[]
-email ..
-[]
-wa5
-[]
-[]
-[]
'''
def load_word_vectors(path):
if os.path.isfile(path+'.pth') and os.path.isfile(path+'.vocab'):
print('==> File found, loading to memory')
vectors = torch.load(path+'.pth')
vocab = Vocab(filename=path+'.vocab')
return vocab, vectors
# saved file not found, read from txt file
# and create tensors for word vectors
print('==> File not found, preparing, be patient')
count = sum(1 for line in open(path+'.txt',encoding='latin-1'))
with open(path+'.txt','r') as f:
contents = f.readline().rstrip('\n').split(' ')
dim = len(contents[1:])
words = [None]*(count)
vectors = torch.zeros(count,dim)
with open(path+'.txt','r',encoding='latin-1') as f:
idx = 0
for line in f:
contents = line.rstrip('\n').split(' ')
words[idx] = contents[0]
#print(contents[1:])
vectors[idx] = torch.Tensor(list(map(float, contents[1:])))
idx += 1
with open(path+'.vocab','w',encoding='latin-1') as f:
for word in words:
f.write(word+'\n')
vocab = Vocab(filename=path+'.vocab')
torch.save(vectors, path+'.pth')
return vocab, vectors
def getembd():
vocab_file='data/vocab.txt'
vocab = Vocab(filename=vocab_file)
emb_file = os.path.join('data/', 'webkbb_embed.pth')
if os.path.isfile(emb_file):
emb = torch.load(emb_file)
print(emb.size())
else:
# load glove embeddings and vocab
glove_vocab, glove_emb = load_word_vectors(os.path.join('data/glove','glove.6B.200d'))
print('==> GLOVE vocabulary size: %d ' % glove_vocab.size())
emb = torch.zeros(vocab.size(),glove_emb.size(1))
for word in vocab.labelToIdx.keys():
if glove_vocab.getIndex(word):
emb[vocab.getIndex(word)] = glove_emb[glove_vocab.getIndex(word)]
else:
emb[vocab.getIndex(word)] = torch.Tensor(emb[vocab.getIndex(word)].size()).normal_(-0.05,0.05)
torch.save(emb, emb_file)
is_preprocessing_data = True # flag to quit
print('done creating emb, quit')
def test():
#print('testing')
dirs=os.listdir('htmls/faculty')
for dir in dirs:
if dir.find('Jessica.K.Hodgins.html') > -1:
path=os.path.join('htmls/faculty',dir)
break
tree=build_tree(get_soup(path))
print(tree.depth())
#print(tree.children[0].children[1].children[19].children[0].words)
#print(tree.depth())
#tree=clean_tree(tree)
#print(tree.depth())
#dfs(tree)
#parent_idx,block_text=generate_line_reprensentation(tree)
#dfs(tree)
#print(len(parent_idx))
#print(block_text)
#dfs(tree)
#vocab_size=10000
#in_dim=300
#mem_dim=100
#hidden_dim=200
#num_classes=5
#sparsity=True
#freeze=False
#sent=['', 'by', 'using', 'control', 'algorithms', 'in', 'combination', 'with', 'physically', 'realistic', 'simulation.', 'in', '1994', 'she', 'received', 'a', 'nsf', 'young', 'investigator', 'award', 'and', 'a', 'packard', 'fellowship.', 'in', '1995', 'she', 'received', 'a', 'sloan', 'fellowship.', 'her', 'recent', 'research', 'funding', 'has', 'been', 'from', 'the', 'national', 'science', 'foundation,', 'the', 'jet', 'propulsion', 'laboratory,', 'and', 'the','mitsubishi', 'electric', 'research', 'laboratory.', '']
#model=DomTreeLSTM(vocab_size, in_dim, mem_dim, hidden_dim, num_classes, sparsity, freeze)
#s=model.forward(tree,sent)
#print('testing')
return 0
def train_val_copy():
source_base='data/htmls/'
cate_names=os.listdir(source_base)
for cate_name in cate_names:
fns=os.listdir(os.path.join(source_base,cate_name))
train_dirs=fns[0:int(len(fns)*0.8):]
dest=os.path.join('data/raw/train/',cate_name)
if not os.path.exists(dest):
os.makedirs(dest)
for fn in train_dirs:
copyfile(os.path.join(source_base,cate_name,fn),os.path.join(dest,fn))
if __name__=='__main__':
#getembd()
#train_val_copy()
#main()
#test()
#htmls2trees()
#getvocab()
#length_compare()