-
Notifications
You must be signed in to change notification settings - Fork 1
/
SVMtemp.py
123 lines (103 loc) · 3.33 KB
/
SVMtemp.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
import numpy as np
from sklearn.svm import SVC
import pydexter
from sentence_similarity import calcQuerySimilarity
# import sentence_similarity
from gensim.models import Word2Vec
from cosine_similarity import calcCosineSimilarity
from entity_check import calcEntityCheck
def calcJaccard(list1,list2):
s1=set(list1)
s2=set(list2)
inters=s1.intersection(s2)
uni=s1.union(s2)
if len(uni)==0:
return 0
return float(len(inters))/len(uni)
def makingQueryPairVector(query1, query2,model,dxtr):
#print "making feature vector"
list1=query1.split()
list2=query2.split()
jcoef=calcJaccard(list1,list2)
querypairvector=[]
querypairvector.append(jcoef)
sentsim=calcQuerySimilarity(query1,query2)
querypairvector.append(sentsim)
cossim = calcCosineSimilarity(query1,query2,model)
querypairvector.append(cossim)
namedentitysc=calcEntityCheck(query1,query2,dxtr) #3rd argument dxtr?????????
querypairvector.append(namedentitysc)
return querypairvector
if __name__ == '__main__':
#configuring the word2vec
print "training word2vec"
#sentences=word2vec.Text8Corpus('text8')
#model=word2vec.Word2Vec(sentences,size=10)
#model = Word2Vec.load('cosModel.model')
# creating the dexter client
print "creating dexter client"
dxtr = pydexter.DexterClient("http://dexterdemo.isti.cnr.it:8080/dexter-webapp/api/")
# processing the query log to obtain training and test data
train_feature_matrix=np.array([])
train_labels=np.array([])
"""i = 0
f1 = open('trainFeatureVector.txt','w')
with open("train2.txt","r") as train_data:
for sample in train_data:
#print "reading training sample", ':' ,i
items=sample.split(",")
train_feature_vector=makingQueryPairVector(items[0],items[1],model,dxtr)
f1.write(' '.join(str(x) for x in train_feature_vector) + ' ' + str(items[2]))
#train_labels.append(items[2])
#train_feature_matrix.append(train_feature_vector)
i += 1
f1.close()
"""
test_feature_matrix=np.array([])
test_label=np.array([])
i = 0
#f1 = open('testFeatureVector.txt','w')
with open("DATA/finalFeature.txt","r") as test_data:
for sample in test_data:
#print "reading test sample",':',i
items=sample.split(" ")
#print items
test_feature_vector=np.asfarray(items[:-2])
t = np.asfarray(items[-1])
if test_label.size:
test_label = np.append(test_label,t)
test_feature_matrix = np.concatenate((test_feature_matrix,[test_feature_vector]),axis=0)
else :
test_label = t
test_feature_matrix = [test_feature_vector]
#print np.shape(test_feature_matrix),np.shape(test_label)
train_X=np.array(test_feature_matrix[:-6000])
train_Y = np.array(test_label[:-6000])
test_X=np.array(test_feature_matrix[-6000:])
test_Y=np.array(test_label[-6000:])
co = 0
for i in range(len(test_Y)):
if test_Y[i] == 1:
co += 1
#preapring the model adn training it
print "Model set up "
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
clf = SVC()
clf.fit(train_X,train_Y)
#testing the model
predict_Y=clf.predict(test_X)
precision=0.0
coP = 0
coN = 0
for i in range(len(predict_Y)):
if predict_Y[i]==1:
coP += 1
else:
coN += 1
if predict_Y[i]==test_Y[i]:
precision+=1
print "precision is: ",precision/len(predict_Y)
print co,coP,coN