-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvm.py
63 lines (51 loc) · 1.55 KB
/
svm.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
# coding=utf-8
# 输入一个string 首先分词,然后读取本地word2vec模型,计算向量,
# 读取本地的svm模型,做预测
# 返回预测结果
import warnings
warnings.filterwarnings("ignore")
import jieba
import numpy as np
from numpy import *
try:
import cPickle as pickle
except:
import pickle
def kernel(x, y, sigma):
x = mat(x)
y = mat(y)
temp = x - y
return math.exp(temp * temp.T / (-2) * sigma * sigma)
def label(x):
alphs_result = pickle.loads(open("data/svm_model_1", 'rb').read(), encoding='iso-8859-1')
x_result = pickle.loads(open("data/svm_model_2", 'rb').read(), encoding='iso-8859-1')
y_result = pickle.loads(open("data/svm_model_3", 'rb').read(), encoding='iso-8859-1')
b = pickle.loads(open("data/svm_model_4", 'rb').read(), encoding='iso-8859-1')
num = len(alphs_result)
re = 0.0
for i in range(num):
re += alphs_result[i] * y_result[i] * kernel(x_result[i], x, 1)
re += b
if (re < 0):
return 0
else:
return 1
def feature(word_model, sentence):
seg_list = jieba.cut(sentence, cut_all=False)
count = 0
for j in seg_list:
if j not in word_model:
continue
if count == 0:
old = word_model[j]
new = np.zeros(shape=old.shape)
else:
new = word_model[j]
old = old + new
count += 1
if (count != 0):
old = old * (1.0 / count)
return list(old)
def svm_predict(word_model, sentence):
x = feature(word_model, sentence)
return label(x)