-
Notifications
You must be signed in to change notification settings - Fork 0
/
similarityMeasures.py
73 lines (63 loc) · 2.22 KB
/
similarityMeasures.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
import falcon
import json
from dbConn.db import Connect
from autoAnswer.nlp import TextMining, RakeTags, WordNet
ALLOWED_ORIGINS = ['http://localhost:4200']
class CorsMiddleware(object):
def process_request(self, request, response):
origin = request.get_header('Origin')
if origin in ALLOWED_ORIGINS:
response.set_header('Access-Control-Allow-Origin', origin)
response.set_header('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept")
class Greeting:
def on_get(self, req, res):
res.body = json.dumps({'status': True, 'message': 'Welcome to auto answering'})
class GetRecommendation:
def on_post(self, req, res):
tm = TextMining()
rt = RakeTags()
wn = WordNet()
body = json.loads(req.stream.read())
text = body['question']
tmResult = tm.generate(text)
rtResult = rt.generate(text, True)
db = Connect().dbOpen()
sql = db.cursor()
case = ''
like = ''
i = 0
for tag in rtResult:
if i != 0:
case += ' + '
like += ' or '
case += 'case when tags like "%%%s%%" then 1 else 0 end' % tag
like += 'tags like "%%%s%%"' % tag
i += 1
sql.execute("SELECT id, id_answer, score, content, tags," + case + " as priority FROM question WHERE " + like + " order by priority desc limit 10")
result = []
for id, id_answer, score, content, tags, priority in sql.fetchall():
sql.execute("SELECT content FROM answer WHERE id = '%s'", (id_answer,))
content = content.replace('\n', "")
answer = sql.fetchone()
if answer is None:
answer = 'Belum terjawab'
else:
answer = answer[0].replace('\n', ' ')
result.append({
'id': id,
'priority': priority,
'similarity': wn.similarity(text, content),
'content': tm.stripTags(content),
'id_answer': id_answer,
'answer': tm.stripTags(answer)
})
result = sorted(result, key = lambda x: x['similarity'], reverse = True)
question = {
'target': text,
'result': result,
}
res.body = json.dumps(result[:5])
api = falcon.API(middleware=[CorsMiddleware()])
api.add_route('/', Greeting())
api.add_route('/getRecommendation', GetRecommendation())
# waitress-serve --port=8080 similarityMeasures:api