-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fasttext_Flask.py
63 lines (49 loc) · 2.07 KB
/
Fasttext_Flask.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
import compress_fasttext
from flask import Flask, request, jsonify, abort
import re
import numpy as np
from numpy.linalg import norm
app = Flask(__name__)
model = compress_fasttext.models.CompressedFastTextKeyedVectors.load('small_model_v2')
stopwords = []
def cosine_similarity(A, B):
all_zeros = not (np.any(A) and np.any(B))
if all_zeros:
return 0.0
return (np.dot(A, B) / (norm(A) * norm(B)))
with open("stopwords.txt", 'r', encoding="utf-8") as stopwords_file:
stopwords = [line.rstrip() for line in stopwords_file]
def process_line(text_line):
return re.sub('|'.join(r'\b%s\b' % re.escape(s) for s in stopwords),
"", text_line)
def get_dish_similarity(dish1, dish2):
dish1_stripped = process_line(dish1).split()
dish2_stripped = process_line(dish2).split()
return cosine_similarity(model.get_sentence_vector(dish1_stripped),model.get_sentence_vector(dish2_stripped))
def get_human_dish_similarity(dish1,dish2):
similarity = get_dish_similarity(dish1,dish2)
print("Similarity of: ",dish1, " AND ",dish2,"==== ",similarity)
def vectorize(dish_name, dish_description):
dish_string = dish_name+" ||| "+dish_description
dish_stripped = process_line(dish_string).split()
return model.get_sentence_vector(dish_stripped).tolist()
@app.route('/get-vector', methods=['GET'])
def get_vector():
try:
request_json = request.get_json()
dish_name = request_json["dish_name"]
dish_description = request_json["dish_description"]
except:
abort(400)
return vectorize(dish_name,dish_description)
@app.route('/get-dish-similarity', methods=['GET'])
def get_dish_similarity():
try:
request_json = request.get_json()
dish1_name = request_json["dish1_name"]
dish1_description = request_json["dish1_description"]
dish2_name = request_json["dish2_name"]
dish2_description = request_json["dish2_description"]
except:
abort(400)
return jsonify(cosine_similarity(vectorize(dish1_name,dish1_description),vectorize(dish2_name,dish2_description)))