-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpredict.py
More file actions
577 lines (507 loc) · 17.3 KB
/
predict.py
File metadata and controls
577 lines (507 loc) · 17.3 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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
# predict.py
# generate crfpp input for input file
# input: preprocessed files (word, pos, chunktag, dependency, etc)
# Yiping 2013
import os
from multiprocessing import Pool
import re
#import nltk
import math
import random
import sys
import getopt
unigram_lm = {}
#keyphrase dictionary
patterns = []
#negative feature: containing pronoun
pronouns = []
#ngrams from wcl positive corpus
_4grams = []
_5grams = []
_6grams = []
_7grams = []
_8grams = []
capitalizedLetter = re.compile('[A-Z]')
digit = re.compile('[0-9]')
OUT_TAG = "?"
def Map(data):
''' process a single sentence and extract features for each word
Args:
data[0]:word
data[1]:pos
data[2]:chunktag
data[3]:shallowParse sequence
data[4]:NE tag
data[5]:dependency parent
data[6]:dependency parent type
data[7]:dependency path to root
Return:
dictionary[]: each dictionary contains the features for that word
'''
#initialize results
results = []
line = data[0]
words = line.split(" ")
#whether the sentence contains pronouns
has_pronoun = 0
for pronoun in pronouns:
pronoun = pronoun.strip()
matchStr = "\W?"+pronoun+"\W"
match = re.compile(matchStr)
if(len(match.findall(line))>0):
has_pronoun = 1
#has i.e.
has_ie = '0'
match = re.compile('i\.e\..{5,100}')
if(len(match.findall(line))>0):
has_ie = '1'
pattern_type = -1
pattern_index = -1
#determine whether the sentence contains a surface pattern
pattern_lookup = ['0']*11 #initialize as not present for every pattern
for i in range(len(patterns)):
pattern = patterns[i]
#process the regular expression
#find all the place holders
pattern = pattern.replace("\n","",1)
#determine the type of pattern
#type 1: <term> <pattern> <definition>
#type 2: <definition> <pattern> <term>
#ptype = pattern[1:2]
ptype = str(i)
splitter = re.compile('\s?<.*?>,?\s?')
segments = splitter.split(pattern)
#the matchStr to look for
matchStr ='.{2,50} '
for segment in segments:
# to avoid the special cases where single space, comma or some
# other characters are parsed as a segment
if (len(segment)>=2 or (len(segment)==1 and segment.count('1')== 0)):
matchStr += segment +' .{2,80} '
match = re.compile(matchStr)
occurances = match.findall(line)
if(len(occurances)>0):
#print(occurance)
#pattern_type = ptype
#pattern_index = len(nltk.word_tokenize(line[:line.index(segments[2])]))
pattern_lookup[i] = '1'
#end for i in range
if(len(words)>=1):
first_word = words[0].lower()
else:
first_word = "EMPTY"
sentence_len = len(words)
postags = data[1].split(" ")
shallowtags = data[2].split(" ")
ne_tags = data[4].split(" ")
parents = data[5].split(" ")
ptypes = data[6].split(" ")
dep_paths = data[7].split(" ")
#####################################
##Shallow parsing sequence features##
parse_str = data[3]
short_str = parse_str.replace("ADVP","")
short_str = short_str.replace("ADJP","")
short_str = short_str.strip()
#short_list = short_str.split(' ')
#print(parse_str)
starts_with_NP = '0'
starts_with_ADVP = '0'
starts_with_PPNP = '0'
starts_with_DTNP = '0'
# NP : NP
has_NPiNP = '0'
# NP is
has_NPIS = '0'
# NP is * NP
has_NPISNP = '0'
# refer(s) to * NP
has_refer_to = '0'
# NP is * NP + of/that/which/for/and/PUNCTUATION
has_NPISNPPP = '0'
# NP or NP
has_NPorNP = '0'
# known as * NP
has_known_as = '0'
# NP of * NP
has_NPofNP = '0'
# NP ( NP )
has_NPBRNPBR = '0'
# NP a NP
has_NPaNP = '0'
has_NPconsistNP = '0'
has_NPdefineNP = '0'
#look up patterns appeared in wcl corpus
in_4gram = '0'
in_5gram = '0'
in_6gram = '0'
in_7gram = '0'
in_8gram = '0'
if(short_str.startswith('NP')):
starts_with_NP = '1'
if(parse_str.startswith('ADVP') and 'NP' in parse_str[:10]):
starts_with_ADVP = '1'
if(short_str.startswith('PP NP')):
starts_with_PPNP = '1'
if(short_str.startswith('the NP') or short_str.startswith('a NP') or short_str.startswith('an NP')):
starts_with_DTNP = '1'
regex = re.compile("NP (is|are|was|were) ")
if(len(regex.findall(short_str))>0):
has_NPIS = '1'
regex = re.compile("NP : (the |The |a |A |an |An )?NP")
if(len(regex.findall(short_str))>0):
has_NPiNP = '1'
regex = re.compile("NP (is|are|was|were) (the |a |any |some|an )?NP")
if(len(regex.findall(short_str))>0):
has_NPISNP = '1'
regex = re.compile("(refer|refers) to (the |The |a |A |an |An )?NP")
if(len(regex.findall(short_str))>0):
has_refer_to = '1'
regex = re.compile("NP (is|are|was|were) (the |a |any |some|an )?NP (of|that|which|for)")
if(len(regex.findall(short_str))>0):
has_NPISNPPP = '1'
regex = re.compile("NP (or|,) (the )?NP")
if(len(regex.findall(short_str))>0):
has_NPorNP = '1'
regex = re.compile(" known as (the )?NP")
if(len(regex.findall(short_str))>0):
has_known_as = '1'
regex = re.compile("NP of (the |a )?NP")
if(len(regex.findall(short_str))>0):
has_NPofNP = '1'
regex = re.compile("NP \( .{0,5}NP \)")
if(len(regex.findall(short_str))>0):
has_NPBRNPBR = '1'
regex = re.compile("NP (the|a|any|some|an) NP")
if(len(regex.findall(short_str))>0):
has_NPaNP = '1'
regex = re.compile("NP (consist|consists) .{0,5}(the |a |any |some|an )?NP")
if(len(regex.findall(short_str))>0):
has_NPconsistNP = '1'
regex = re.compile("(NP )?(defined|Defined) (as |by )?(the |a |any |some|an )?NP")
if(len(regex.findall(short_str))>0):
has_NPdefineNP = '1'
#print("Found: "+ short_str)
for _4gram in _4grams:
if _4gram.strip()[:-3] in short_str:
in_4gram = '1'
for _5gram in _5grams:
if _5gram.strip()[:-3] in short_str:
in_5gram = '1'
for _6gram in _6grams:
if _6gram.strip()[:-3] in short_str:
in_6gram = '1'
for _7gram in _7grams:
if _7gram[:-3] in short_str:
in_7gram = '1'
for _8gram in _8grams:
if _8gram[:-3] in short_str:
in_8gram = '1'
##Shallow parsing sequence features##
#####################################
started = True
temp_result = []
#general purpose stemmer
#porter = nltk.PorterStemmer()
#current index in the original str
current_index = 0
# position features are not used
section_header = "O" # no section header found
section_id = "-1"
sent_id = -1
#sent index within section
sent_id_in_sec= -1
############################################
## below extracts all word level features ##
for i in range(len(words)):
#store the features for current word in a dictionary
all_features = {}
#stemmed word
if(len(words[i])>8):
stem = words[i][:-2]
else:
stem = words[i]
#suffix (if ends with ...)
if words[i].endswith('ion') or words[i].endswith('ity') or words[i].endswith('tor') or words[i].endswith('ics') or words[i].endswith('ment') or words[i].endswith('ive') or words[i].endswith('ic'):
suffix = 1
else:
suffix = 0
##################################
## shape features ##
capitalized = 0
all_caps = 0
mixed_cases = 0
cap_with_period = 0
with_digit = 0
hyphen = 0
length = words[i].__len__()
#captalized
if capitalizedLetter.search(words[i][0:1]):
capitalized = 1
#all captalized
if length > 1 and capitalizedLetter.search(words[i][1:]):
all_caps = 1
#some letter is captalized, not first one
elif length >1 and capitalizedLetter.search(words[i][1:]):
mixed_cases = 1
#last character '.' and last but one is capitalized
if length >2 and capitalizedLetter.search(words[i][length-2:]) and words[i][length-1:length] == '\.':
cap_with_period = 1
#contains digit
if digit.search(words[i]):
with_digit = 1
#contains hyphen
if '-' in words[i]:
hyphen = 1
#######################################
## position features ###
#the relative position with the pattern
relative_pos = current_index - pattern_index
#if the word is in the first 10 words of the sentence, for term classifier
if(i<10 and i<0.4*len(words)):
first_10_word = '1'
else:
first_10_word = '0'
#if the word is part of a NP
if(shallowtags[i].endswith('NP')):
is_NP = '1'
else:
is_NP = '0'
if(is_NP=='1' and first_10_word=='1'):
is_head_NP = '1'
else:
is_head_NP = '0'
#if the token is directly before 'is a' or 'is the'
before_pattern = '0'
if(i<len(words)-5):
context_str = ''
for k in range(5):
context_str += words[i+k] + ' '
if('is a' in context_str or 'is the' in context_str):
before_pattern = '1'
#print(context_str)
#####################################
#### dependency features #####
ancestor = "aBSENT" # the root of the word
#print dep_path_str
#store the value of the features in the dictionary
all_features['word'] = words[i]
all_features['pos'] = postags[i]
all_features['stem'] = stem
all_features['suffix'] = suffix
all_features['outputTag'] = OUT_TAG
all_features['length'] = len(words)/10
all_features['capitalized'] = capitalized
all_features['all_caps'] = all_caps
all_features['mixed_cases'] = mixed_cases
all_features['cap_with_period'] = cap_with_period
all_features['with_digit'] = with_digit
all_features['hyphen'] = hyphen
all_features['sentence_length'] = sentence_len
all_features['first_word'] = first_word
for kk in range(len(pattern_lookup)):
all_features[str(kk)] = pattern_lookup[kk]
all_features['pattern_type'] = pattern_type
all_features['pattern_index'] = pattern_index
all_features['relative_pos'] = relative_pos
all_features['shallow_tag'] = shallowtags[i]
all_features['word_position'] = i
all_features['has_pronoun'] = has_pronoun
all_features['section_id'] = section_id
all_features['section_header'] = section_header
all_features['sent_id'] = sent_id
all_features['sent_id_in_sec'] = sent_id_in_sec
#shallow parsing sequence features
all_features['starts_with_NP'] = starts_with_NP
all_features['starts_with_ADVP'] = starts_with_ADVP
all_features['starts_with_PPNP'] = starts_with_PPNP
all_features['starts_with_DTNP'] = starts_with_DTNP
# NP : NP
all_features['has_NPiNP'] = has_NPiNP
# NP is
all_features['has_NPIS'] = has_NPIS
# NP is * NP
all_features['has_NPISNP'] = has_NPISNP
# refer(s) to * NP
all_features['has_refer_to'] = has_refer_to
# NP is * NP + of/that/which/for/and/PUNCTUATION
all_features['has_NPISNPPP'] = has_NPISNPPP
# NP or NP
all_features['has_NPorNP'] = has_NPorNP
# known as * NP
all_features['has_known_as'] = has_known_as
# NP of * NP
all_features['has_NPofNP'] = has_NPofNP
# NP ( NP )
all_features['has_NPBRNPBR'] = has_NPBRNPBR
all_features['has_NPaNP'] = has_NPaNP
all_features['has_NPconsistNP'] = has_NPconsistNP
all_features['has_NPdefineNP'] = has_NPdefineNP
all_features['has_ie'] = has_ie
all_features['in_4gram'] = in_4gram
all_features['in_5gram'] = in_5gram
all_features['in_6gram'] = in_6gram
all_features['in_7gram'] = in_7gram
all_features['in_8gram'] = in_8gram
all_features['first_10_word'] = first_10_word
all_features['is_NP'] = is_NP
all_features['is_head_NP'] = is_head_NP
all_features['before_pattern'] = before_pattern
if len(ne_tags)>0:
all_features['NE'] = ne_tags[i]
else:
print "!!!"
#dependency features
all_features['root'] = ancestor
all_features['parent_word'] = parents[i]
all_features['parent_type'] = ptypes[i]
all_features['dep_path_str'] = dep_paths[i]
results.append(all_features)
current_index += words[i].__len__() + 1
#print(len(results))
return results
#end of map
################################################
# Main Function #
################################################
def usage():
print 'Usage: '+sys.argv[0]+' -i <file>'
if __name__ == '__main__' :
try:
opts, args = getopt.getopt(sys.argv[1:], 'i:', ['help', 'input file='])
if not opts or len(opts) != 1:
print 'Wrong number of options supplied'
usage()
sys.exit(2)
#get sys arguments
input_path = opts[0][1]
print "input_path=%s" % input_path
''' now done by bash script
#find the folder containing all the preprocessing files
if '.' in input_file:
input_folder = input_file[:input_file.rfind('.')] + "_defminer"
else:
input_folder = input_file + "_defminer"
if "/" in input_folder:
filename = input_folder[input_folder.rfind('/')+1:]
else:
filename = input_folder
input_path = input_folder + "/" + filename
'''
except getopt.GetoptError,e:
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit(2)
print "input file at %s" % input_path
print('reading stop words...')
#load the stopwords
f_stop = open('dictionary/stopwords')
stopwords = f_stop.readlines()
stopwords_str = ' '.join(stopwords)
print('read ' + str(len(stopwords)) + ' stopwords.' )
#load the patterns
f_pt = open('./pattern.txt')
patterns = f_pt.readlines()
f_pt.close()
#load the pronouns
f_pn = open('./pronoun.txt')
pronouns = f_pn.readlines()
f_pn.close()
#load chunk ngrams from wcl corpus
f = open('./corpus/wcl_datasets/ngram/4gram')
_4grams = f.readlines()
f.close()
f = open('./corpus/wcl_datasets/ngram/5gram')
_5grams = f.readlines()
f.close()
f = open('./corpus/wcl_datasets/ngram/6gram')
_6grams = f.readlines()
f.close()
f = open('./corpus/wcl_datasets/ngram/7gram')
_7grams = f.readlines()
f.close()
f = open('./corpus/wcl_datasets/ngram/8gram')
_8grams = f.readlines()
f.close()
#############################################################
##reading the corpus, including .word, .pos, .chunk, .seq,
## .ne, .parent, .ptype, .path
# read words
f_word = open(input_path+".word")
sents = f_word.readlines()
f_word.close()
# read pos tags
f_pos = open(input_path+".pos")
poss = f_pos.readlines()
f_pos.close()
# read the shallow parsing chunk tags for each word
f_chunk = open(input_path+".chunk")
chunks = f_chunk.readlines()
f_chunk.close()
# read the shallow parse sequences
f_parse = open(input_path+".seq")
parses = f_parse.readlines()
f_parse.close()
# read the named entity sequence
f_ne = open(input_path+".ne")
nes = f_ne.readlines()
f_ne.close()
# read dependency parent word
f_parent = open(input_path+".parent")
parents = f_parent.readlines()
f_parent.close()
# read the dependency parent type
f = open(input_path+".ptype")
ptypes = f.readlines()
f.close()
# read the dependency path to root
f = open(input_path+".path")
paths = f.readlines()
f.close()
## end reading input from files
#############################################################
print("Extract features for %d sentences" % len(sents))
results = []
for i in range(len(sents)):
data = []
data.append(sents[i].strip())
data.append(poss[i].strip())
data.append(chunks[i].strip())
data.append(parses[i].strip())
#below will not be generated for fast mode
data.append(nes[i].strip())
data.append(parents[i].strip())
data.append(ptypes[i].strip())
data.append(paths[i].strip())
results.append(Map(data))
#end for loop prepare data
print 'Found ' + str(len(results)) + ' sentences.'
#print 'Found ' + str(len(sentence_results)) + 'sentences'
#list of features to be used
features =['word','pos','stem','suffix','capitalized','all_caps','mixed_cases','cap_with_period','with_digit','hyphen','length','sentence_length','first_word','shallow_tag','word_position','section_id','section_header','sent_id','sent_id_in_sec','pattern_type','pattern_index','has_pronoun','starts_with_NP','starts_with_ADVP','starts_with_PPNP','starts_with_DTNP','has_NPiNP','has_NPIS','has_NPISNP','has_refer_to','has_NPISNPPP','has_NPorNP','has_known_as','has_NPofNP','has_NPBRNPBR','has_NPaNP','has_NPconsistNP','has_NPdefineNP','has_ie','in_4gram','in_5gram','in_6gram','in_7gram','in_8gram','first_10_word','is_NP','is_head_NP','NE','root']
for k in range(11):
features.append(str(k))
features.append('parent_word')
features.append('parent_type')
features.append('dep_path_str')
features.append('outputTag')
print "Total %d features" % len(features)
f = open(input_path + ".data","w+")
for sentence in results:
for word in sentence:
is_complete = True
line = ''
for feature in features:
line += str(word[feature]) + ' '
if(len(str(word[feature]))<1):
#error extracting feature
is_complete = False
print(str(feature) + " : " + str(word[feature]))
if(is_complete):
f.write(line + '\n')
f.write('\n')
f.close()
#end of script