Skip to content

Commit 78cf8f6

Browse files
committed
添加svm模型
1 parent 1fdb570 commit 78cf8f6

File tree

3 files changed

+95
-4
lines changed

3 files changed

+95
-4
lines changed

online/svm.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# coding=utf-8
2+
3+
# 输入一个string 首先分词,然后读取本地word2vec模型,计算向量,
4+
# 读取本地的svm模型,做预测
5+
# 返回预测结果
6+
import warnings
7+
8+
warnings.filterwarnings("ignore")
9+
import jieba
10+
import numpy as np
11+
from numpy import *
12+
13+
try:
14+
import cPickle as pickle
15+
except:
16+
import pickle
17+
18+
19+
def kernel(x, y, sigma):
20+
x = mat(x)
21+
y = mat(y)
22+
temp = x - y
23+
return math.exp(temp * temp.T / (-2) * sigma * sigma)
24+
25+
26+
def label(x):
27+
alphs_result = pickle.loads(open("data/svm_model_1", 'rb').read(), encoding='iso-8859-1')
28+
x_result = pickle.loads(open("data/svm_model_2", 'rb').read(), encoding='iso-8859-1')
29+
y_result = pickle.loads(open("data/svm_model_3", 'rb').read(), encoding='iso-8859-1')
30+
b = pickle.loads(open("data/svm_model_4", 'rb').read(), encoding='iso-8859-1')
31+
32+
num = len(alphs_result)
33+
re = 0.0
34+
for i in range(num):
35+
re += alphs_result[i] * y_result[i] * kernel(x_result[i], x, 1)
36+
re += b
37+
if (re < 0):
38+
return 0
39+
else:
40+
return 1
41+
42+
43+
def feature(word_model, sentence):
44+
seg_list = jieba.cut(sentence, cut_all=False)
45+
count = 0
46+
for j in seg_list:
47+
if j not in word_model:
48+
continue
49+
if count == 0:
50+
old = word_model[j]
51+
new = np.zeros(shape=old.shape)
52+
else:
53+
new = word_model[j]
54+
old = old + new
55+
count += 1
56+
if (count != 0):
57+
old = old * (1.0 / count)
58+
return list(old)
59+
60+
61+
def svm_predict(word_model, sentence):
62+
x = feature(word_model, sentence)
63+
return label(x)

online/templates/index.html

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414

1515

16+
17+
18+
19+
1620
</style>
1721
{% endblock %}
1822

@@ -21,17 +25,17 @@
2125
function check(){
2226

2327
$("#alert").hide();
24-
2528
msg=$("#msg").val();
26-
2729
if(msg==""){
2830
return false;
2931
}
3032

33+
radio= $('input[name="option"]:checked ').val();
34+
3135
$.ajax({
3236
url: "check",
3337
method: "POST",
34-
data: "msg="+msg,
38+
data: "msg="+msg+"&method="+radio,
3539
success: function(data){
3640
if(data=="1"){
3741
$("#alert").text("这条短信是垃圾短信!");
@@ -63,6 +67,10 @@
6367

6468

6569

70+
71+
72+
73+
6674
</script>
6775
{% endblock %}
6876

@@ -72,6 +80,17 @@
7280
<div class="row">
7381
<div class="page-header">
7482
<h2>输入短信文本</h2></div>
83+
<div class="input-group">
84+
<label>
85+
<input type="radio" name="option" id="option1" value="1" checked>
86+
朴素贝叶斯
87+
</label>
88+
<label>
89+
<input type="radio" name="option" id="option2" value="2" checked>
90+
SVM
91+
</label>
92+
</div>
93+
7594
<div class="input-group">
7695
<input id="msg" class="form-control input-lg" type="text" placeholder="输入文本"><span class="input-group-btn">
7796
<button id="check" onclick="check();" class="btn btn-lg btn-default" type="button">检测</button>

online/view.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
from flask_bootstrap import Bootstrap
33
import bayes
44
import random
5+
import online.svm
6+
import gensim
57

68
app = Flask(__name__)
79
Bootstrap(app)
810

911
prior, condprob = bayes.runBayes()
12+
word_model = gensim.models.Word2Vec.load_word2vec_format('data/word2vec_model', binary=True)
1013

1114

1215
@app.route("/")
@@ -18,7 +21,13 @@ def index():
1821
@app.route("/check", methods=['POST', 'GET'])
1922
def checkmsg():
2023
msg = request.form['msg']
21-
result = bayes.predict(msg, prior, condprob)
24+
method = request.form['method']
25+
if msg == "" or method == "":
26+
return ""
27+
if method == "1":
28+
result = bayes.predict(msg, prior, condprob)
29+
elif method == "2":
30+
result = online.svm.svm_predict(word_model, msg)
2231
return str(result)
2332

2433

0 commit comments

Comments
 (0)