bluejoyq

add machine read server py

1 +from flask import Flask, redirect, url_for, request
2 +from gensim.models.doc2vec import Doc2Vec
3 +from konlpy.tag import Mecab
4 +import json
5 +import sys
6 +
7 +model = Doc2Vec.load("doc2vec.model")
8 +
9 +
10 +app = Flask(__name__)
11 +
12 +
13 +@app.route('/analyze', methods=['POST'])
14 +def sendreq():
15 + try:
16 + data = request.data.decode('utf-8').replace("'", '"')
17 + datadic = json.loads(data)
18 + sentence1 = datadic["sentence1"]
19 + sentence2 = datadic["sentence2"]
20 + token1 = parsetoken(sentence1)
21 + token2 = parsetoken(sentence2)
22 + num_similar = model.n_similarity(token1, token2)
23 + return json.dumps({"result": str(num_similar)})
24 + except:
25 + return json.dumps({"result": -1})
26 +
27 +
28 +def parsetoken(sentence):
29 + mecab = Mecab()
30 + lst = []
31 + tags = mecab.pos(sentence)
32 + for tag in tags:
33 + try:
34 + model["/".join(tag)]
35 + lst.append("/".join(tag))
36 + except:
37 + null
38 + # resdata = dict(zip(range(1, len(lst) + 1), lst))
39 + return lst
40 +
41 +
42 +if __name__ == "__main__":
43 + app.run()