app.py
1.02 KB
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
from flask import Flask, redirect, url_for, request
from gensim.models.doc2vec import Doc2Vec
from konlpy.tag import Mecab
import json
import sys
model = Doc2Vec.load("doc2vec.model")
app = Flask(__name__)
@app.route('/analyze', methods=['POST'])
def sendreq():
try:
data = request.data.decode('utf-8').replace("'", '"')
datadic = json.loads(data)
sentence1 = datadic["sentence1"]
sentence2 = datadic["sentence2"]
token1 = parsetoken(sentence1)
token2 = parsetoken(sentence2)
num_similar = model.n_similarity(token1, token2)
return json.dumps({"result": str(num_similar)})
except:
return json.dumps({"result": -1})
def parsetoken(sentence):
mecab = Mecab()
lst = []
tags = mecab.pos(sentence)
for tag in tags:
try:
model["/".join(tag)]
lst.append("/".join(tag))
except:
print(1)
# resdata = dict(zip(range(1, len(lst) + 1), lst))
return lst
if __name__ == "__main__":
app.run()