-
Notifications
You must be signed in to change notification settings - Fork 1
/
KNOW_2016_feature_generatorv9.py
196 lines (164 loc) · 6.6 KB
/
KNOW_2016_feature_generatorv9.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
import os.path
import pickle
import re
from KNOW_2016_feature_generatorv9functions import populateFeatureAll
from KNOW_2016_feature_generatorv9functions import k_fold_generator
# Load of attribute and test values
with open('trainingDataset.tsv','r') as f:
trainingsetAttributes=[x.strip().split('\t') for x in f][1:]
with open('testDataset.tsv','r') as f:
testsetAttributes=[x.strip().split('\t') for x in f][1:]
# Query caching prevents the algorithm to send DBpedia requests if it is already in the local storage
queryCache = set()
with open('querycache.txt','r') as f:
lines = f.readlines()
for line in lines:
queryCache.add(line.replace("\n",""))
cacheFile = open('querycache.txt', 'a',encoding='utf-8')
i = 0
featureListTest = []
featureListTrain = []
# Initialize Feature Sets
for row in trainingsetAttributes:
URI = row[1].replace('"','')
featureDictTrain = {"uri":URI}
ID = row[0].replace('"','')
featureDictTrain.update({"ID":ID})
featureListTrain.append(featureDictTrain)
for row in testsetAttributes:
URI = row[1].replace('"','')
featureDictTest = {"uri":URI}
ID = row[0].replace('"','')
featureDictTest.update({"ID":ID})
featureListTest.append(featureDictTest)
if os.path.isfile('traindumpv3') and os.path.isfile('testdumpv3'):
with open('traindumpv3','rb') as f:
featureListTrain = pickle.load(f)
with open('testdumpv3','rb') as f:
featureListTest = pickle.load(f)
else:
# Populate Features
for featDict in featureListTrain:
populateFeatureAll(featDict, queryCache, cacheFile)
for featDict in featureListTest:
populateFeatureAll(featDict, queryCache, cacheFile)
with open('traindumpv3','wb') as f:
pickle.dump(featureListTrain, f)
with open('testdumpv3','wb') as f:
pickle.dump(featureListTest, f)
cacheFile.close()
import nltk
from nltk.corpus import stopwords
stemmer = nltk.stem.snowball.EnglishStemmer(ignore_stopwords=False)
stop = stopwords.words('english')
# ------ ADDING TEXT FEATURES
if os.path.isfile('traindumpv3text') and os.path.isfile('testdumpv3text'):
with open('traindumpv3text','rb') as f:
featureListTrain = pickle.load(f)
with open('testdumpv3text','rb') as f:
featureListTest = pickle.load(f)
else:
import nltk
def get_words_in_reviews(reviews):
all_words = []
for (words, sentiment) in reviews:
all_words.extend(words)
return all_words
def get_word_features(wordlist):
wordlist = nltk.FreqDist(wordlist)
word_features = wordlist.keys()
return word_features
# ------ ADDING TEXT FEATURES
with open('trainingDataset.tsv','r') as f:
trainingsetAttributes=[x.strip().split('\t') for x in f][1:]
import re
uncleanedreviews = []
reviews = []
for row in trainingsetAttributes:
try:
import os.path
ID = row[0]
if os.path.isfile('MetacriticReviews/'+ID+'.txt'):
with open('MetacriticReviews/'+ID+'.txt', 'rb') as f:
revs = re.sub('[^0-9a-zA-Z ]+',"",f.read().decode("utf-8").replace("\n"," "))
uncleanedreviews.append((revs,str(row[6])))
except BaseException as b:
print(b)
for (words,sentiment) in uncleanedreviews:
words_filtered = [stemmer.stem(e.lower()) for e in words.split() if len(e) >= 3 and stemmer.stem(e.lower()) not in stop]
reviews.append((words_filtered, sentiment))
word_features = get_word_features(get_words_in_reviews(reviews))
for row in trainingsetAttributes:
try:
import os.path
ID = row[0]
if os.path.isfile('MetacriticReviews/'+ID+'.txt'):
with open('MetacriticReviews/'+ID+'.txt', 'rb') as f:
revs = re.sub('[^0-9a-zA-Z ]+',"",f.read().decode("utf-8").replace("\n"," "))
for featDict in featureListTrain:
if featDict['ID']==row[0].replace('"',''):
for word in revs.split():
root = stemmer.stem(word)
if root in word_features and root not in stop:
featDict.update({root: 1})
else:
print("dosya yok")
except BaseException as b:
print(b)
for row in testsetAttributes:
try:
import os.path
ID = row[0]
if os.path.isfile('MetacriticReviewsTest/'+ID+'.txt'):
with open('MetacriticReviewsTest/'+ID+'.txt', 'rb') as f:
revs = re.sub('[^0-9a-zA-Z ]+',"",f.read().decode("utf-8").replace("\n"," "))
for featDict in featureListTest:
if featDict['ID']==row[0].replace('"',''):
for word in revs.split():
root = stemmer.stem(word)
if root in word_features and root not in stop:
featDict.update({root: 1})
else:
print(ID+"dosya yok")
except BaseException as b:
print(b)
import re
with open('traindumpv3text','wb') as f:
pickle.dump(featureListTrain, f)
with open('testdumpv3text','wb') as f:
pickle.dump(featureListTest, f)
# ----- ENG TEXT FEATURES
with open('trainingDataset.tsv','r') as f:
trainingsetLabels=[x.strip().split('\t')[6] for x in f][1:]
X=featureListTrain
y= trainingsetLabels
from sklearn.svm import SVC
from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer()
fit = vec.fit(X)
X_train_counts = fit.transform(X)
X_test_counts = fit.transform(featureListTest)
clf = SVC(kernel="linear", C=0.025)
clf.fit(X_train_counts.toarray(), y)
predict = clf.predict(X_test_counts.toarray())
for v in predict:
print(v)
try:
accuracy = 0.0
for X_train, y_train, X_test, y_test in k_fold_generator(X, y, 10):
vec = DictVectorizer()
fit = vec.fit(X_train)
X_train_counts = fit.transform(X_train)
X_test_counts = fit.transform(X_test)
clf = SVC(kernel="linear", C=0.025)
try:
clf.fit(X_train_counts.toarray(), y_train)
predict = clf.predict(X_test_counts.toarray())
accuracy += clf.score(X_test_counts.toarray(),y_test)
for i in range(0,len(X_test)):
print (X_test[i]['ID']+"\t"+y_test[i]+"\t"+predict[i])
except BaseException as b:
print (b)
print (str(accuracy))
except BaseException as b:
print (b)