forked from youzan/YZSpamFilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterApi.py
64 lines (52 loc) · 1.51 KB
/
filterApi.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
# -*- coding: utf-8 -*-
import os
import sys
import logging
from config import configs
from utils import ClearAndSegment, u
from flask.ext.restful import abort, Resource
from flask import request, make_response, jsonify
from classifier import Algorithm
from filter import filter
import pickle
reload(sys)
sys.setdefaultencoding('utf-8')
stopwords_file = configs['stopwords_file']
fstop = open(stopwords_file)
totalStop = fstop.readlines()
fstop.close()
stops = []
for s in totalStop:
s = s.strip()
stops.append(s)
threshold = configs['threshold']
classify_model = configs['classify_model']
if not os.path.exists(classify_model):
print "ERROR: you should have a filter model"
exit(-1)
with open(classify_model, 'rb') as file:
f = filter(Algorithm)
t = pickle.load(file)
f.Algorithm.loadmodel(t)
file.close()
class SpamFilter(Resource):
"""
垃圾信息过滤服务
"""
def get(self):
if 'query' not in request.args:
abort(404, message="parameter `query` doestn't exist")
query = u(request.args['query'])
#logging.debug("new query '%s'" % (query))
liststr = ClearAndSegment(query)
#print liststr
liststr = [word.encode('UTF-8') for word in liststr if word not in stops]
score = f.singlejudge(liststr)
#print score
body = {}
if score > threshold:
body = {'spam': 'True'}
else:
body = {'spam': 'False'}
resp = make_response(jsonify(body))
return resp