-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.py
More file actions
311 lines (265 loc) · 10.2 KB
/
features.py
File metadata and controls
311 lines (265 loc) · 10.2 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
from stanfordcorenlp import StanfordCoreNLP
from stanfordnlp import StanfordNLP
import json
from nltk.tree import Tree
import numpy as np
sNLP = StanfordNLP()
"""Sentences__________________________________________________"""
class Sentenceftrs:
def __init__(self, stopwords):
self.stopwords = stopwords
def position(self, sentence, slist):
"""The position of the sentences. Suppose there are M sentences in the document, for th ith sentence, the position feature is computed as 1-(i-1)/(M-1)"""
ith = slist.index(sentence) + 1
M = len(slist)
feature = 1 - (ith-1)/(M-1)
return feature
def length(self, sentence):
return len(sentence)
def subs(self, tree):
"""Sub-sentence count in parsing tree"""
#t = sNLP.parse(sentence)
nodes = tree.treepositions()
cs = 0
for node in nodes:
if not type(tree[node]) is str:
if tree[node].label() == "S" or tree[node].label() == "@S":
cs += 1
return cs
def depth(self, stree):
"""The root depth of the parsing tree"""
#tree = sNLP.parse(sentence)
maxd = 0
for pos in tree.treepositions():
if len(pos) > maxd:
maxd = len(pos)
return maxd
def atf(self, wlist, tf_dic):
"""The mean TF values of words in the sentence, devided bu sentence length"""
tf_sum = 0
#wlist = [w.lower() for w in sNLP.word_tokenize(sentence)]
for word in wlist:
tf_sum += tf_dic[word]
return tf_sum / len(wlist)**2
def aidf(self, wlist, idf_dic):
"""The mean word IDF values in sentence, devided by the sentence lenght"""
idf_sum = 0
#wlist = [w.lower() for w in sNLP.word_tokenize(sentence)]
for word in wlist:
if word in idf_dic:
idf_sum += idf_dic[word]
else:
idf_sum += max(list(idf_dic.values()))
return idf_sum / len(wlist)**2
def acf(self, wlist, cf_dic):
"""The mean word CF values in sentence, devided by the sentence length"""
cf_sum = 0
#wlist = [w.lower() for w in sNLP.word_tokenize(sentence)]
for word in wlist:
cf_sum += cf_dic[word]
return cf_sum / len(wlist)**2
def posratio(self, tags):
"""The number of nouns, verbs, adjectives and adverbs in the sentence, devided by sentence length"""
#tags = sNLP.pos(sentence)
cn = 0
cv = 0
cj = 0
cr = 0
c = 0
for tag in tags:
c += 1
if tag[1][0] == "N":
cn += 1
if tag[1][0] == "V":
cv += 1
if tag[1][0] == "J":
cj += 1
if tag[1][0:1] == "RB":
cr += 1
feature = np.array([cn/c, cv/c, cj/c, cr/c])
return feature
def neration(self, tags):
"""The number of named enitites, devided by sentence length"""
#tags = sNLP.ner(sentence)
c1 = 0
c2 = 0
for tag in tags:
c1 += 1
if tag[1] != "O":
c2 += 1
return c2/c1
def numberratio(self, tags):
"""The number of digits, devided by sentence length"""
#tags = sNLP.pos(sentence)
c1 = 0
c2 = 0
for tag in tags:
c1 += 1
if tag[1] == "CD":
c2 += 1
return c2/c1
def stopratio(self, wlist):
"""The number of stopwords, devided by sentence length. Use stopword list of ROUGE"""
counter = 0
for word in wlist:
counter += word in self.stopwords
return counter
"""Wordi features ________________________________________________________________________"""
class Wordftrs:
"""
word, slist - list of sentences in document
"""
def __init__(self, idf_dic):
self.tf_dic = {}
self.cf_dic = {}
self.idf_dic = idf_dic
self.slen_dic = {}
self.stf_dic = {}
self.sidf_dic = {}
self.scf_dic = {}
self.ss_dic = {}
self.sd_dic = {}
def tf(self, slist):
""" term frequency """
for wlist in slist:
#wlist = [w.lower() for w in sNLP.word_tokenize(sentence)]
for word in wlist:
if word in self.tf_dic:
self.tf_dic[word] += 1
else:
self.tf_dic[word] = 1
def idf(self, cluster_size):
""" total document number in the datasets, devided by the frequency of documents which contains the word"""
pass
def cf(self, slist):
""" the frequency of documents which conntains this word in the current cluster"""
words = []
for wlist in slist:
for w in wlist:
words.append(w.lower())
wset = set(words)
for word in wset:
if word in self.cf_dic:
self.cf_dic[word] += 1
else:
self.cf_dic[word] = 1
def pos(self, wlist_tuple):
""" a 4-dimension binary vector indicates whether the word is a noun, a verb, an adjective or an adverb. If the word has another part-of-speech, the vector is all-zero"""
#wlist_tuple = sNLP.pos(sentence)
feature_vec = []
for wordtuple in wlist_tuple:
if wordtuple[1][0] == "N":
feature = np.array([1, 0, 0, 0])
elif wordtuple[1][0] == "V":
feature = np.array([0, 1, 0, 0])
elif wordtuple[1][0] == "J":
feature = np.array([0, 0, 1, 0])
elif wordtuple[1][0:1] == "RB":
feature = np.array([0, 0, 0, 1])
else:
feature = np.array([0, 0, 0, 0])
feature_vec.append(feature)
return feature_vec
def namedentity(self, wlist):
""" a binary value equals one iff the output of named entity classifier from CoreNLP is not entity"""
feature_vec = []
for word in wlist:
try:
ne = sNLP.ner(word)[0]
except:
feature_vec.append(0)
continue
if ne[1] == "O":
feature = 0
else:
feature = 1
feature_vec.append(feature)
return feature_vec
def number(self, wlist_tuple):
""" a binary value denotes if whether the word is a number"""
#wlist_tuple = sNLP.pos(sentence)
feature_vec = []
for wordtuple in wlist_tuple:
if wordtuple[1] == "CD":
feature = 1
else:
feature = 0
feature_vec.append(feature)
return feature_vec
def slen(self, swlist):
"""The maximal length of sentences owning the word"""
for wlist in swlist:
#wlist = [w.lower() for w in sNLP.word_tokenize(sentence)]
ln = len(wlist)
for word in wlist:
if not word in self.slen_dic:
self.slen_dic[word] = ln
else:
self.slen_dic[word] = max(self.slen_dic[word], ln)
def stf(self, slist):
"""The maximal TF score of sentences owning the word"""
maxes = []
for wlist in slist:
#wlist = [w.lower() for w in sNLP.word_tokenize(sentence)]
maxes.append(max([self.tf_dic[wrd] for wrd in wlist]))
for idx, wlist in enumerate(slist):
#wlist = [w.lower() for w in sNLP.word_tokenize(sentence)]
for word in wlist:
if not word in self.stf_dic:
self.stf_dic[word] = maxes[idx]
else:
self.stf_dic[word] = max(self.stf_dic[word], maxes[idx])
def scf(self, swlist):
"""The maximal CF score of sentences owning the word"""
maxes = []
for wlist in swlist:
#wlist = [w.lower() for w in sNLP.word_tokenize(sentence)]
maxes.append(max([self.cf_dic[wrd] for wrd in wlist]))
for idx, wlist in enumerate(swlist):
#wlist = [w.lower() for w in sNLP.word_tokenize(sentence)]
for word in wlist:
if not word in self.scf_dic:
self.scf_dic[word] = maxes[idx]
else:
self.scf_dic[word] = max(self.scf_dic[word], maxes[idx])
def sidf(self, swlist):
"""The maximal IDF score of sentences owning the word"""
maxes = []
for wlist in swlist:
#wlist = [w.lower() for w in sNLP.word_tokenize(sentence)]
mx = 0
for wrd in wlist:
if not wrd in self.idf_dic:
self.idf_dic[wrd] = max(list(self.idf_dic.values()))
mx = max(mx, self.idf_dic[wrd])
maxes.append(mx)
#maxes.append(max([self.idf_dic[wrd] for wrd in wlist]))
for idx, wlist in enumerate(swlist):
#wlist = [w.lower() for w in sNLP.word_tokenize(sentence)]
for word in wlist:
if not word in self.sidf_dic:
self.sidf_dic[word] = maxes[idx]
else:
self.sidf_dic[word] = max(self.sidf_dic[word], maxes[idx])
def update_ss(self, tlist):
"""The maximal sub-sentence count of sentences owning the word. A sub-sentence means the node label is S or @S in parsing tree"""
for tree in tlist:
subs = tree.subs()
wlist = [w.lower() for w in tree.wordlist]
for word in wlist:
if not word in self.ss_dic:
self.ss_dic[word] = subs
else:
self.ss_dic[word] = max(
self.ss_dic[word], subs)
def update_sd(self, tlist):
"""The maximal parsing tree depth of sentences owning the word"""
for tree in tlist:
depth = tree.depth()
wlist = [w.lower() for w in tree.wordlist]
for word in wlist:
if not word in self.sd_dic:
self.sd_dic[word] = depth
else:
self.sd_dic[word] = max(
self.sd_dic[word], depth)