File tree 3 files changed +95
-4
lines changed 3 files changed +95
-4
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change 13
13
14
14
15
15
16
+
17
+
18
+
19
+
16
20
</ style >
17
21
{% endblock %}
18
22
21
25
function check ( ) {
22
26
23
27
$ ( "#alert" ) . hide ( ) ;
24
-
25
28
msg = $ ( "#msg" ) . val ( ) ;
26
-
27
29
if ( msg == "" ) {
28
30
return false ;
29
31
}
30
32
33
+ radio = $ ( 'input[name="option"]:checked ' ) . val ( ) ;
34
+
31
35
$ . ajax ( {
32
36
url : "check" ,
33
37
method : "POST" ,
34
- data : "msg=" + msg ,
38
+ data : "msg=" + msg + "&method=" + radio ,
35
39
success : function ( data ) {
36
40
if ( data == "1" ) {
37
41
$ ( "#alert" ) . text ( "这条短信是垃圾短信!" ) ;
63
67
64
68
65
69
70
+
71
+
72
+
73
+
66
74
</ script >
67
75
{% endblock %}
68
76
72
80
< div class ="row ">
73
81
< div class ="page-header ">
74
82
< 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
+
75
94
< div class ="input-group ">
76
95
< input id ="msg " class ="form-control input-lg " type ="text " placeholder ="输入文本 "> < span class ="input-group-btn ">
77
96
< button id ="check " onclick ="check(); " class ="btn btn-lg btn-default " type ="button "> 检测</ button >
Original file line number Diff line number Diff line change 2
2
from flask_bootstrap import Bootstrap
3
3
import bayes
4
4
import random
5
+ import online .svm
6
+ import gensim
5
7
6
8
app = Flask (__name__ )
7
9
Bootstrap (app )
8
10
9
11
prior , condprob = bayes .runBayes ()
12
+ word_model = gensim .models .Word2Vec .load_word2vec_format ('data/word2vec_model' , binary = True )
10
13
11
14
12
15
@app .route ("/" )
@@ -18,7 +21,13 @@ def index():
18
21
@app .route ("/check" , methods = ['POST' , 'GET' ])
19
22
def checkmsg ():
20
23
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 )
22
31
return str (result )
23
32
24
33
You can’t perform that action at this time.
0 commit comments