-
Notifications
You must be signed in to change notification settings - Fork 0
/
API.py
46 lines (40 loc) · 1.21 KB
/
API.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
from flask import Flask, render_template, request
from gensim.models import KeyedVectors
model_name = 'models/wiki_skipgram.vec'
try:
model = KeyedVectors.load_word2vec_format(model_name)
except FileNotFoundError:
import os
print("download model")
os.system('./download.sh')
print("download selesai")
app = Flask(__name__)
def toDict(result):
tmp = {}
for token, distance in result:
try:
tmp[token] = round(float(distance), 3)
except ValueError:
tmp[token] = distance
return tmp
def inference(word, top=5):
# 5 top similar words
if type(word) == list:
word[0] = word[0].lower()
else:
word = word.lower()
try:
return(model.most_similar(word)[:top])
except KeyError:
return([("peringatan", "kata yang Anda masukkan tidak ada dalam vocabulary")])
@app.route('/')
def student():
return render_template('index.html')
@app.route('/top',methods = ['POST'])
def result():
if request.method == 'POST':
result = dict(request.form)
top = toDict(inference(result['kata']))
return render_template("result.html", result = top)
if __name__ == '__main__':
app.run(host = '0.0.0.0', port = 1234)