-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
293 lines (241 loc) · 8.44 KB
/
utils.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
from collections import defaultdict
import codecs
import numpy as np
import torch
from copy import copy
from parsivar import Tokenizer, Normalizer, FindChunks, POSTagger
from parsivar import FindStems
import re
my_normalizer = Normalizer()
my_tokenizer = Tokenizer()
my_stemmer = FindStems()
my_tagger = POSTagger(tagging_model="wapiti")
my_chunker = FindChunks()
stop_words = "\n".join(sorted(list(
set([my_normalizer.normalize(w) for w in codecs.open('stopwords/nonverbal', encoding='utf-8').read().split('\n') if w]))))
def build_graph(matrix):
graph = defaultdict(list)
for idx in range(0, len(matrix)):
for col in range(idx + 1, len(matrix)):
graph[idx].append((col, matrix[idx][col]))
return graph
def BFS(s, end, graph, max_size=-1, black_list_relation=[]):
visited = [False] * (max(graph.keys()) + 100)
# Create a queue for BFS
queue = []
# Mark the source node as
# visited and enqueue it
queue.append((s, [(s, 0)]))
found_paths = []
visited[s] = True
while queue:
s, path = queue.pop(0)
# Get all adjacent vertices of the
# dequeued vertex s. If a adjacent
# has not been visited, then mark it
# visited and enqueue it
for i, conf in graph[s]:
if i == end:
found_paths.append(path + [(i, conf)])
break
if visited[i] == False:
queue.append((i, copy(path) + [(i, conf)]))
visited[i] = True
candidate_facts = []
for path_pairs in found_paths:
# if len(path_pairs) < 3: #####
# continue
path = []
cum_conf = 0
for (node, conf) in path_pairs:
path.append(node)
cum_conf += conf
# if path[1] in black_list_relation:
# continue
candidate_facts.append((path, cum_conf))
candidate_facts = sorted(candidate_facts, key=lambda x: x[1], reverse=True)
return candidate_facts
def create_mapping(sentence, return_pt=False, tokenizer=None):
'''Create a mapping
tokenizer: huggingface tokenizer
'''
# for ch in "1234567890&;#$()*,-./:[]«»؛،؟۰۱۲۳۴۵۶۷۸۹!":
# sentence = sentence.replace(ch, "")
text = sentence
normalized_text = my_normalizer.normalize(text)
# print("text: ", normalized_text)
text = normalized_text.replace("", " ")
tokens = my_tokenizer.tokenize_words(text)
# print("tokens: ", tokens)
# for token in tokens:
# token = my_stemmer.convert_to_stem(token)
# print(tokens)
# print("text :", text)
text_tags = my_tagger.parse(my_tokenizer.tokenize_words(text))
chunks = my_chunker.chunk_sentence(text_tags)
result = my_chunker.convert_nestedtree2rawstring(chunks)
# print("tagged result", result)
# print("results before del puncs: ", result)
for ch in ",-.«»؛،؟۰!":
result = result.replace(ch, "")
# print("results after del puncs: ", result)
# ###################################################3
# verbs
result_items_v = []
verbs = []
s_v = ""
i_v = 0
while i_v < len(result):
if result[i_v] != "[":
i_v += 1
else:
while result[i_v] != "]":
i_v += 1
s_v += result[i_v]
result_items_v.append(s_v.strip(" ]"))
s_v = ""
# print("result_items_v", result_items_v)
for a in result_items_v:
if a.find("V") != -1:
# print("A", a)
for ch in "VPND":
a = a.replace(ch, "")
verbs.append(a.strip(" "))
# for i in range(len(verbs)):
# tokens1 = verbs[i].split(" ")
# tokens_filtered = [word for word in tokens1 if word not in stop_words]
# verbs[i] = " ".join(tokens_filtered)
# ##################################################
for ch in "VPND":
result = result.replace(ch, "")
result_items = []
s = ""
i = 0
while i < len(result):
if result[i] != "[":
i += 1
else:
while result[i] != "]":
i += 1
s += result[i]
result_items.append(s.strip(" ]"))
s = ""
# print("result_items", result_items)
# print("results before del nonverbals: ", result_items)
for i in range(len(result_items)):
if result_items[i] in verbs:
continue
tokens1 = result_items[i].split(" ")
tokens_filtered = [word for word in tokens1 if word not in stop_words]
result_items[i] = " ".join(tokens_filtered)
# print("results after del nonverbals: ", result_items)
for a in result_items:
if a == '':
result_items.remove('')
elif a == ' ':
result_items.remove(' ')
# print([(idx, token) for idx, token in enumerate(tokens)])
# print("result_items", result_items)
doc = []
first_tokens = []
size_tokens = []
for a in result_items:
chunk_tokens = a.split(" ")
first_tokens.append(chunk_tokens[0])
size_tokens.append(len(chunk_tokens))
j = 0
len_tokens = len(tokens)
# print("first_tokens", first_tokens)
# print("tokens")
for i in range(len(first_tokens)):
while j < len_tokens and first_tokens[i] != tokens[j]:
j += 1
if j == len_tokens:
break
context = dict()
context["word"] = result_items[i]
context["start"] = j
context["end"] = j + size_tokens[i]
doc.append(context)
# print("doc", doc)
chunk2id = {}
start_chunk = []
end_chunk = []
noun_chunks = []
ner_ranges = list()
for chunk in doc:
noun_chunks.append(chunk["word"])
start_chunk.append(chunk["start"])
end_chunk.append(chunk["end"])
sentence_mapping = []
token2id = {}
mode = 0 # 1 in chunk, 0 not in chunk
chunk_id = 0
# print("verbs", verbs)
# print("noun_chunks:", noun_chunks)
for idx, token in enumerate(tokens):
# print("idx", idx)
# print("token", token)
# print(token.text)
if idx in start_chunk:
# print("noun_chunks[chunk_id]", noun_chunks[chunk_id])
mode = 1
sentence_mapping.append(noun_chunks[chunk_id])
token2id[sentence_mapping[-1]] = len(token2id)
chunk_id += 1
elif idx in end_chunk:
mode = 0
if mode == 0:
# sentence_mapping.append(token.text)
sentence_mapping.append(tokens[idx])
token2id[sentence_mapping[-1]] = len(token2id)
# print("token2id", token2id)
token_ids = []
tokenid2word_mapping = []
for token in sentence_mapping:
subtoken_ids = tokenizer(str(token), add_special_tokens=False)['input_ids']
# print("token before tokenizer", token)
# print("subtoken_ids", subtoken_ids)
tokenid2word_mapping += [token2id[token]] * len(subtoken_ids)
token_ids += subtoken_ids
# print(tokenid2word_mapping)
tokenizer_name = str(tokenizer.__str__)
outputs = {
'input_ids': [tokenizer.cls_token_id] + token_ids + [tokenizer.sep_token_id],
'attention_mask': [1] * (len(token_ids) + 2),
'token_type_ids': [0] * (len(token_ids) + 2)
}
if return_pt:
for key, value in outputs.items():
outputs[key] = torch.from_numpy(np.array(value)).long().unsqueeze(0)
return verbs, outputs, tokenid2word_mapping, token2id, noun_chunks
# return outputs, tokenid2word_mapping, token2id, noun_chunks, linked_entities
def compress_attention(attention, tokenid2word_mapping, operator=np.mean):
new_index = []
prev = -1
for idx, row in enumerate(attention):
token_id = tokenid2word_mapping[idx]
if token_id != prev:
new_index.append([row])
prev = token_id
else:
new_index[-1].append(row)
new_matrix = []
for row in new_index:
new_matrix.append(operator(np.array(row), 0))
new_matrix = np.array(new_matrix)
attention = np.array(new_matrix).T
prev = -1
new_index = []
for idx, row in enumerate(attention):
token_id = tokenid2word_mapping[idx]
if token_id != prev:
new_index.append([row])
prev = token_id
else:
new_index[-1].append(row)
new_matrix = []
for row in new_index:
new_matrix.append(operator(np.array(row), 0))
new_matrix = np.array(new_matrix)
return new_matrix.T