forked from davidsbatista/BREDS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BREDS-single.py
executable file
·363 lines (307 loc) · 14.8 KB
/
BREDS-single.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "David S. Batista"
__email__ = "dsbatista@inesc-id.pt"
import cPickle
import sys
import os
import codecs
import operator
from numpy import dot
from gensim import matutils
from collections import defaultdict
from nltk.data import load
from BREDS.Seed import Seed
from BREDS.Pattern import Pattern
from BREDS.Config import Config
from BREDS.Tuple import Tuple
from BREDS.Sentence import Sentence
# usefull stuff for debugging
PRINT_TUPLES = False
PRINT_PATTERNS = True
class BREDS(object):
def __init__(self, config_file, seeds_file, negative_seeds, similarity, confidance):
self.curr_iteration = 0
self.patterns = list()
self.processed_tuples = list()
self.candidate_tuples = defaultdict(list)
self.config = Config(config_file, seeds_file, negative_seeds, similarity, confidance)
def generate_tuples(self, sentences_file):
"""
Generate tuples instances from a text file with sentences where named entities are already tagged
"""
try:
os.path.isfile("processed_tuples.pkl")
f = open("processed_tuples.pkl", "r")
print "\nLoading processed tuples from disk..."
self.processed_tuples = cPickle.load(f)
f.close()
print len(self.processed_tuples), "tuples loaded"
except IOError:
self.config.read_word2vec()
tagger = load('taggers/maxent_treebank_pos_tagger/english.pickle')
print "\nGenerating relationship instances from sentences"
f_sentences = codecs.open(sentences_file, encoding='utf-8')
count = 0
for line in f_sentences:
if line.startswith("#"):
continue
count += 1
if count % 10000 == 0:
sys.stdout.write(".")
sentence = Sentence(line.strip(), self.config.e1_type, self.config.e2_type, self.config.max_tokens_away,
self.config.min_tokens_away, self.config.context_window_size, tagger, self.config)
for rel in sentence.relationships:
t = Tuple(rel.e1, rel.e2, rel.sentence, rel.before, rel.between, rel.after, self.config)
self.processed_tuples.append(t)
f_sentences.close()
print "\n", len(self.processed_tuples), "tuples generated"
print "Writing generated tuples to disk"
f = open("processed_tuples.pkl", "wb")
cPickle.dump(self.processed_tuples, f)
f.close()
def similarity_3_contexts(self, p, t):
(bef, bet, aft) = (0, 0, 0)
if t.bef_vector is not None and p.bef_vector is not None:
bef = dot(matutils.unitvec(t.bef_vector), matutils.unitvec(p.bef_vector))
if t.bet_vector is not None and p.bet_vector is not None:
bet = dot(matutils.unitvec(t.bet_vector), matutils.unitvec(p.bet_vector))
if t.aft_vector is not None and p.aft_vector is not None:
aft = dot(matutils.unitvec(t.aft_vector), matutils.unitvec(p.aft_vector))
return self.config.alpha*bef + self.config.beta*bet + self.config.gamma*aft
def similarity_all(self, t, extraction_pattern):
"""
Cosine similarity between all patterns part of a Cluster/Extraction Pattern
and the vector of a ReVerb pattern extracted from a sentence
returns the max
"""
good = 0
bad = 0
max_similarity = 0
for p in list(extraction_pattern.tuples):
score = self.similarity_3_contexts(t, p)
if score > max_similarity:
max_similarity = score
if score >= self.config.threshold_similarity:
good += 1
else:
bad += 1
if good >= bad:
return True, max_similarity
else:
return False, 0.0
def match_seeds_tuples(self):
"""
checks if an extracted tuple matches seeds tuples
"""
matched_tuples = list()
count_matches = dict()
for t in self.processed_tuples:
for s in self.config.positive_seed_tuples:
if t.e1 == s.e1 and t.e2 == s.e2:
matched_tuples.append(t)
try:
count_matches[(t.e1, t.e2)] += 1
except KeyError:
count_matches[(t.e1, t.e2)] = 1
return count_matches, matched_tuples
def write_relationships_to_disk(self):
print "\nWriting extracted relationships to disk"
f_output = open("relationships.txt", "w")
tmp = sorted(self.candidate_tuples.keys(), reverse=True)
try:
for t in tmp:
f_output.write(
"instance: " + t.e1.encode("utf8") + '\t' + t.e2.encode("utf8") + '\tscore:' + str(t.confidence) +
'\n')
f_output.write("sentence: " + t.sentence.encode("utf8") + '\n')
f_output.write("pattern_bef: " + t.bef_words.encode("utf8") + '\n')
f_output.write("pattern_bet: " + t.bet_words.encode("utf8") + '\n')
f_output.write("pattern_aft: " + t.aft_words.encode("utf8") + '\n')
if t.passive_voice is False:
f_output.write("passive voice: False\n")
elif t.passive_voice is True:
f_output.write("passive voice: True\n")
f_output.write("\n")
f_output.close()
except Exception, e:
print e
sys.exit(1)
def init_bootstrapp(self, tuples):
"""
starts a bootstrap iteration
"""
if tuples is not None:
f = open(tuples, "r")
print "\nLoading processed tuples from disk..."
self.processed_tuples = cPickle.load(f)
f.close()
print len(self.processed_tuples), "tuples loaded"
self.curr_iteration = 0
while self.curr_iteration <= self.config.number_iterations:
print "=========================================="
print "\nStarting iteration", self.curr_iteration
print "\nLooking for seed matches of:"
for s in self.config.positive_seed_tuples:
print s.e1, '\t', s.e2
# Looks for sentences macthing the seed instances
count_matches, matched_tuples = self.match_seeds_tuples()
if len(matched_tuples) == 0:
print "\nNo seed matches found"
sys.exit(0)
else:
print "\nNumber of seed matches found"
sorted_counts = sorted(count_matches.items(), key=operator.itemgetter(1), reverse=True)
for t in sorted_counts:
print t[0][0], '\t', t[0][1], t[1]
print "\n", len(matched_tuples), "tuples matched"
# Cluster the matched instances: generate patterns/update patterns
print "\nClustering matched instances to generate patterns"
self.cluster_tuples(matched_tuples)
# Eliminate patterns supported by less than 'min_pattern_support' tuples
new_patterns = [p for p in self.patterns if len(p.tuples) > self.config.min_pattern_support]
self.patterns = new_patterns
print "\n", len(self.patterns), "patterns generated"
if PRINT_PATTERNS is True:
count = 1
print "\nPatterns:"
for p in self.patterns:
print count
for t in p.tuples:
print "BEF", t.bef_words
print "BET", t.bet_words
print "AFT", t.aft_words
print "========"
print "\n"
count += 1
if self.curr_iteration == 0 and len(self.patterns) == 0:
print "No patterns generated"
sys.exit(0)
# Look for sentences with occurrence of seeds semantic types (e.g., ORG - LOC)
# This was already collect and its stored in: self.processed_tuples
#
# Measure the similarity of each occurrence with each extraction pattern
# and store each pattern that has a similarity higher than a given threshold
#
# Each candidate tuple will then have a number of patterns that extracted it
# each with an associated degree of match.
print "Number of tuples to be analyzed:", len(self.processed_tuples)
print "\nCollecting instances based on extraction patterns"
count = 0
for t in self.processed_tuples:
count += 1
if count % 1000 == 0:
sys.stdout.write(".")
sys.stdout.flush()
sim_best = 0
for extraction_pattern in self.patterns:
accept, score = self.similarity_all(t, extraction_pattern)
if accept is True:
extraction_pattern.update_selectivity(t, self.config)
if score > sim_best:
sim_best = score
pattern_best = extraction_pattern
if sim_best >= self.config.threshold_similarity:
print t.e1, t.e2, sim_best, t.bet_words, pattern_best.id
# if this tuple was already extracted, check if this extraction pattern is already associated
# with it, if not, associate this pattern with it and similarity score
patterns = self.candidate_tuples[t]
if patterns is not None:
if pattern_best not in [x[0] for x in patterns]:
self.candidate_tuples[t].append((pattern_best, sim_best))
# If this tuple was not extracted before, associate this pattern with the instance
# and the similarity score
else:
self.candidate_tuples[t].append((pattern_best, sim_best))
# update all patterns confidence
for p in self.patterns:
p.update_confidence(self.config)
if PRINT_PATTERNS is True:
print "\nPatterns:"
for p in self.patterns:
for t in p.tuples:
print "BEF", t.bef_words
print "BET", t.bet_words
print "AFT", t.aft_words
print "========"
print "Positive", p.positive
print "Negative", p.negative
print "Unknown", p.unknown
print "Tuples", len(p.tuples)
print "Pattern Confidence", p.confidence
print "\n"
# update tuple confidence based on patterns confidence
print "\n\nCalculating tuples confidence"
for t in self.candidate_tuples.keys():
confidence = 1
t.confidence_old = t.confidence
for p in self.candidate_tuples.get(t):
confidence *= 1 - (p[0].confidence * p[1])
t.confidence = 1 - confidence
# sort tuples by confidence and print
if PRINT_TUPLES is True:
extracted_tuples = self.candidate_tuples.keys()
tuples_sorted = sorted(extracted_tuples, key=lambda tpl: tpl.confidence, reverse=True)
for t in tuples_sorted:
print t.sentence
print t.e1, t.e2
print t.confidence
print "\n"
print "Adding tuples to seed with confidence >=" + str(self.config.instance_confidance)
for t in self.candidate_tuples.keys():
if t.confidence >= self.config.instance_confidance:
seed = Seed(t.e1, t.e2)
self.config.positive_seed_tuples.add(seed)
# increment the number of iterations
self.curr_iteration += 1
self.write_relationships_to_disk()
def cluster_tuples(self, matched_tuples):
"""
Single-Pass Clustering
"""
# Initialize: if no patterns exist, first tuple goes to first cluster
if len(self.patterns) == 0:
c1 = Pattern(matched_tuples[0])
self.patterns.append(c1)
count = 0
for t in matched_tuples:
count += 1
if count % 1000 == 0:
sys.stdout.write(".")
sys.stdout.flush()
max_similarity = 0
max_similarity_cluster_index = 0
# go through all patterns(clusters of tuples) and find the one with the highest similarity score
for i in range(0, len(self.patterns), 1):
extraction_pattern = self.patterns[i]
accept, score = self.similarity_all(t, extraction_pattern)
if accept is True and score > max_similarity:
max_similarity = score
max_similarity_cluster_index = i
# if max_similarity < min_degree_match create a new cluster having this tuple as the centroid
if max_similarity < self.config.threshold_similarity:
c = Pattern(t)
self.patterns.append(c)
# if max_similarity >= min_degree_match add to the cluster with the highest similarity
else:
self.patterns[max_similarity_cluster_index].add_tuple(t)
def main():
if len(sys.argv) != 7:
print "\nBREDS.py parameters sentences positive_seeds negative_simties similarity confidance\n"
sys.exit(0)
else:
configuration = sys.argv[1]
sentences_file = sys.argv[2]
seeds_file = sys.argv[3]
negative_seeds = sys.argv[4]
similarity = sys.argv[5] # threshold similarity for clustering/extracting instances
confidance = sys.argv[6] # confidence threshold of an instance to used as seed
breads = BREDS(configuration, seeds_file, negative_seeds, float(similarity), float(confidance))
if sentences_file.endswith('.pkl'):
print "Loading pre-processed sentences", sentences_file
breads.init_bootstrapp(tuples=sentences_file)
else:
breads.generate_tuples(sentences_file)
breads.init_bootstrapp(tuples=None)
if __name__ == "__main__":
main()