-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprocess_data.py
179 lines (152 loc) · 4.92 KB
/
process_data.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
"""
debug process pipeline
"""
import sys
import pickle
import re
from nltk import sent_tokenize
from io import StringIO
import networkx as nx
from data import *
from utils import *
from distant_superv import *
#import stanfordnlp
#nlp = stanfordnlp.Pipeline()
"""
if stanford nlp
"""
#batch_id = sys.argv[1]
batch_id = 0
def GetDeps(dep_lines):
special = ['-LRB-', "-RRB-"]
tups = []
words = []
try:
for t in dep_lines:
arc = t.split("(")[0]
tokens = t.split("(")[-1].split(")")[:-1][0].split(",")
if arc =="punct" or arc=="root":
continue
if len(tokens) >2:
src = tokens[0].strip()
tgt = ','+tokens[-1].strip()
else:
src = tokens[0].strip()
tgt = tokens[1].strip()
if "-LRB-" in src:
#print("src ", src)
src = src.replace("-LRB-", "LRB")
if "-RRB-" in src:
#print("src ", src)
src = src.replace("-RRB-", "RRB")
if "-RRB-" in tgt:
#print("tgt ", tgt)
tgt = tgt.replace("-RRB-", "RRB")
if "-LRB-" in tgt:
#print("tgt ", tgt)
tgt = tgt.replace("-LRB-", "LRB")
for i in [src, tgt]:
if i not in words and i != '':
i = i.split("-")[0] + "-"+re.sub("[^0-9]", "", i.split("-")[1])
#print(i)
if i != "-":
words.append(i)
if src == '' or tgt == '':
continue
if src == '_' or tgt == "_":
continue
else:
#print("new src", src)
#print("new tgt", tgt)
newsrc = src.split("-")[0] + "-"+re.sub("[^0-9]", "", src.split("-")[1])
newtgt = tgt.split("-")[0] + "-"+re.sub("[^0-9]", "", tgt.split("-")[1])
tups.append((newsrc, newtgt, arc))
except:
return False, False
return tups, words
sents = open("data/clean_orig_sent_"+str(batch_id)+".txt").readlines()
orig_sents = [s.strip() for s in sents]
golds = open("data/clean_gold_sent_"+str(batch_id)+".txt").readlines()
gold_sents = [s.strip() for s in golds]
deplines = open("data/clean_orig_sent_"+str(batch_id)+".txt.out").readlines()
locs = [ind for ind, value in enumerate(deplines) if "Dependency Parse (enhanced plus plus dependencies):\n" in value]
sent_locs = [ind for ind, value in enumerate(deplines) if "Sentence #" in value]
output_arcs = {}
#for _ in range(0, len(sents)):
for _ in range(4734,4734+1):
loc = locs[_]
if _ == len(sents) -1:
dep_lines = deplines[loc:]
else:
dep_lines = deplines[loc+1:sent_locs[_+1]]
tmp, words = GetDeps(dep_lines)
if tmp == False and words ==False:
print("Skipping problematic sentence because of token errors", _)
continue
try:
output_arcs[_] = {}
vtov, vtoi = wordict(words)
itov = {v:k for k,v in vtoi.items()}
adjs, conn_adjs = BuildAdj(vtoi, tmp) # connected nodes by neighbor, adj: nodes connected by dependency arcs
# Get all pairs of src, tgt, arcs
all_pairs = BuildPairs(adjs)
# Add self arc to indicate if a word being dropped
all_pairs = AddSelf(itov, all_pairs)
#tmp = "Sokuhi was born in Fuzhou , Fujian , China. Sokuhi was ordained at 17 by Feiyin Tongrong."
g_temp = gold_sents[_]
golds_out = sent_tokenize(g_temp)
golds_out = [i.replace("-LRB-","LRB").replace("-RRB-","RRB") for i in golds_out]
s = orig_sents[_].replace("-LRB-","LRB").replace("-RRB-","RRB")
where_it_from, new_temp, cut_temp, del_temp = KEEPORDROP(all_pairs, golds_out, itov, s)
#new_temp, cut_temp, del_temp = KEEP(all_pairs, golds, itov, s)
copy_temp = COPY(all_pairs, golds, itov,where_it_from)
except:
print("Skipping problematic sentence because of token errors", _)
continue
if len(new_temp) == 0 :
print("skipping sentence at index ", _ )
continue
else:
output_arcs[_]['accept'], output_arcs[_]['copy'], output_arcs[_]['break'], output_arcs[_]['drop'] = new_temp, copy_temp, cut_temp, del_temp
output_arcs[_]['adjs'], output_arcs[_]['all_pairs'] = adjs, all_pairs
output_arcs[_]['words'] = words
output_arcs[_]['vtoi'], output_arcs[_]['itov'] = vtoi, itov
output_arcs[_]['golds'] = golds_out
"""
with open('data/batch_'+str(batch_id)+'.pkl', 'wb') as handle:
with open('data/sent_18.pkl', 'wb') as handle:
pickle.dump(output_arcs, handle, protocol=pickle.HIGHEST_PROTOCOL)
"""
"""
if spacy
"""
#nlp = spacy.load("en_core_web_sm")
#doc = nlp("Apple is looking at buying U.K. startup for $1 billion")
# for token in doc:
# print(token.text, token.lemma_, token.pos_, token.tag_, token.dep_,
# token.shape_, token.is_alpha, token.is_stop)
"""
import spacy
nlp = spacy.load("en_core_web_sm")
def spacy_deps(doc):
tups = []
for tki, token in enumerate(doc):
dep = token.text +"_"+str(tki)
head = token.head.text+"_" +str(token.head.i)
arc = token.dep_
tups.append((dep, head, arc))
return tups
def AddEdges(depG, deps):
for tup in deps:
# dependent, head, arc
depG.add_edge(tup[0], tup[1], label=tup[2])
deps = []
for sent in orig_sents:
doc = nlp(sent)
orig_dep = spacy_deps(doc)
deps.append(orig_dep)
"""
#tmpG = nx.DiGraph(directed=False)
#AddEdges(tmpG, tmp, vtoi)
#A = AdjGraph(tmpG)
#e = (2,3,{'weight':7})