-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsFeature.py
331 lines (298 loc) · 9.52 KB
/
sFeature.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
# -*- coding: utf-8 -*-
from __future__ import division
import nltk, string, math, csv, parse, os, random, re, numpy
pos = set()
neg = set()
lit = set()
un = set()
weak = set()
strong = set()
def csvload():
posfile = open('wordstat/positive.csv', 'rb')
negfile = open('wordstat/negative.csv', 'rb')
litfile = open('wordstat/litigious.csv', 'rb')
unfile = open('wordstat/uncertainty.csv', 'rb')
weakfile = open('wordstat/modalWeak.csv', 'rb')
strongfile = open('wordstat/modalStrong.csv', 'rb')
reader1 = csv.reader(posfile)
reader2 = csv.reader(negfile)
reader3 = csv.reader(litfile)
reader4 = csv.reader(unfile)
reader5 = csv.reader(weakfile)
reader6 = csv.reader(strongfile)
for row in reader1:
if len(row) == 1:
pos.add(row[0].lower())
for row in reader2:
if len(row) == 1:
neg.add(row[0].lower())
for row in reader3:
if len(row) == 1:
lit.add(row[0].lower())
for row in reader4:
if len(row) == 1:
un.add(row[0].lower())
for row in reader5:
if len(row) == 1:
weak.add(row[0].lower())
for row in reader6:
if len(row) == 1:
strong.add(row[0].lower())
csvload()
#train_base_path = "data/training/"
#train_files = ["Canon PowerShot SD500.txt", "Canon S100.txt", "Diaper Champ.txt", "Hitachi router.txt", "ipod.txt", "Linksys Router.txt", "MicroMP3.txt","Nokia 6600.txt", "norton.txt"]
#
#training_data = {}
#for train_file in train_files:
# train_path = os.path.join(train_base_path, train_file)
# parse.read_txt_data(train_path, training_data)
#
#held_base_path = "data/heldout/"
#held_files = ["Apex AD2600 Progressive-scan DVD player.txt", "Canon G3.txt", "Creative Labs Nomad Jukebox Zen Xtra 40GB.txt", "Nikon coolpix 4300.txt", "Nokia 6610.txt"]
#
#held_data = {}
#for held_file in held_files:
# held_path = os.path.join(held_base_path, held_file)
# parse.read_txt_data(held_path, held_data)
#
#
#training_data = parse.val_to_polarity(training_data)
#held_data = parse.val_to_polarity(held_data)
#
''' Turney's Feature's list for the Gender Indentification paper gave us a basis
for which features to look for. The group split up the features list. I took
word features.
'''
''' Takes a string as an input. Tokenizes string into list.
returns a dictionary with the following word features:
-total number of words (n)
-Average length per word (in characters) (avglen)
-Vocabulary richness (total different words/N) (rich)
-Words longer than 6 characters/N (longs)
-Total number of short words (1-3 characters)/N (shorts)
-Simpson’s D measure (SimpsonD)
-Sichel’s S measure (SichelS)
-Honore’s R measure (HonoreR)
(LIWC was abandonned because of lack of access)
-Wordstat features:
-Negative (negemo)
-Positive (posemo)
-Uncertainty (uncertain)
-Litigiousness (litig)
-Weak Modal Words (weakModal)
-Strong Modal Words (strongModal)
'''
def sFeature(sent):
sent = nltk.word_tokenize(sent)
sent = [w.lower().rstrip(string.punctuation).lstrip(string.punctuation) for w in sent]
features = {}
def basicFt(s):
wc = len(s)
if wc >= 40:
features["wc"] = 5
elif wc < 40 and wc >= 30:
features["wc"] = 4
elif wc < 30 and wc >= 20:
features["wc"] = 3
elif wc >= 10:
features["wc"] = 2
else:
features["wc"] = 1
rich = 0
if wc > 0:
rich = len(set(s))/wc
if rich > 0.667:
features["rich"] = 3
elif rich > 0.333 and rich < 0.667:
features["rich"] = 2
else:
features["rich"] = 1
avglen = 0
longs = 0
shorts= 0
for w in s:
if len(w) > 6:
longs += 1
if len(w) < 4:
shorts += 1
avglen += len(w)
if longs < 4:
features["longs"] = 1
if longs >= 4 and longs < 8:
features["longs"] = 2
if longs >= 8:
features["longs"] = 3
if shorts < 4:
features["shorts"] = 1
if shorts >= 4 and shorts < 10:
features["shorts"] = 2
if shorts >= 10 and shorts < 20:
features["shorts"] = 3
if shorts >= 20:
features["shorts"] = 4
if wc > 0:
avglen = avglen/wc
if avglen < 2:
features["avglen"] = 1
if avglen >= 2 and avglen < 3:
features["avglen"] = 2
if avglen >= 3 and avglen < 3.5:
features["avglen"] = 3
if avglen >= 3.5 and avglen < 4:
features["avglen"] = 4
if avglen >= 4 and avglen < 5:
features["avglen"] = 5
if avglen >= 5:
features["avglen"] = 6
def sichelS(s):
n = len(s)
s2 = sorted(s)
v2 = V(2, s2, n)
if n > 0:
S = v2/n
else:
S = 0
if S < 0.1 and S > 0:
features["sichelS"] = 1
elif S < 0.2 and S >= 0.1:
features["sichelS"] = 2
elif S >= 0.2 and S < 0.3:
features["sichelS"] = 3
elif S >= 0.3:
features["sichelS"] = 4
else:
features["sichelS"] = 0
def V(num, s1, n):
vm = 0
for i in range(0, n-1):
if i == 0:
flag = False
for j in range(0, num):
if s1[i] == s1[i + j]:
flag = True
else:
flag = False
if flag == True:
vm += 1
elif s1[i] != s1[i-1]:
flag = False
for j in range(0, num):
if i + j < n:
if s1[i] == s1[i + j]:
flag = True
else:
flag = False
if flag == True:
vm += 1
return vm
def simpsonD(s):
s1 = sorted(s)
n = len(s)
D = 0
for m in range(1, n):
D += V(m, s1, n) * (m / n) * ((m - 1)/(n-1))
if D < 0.01 and D > 0:
features["simpsonD"] = 1
elif D >= 0.01 and D < 0.3:
features["simpsonD"] = 2
elif D >= 0.3 and D < 1:
features["simpsonD"] = 3
elif D >= 1:
features["simpsonD"] = 4
else:
features["simpsonD"] = 0
def honoreR(s):
n = len(s)
s2 = sorted(s)
v1 = V(1, s2, n)
if n > 0:
R = 100 * (math.log(n)/ (1 - v1/n))
else:
R = 0
if R < 1000:
features["honoreR"] = 1
if R < 2000 and R >= 1000:
features["honoreR"] = 2
if R < 3000 and R >= 2000:
features["honoreR"] = 3
if R > 3000:
features["honoreR"] = 4
def wordStat(s):
posemo = 0
negemo = 0
uncertain = 0
litig = 0
weakModal = 0
strongModal = 0
for w in s:
if w in pos:
posemo += 1
if w in neg:
negemo += 1
if w in lit:
litig += 1
if w in un:
uncertain += 1
if w in weak:
weakModal += 1
if w in strong:
strongModal += 1
if negemo >= 3:
features["negemo"] = 3
elif negemo >= 1 and negemo < 3:
features["negemo"] = 2
else:
features["negemo"] = negemo
if posemo >= 3:
features["posemo"] = 3
elif posemo >= 1 and posemo < 3:
features["posemo"] = 2
else:
features["posemo"] = posemo
if uncertain >= 3:
features["uncertain"] = 3
elif uncertain >= 1 and uncertain < 3:
features["uncertain"] = 2
else:
features["uncertain"] = uncertain
if litig >= 3:
features["litig"] = 3
elif litig >= 1 and litig < 3:
features["litig"] = 2
else:
features["litig"] = litig
if weakModal >= 3:
features["weakModal"] = 3
elif weakModal >= 1 and weakModal < 3:
features["weakModal"] = 2
else:
features["weakModal"] = weakModal
if strongModal >= 3:
features["strongModal"] = 3
elif strongModal >= 1 and strongModal < 3:
features["strongModal"] = 2
else:
features["strongModal"] = strongModal
basicFt(sent)
wordStat(sent)
honoreR(sent)
sichelS(sent)
simpsonD(sent)
return features
##testing
#feature_sets = [(sFeature(n), v) for (n,v) in training_data.items()]
#random.shuffle(feature_sets)
#size = int(len(feature_sets) * 0.9)
#print "training results"
#train_set, test_set = feature_sets[size:], feature_sets[:size]
##train_set = [line for line in train_set if line]
#
#classifier = nltk.NaiveBayesClassifier.train(train_set)
#print nltk.classify.accuracy(classifier, test_set)
#classifier.show_most_informative_features()
#
#print " heldout results"
#train_set = feature_sets
#test_set = [(sFeature(n), v) for (n,v) in held_data.items()]
#print nltk.classify.accuracy(classifier, test_set)
#classifier.show_most_informative_features()