-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantle.py
158 lines (139 loc) · 4.58 KB
/
semantle.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
from flask import (
Flask,
request,
jsonify,
send_file,
send_from_directory,
render_template,
)
import struct
import sqlite3
import base64
from time import strftime
def create_app():
app = Flask(__name__)
app.config["JSON_AS_ASCII"] = False
@app.route("/")
def send_index():
return send_file("static/index.html")
@app.route("/favicon.ico")
def send_favicon():
return send_file("static/assets/favicon.ico")
@app.route("/assets/<path:path>")
def send_static(path):
return send_from_directory("static/assets", path)
@app.route("/model/<string:word>")
def word(word):
try:
con = sqlite3.connect("word2vec.db")
cur = con.cursor()
res = cur.execute("SELECT vec FROM word2vec WHERE word = ?", (word,))
res = list(cur.fetchone())
con.close()
if not res:
return ""
res = res[0]
return jsonify(list(struct.unpack("300f", res)))
except Exception as e:
print(e)
return jsonify(e)
@app.route("/model2/<string:secret>/<string:word>")
def model2(secret, word):
try:
con = sqlite3.connect("word2vec.db")
cur = con.cursor()
res = cur.execute(
"SELECT vec, percentile FROM word2vec left outer join nearby on nearby.word=? and nearby.neighbor=? WHERE word2vec.word = ?",
(secret, word, word),
)
row = cur.fetchone()
if row:
row = list(row)
con.close()
if not row:
return ""
vec = row[0]
result = {"vec": list(struct.unpack("300f", vec))}
if row[1]:
result["percentile"] = row[1]
return jsonify(result)
except Exception as e:
print(e)
return jsonify(e)
@app.route("/similarity/<string:word>")
def similarity(word):
try:
con = sqlite3.connect("word2vec.db")
cur = con.cursor()
res = cur.execute(
"SELECT top, top10, rest FROM similarity_range WHERE word = ?", (word,)
)
res = list(cur.fetchone())
con.close()
if not res:
return ""
return jsonify({"top": res[0], "top10": res[1], "rest": res[2]})
except Exception as e:
print(e)
return jsonify(e)
@app.route("/nearby/<string:word>")
def nearby(word):
try:
con = sqlite3.connect("word2vec.db")
cur = con.cursor()
res = cur.execute(
"SELECT neighbor FROM nearby WHERE word = ? order by percentile desc limit 10 offset 1",
(word,),
)
rows = cur.fetchall()
con.close()
if not rows:
return ""
return jsonify([row[0] for row in rows])
except Exception as e:
print(e)
return jsonify(e)
@app.route("/nearby_1k/<string:word_b64>")
def nearby_1k(word_b64):
try:
word = base64.b64decode(word_b64.encode("iso-8859-1")).decode("iso-8859-1")
print(word)
con = sqlite3.connect("word2vec.db")
cur = con.cursor()
res = cur.execute(
"SELECT neighbor, percentile, similarity FROM nearby WHERE word = ? order by percentile desc limit 1000 offset 1 ",
(word,),
)
rows = cur.fetchall()
con.close()
words = [
dict(
neighbor=row[0],
percentile=int(row[1]),
similarity="%0.2f" % (100 * row[2]),
)
for row in rows
]
return render_template("top1k.html", word=word, words=words)
except Exception as e:
import traceback
traceback.print_exc()
return "Oops, error"
@app.errorhandler(404)
def not_found(error):
return "page not found"
@app.errorhandler(500)
def error_handler(error):
return error
@app.after_request
def add_header(response):
response.headers["Cache-Control"] = "no-store"
timestamp = strftime("[%Y-%b-%d %H:%M]")
print(
f"{timestamp} {request.remote_addr} {request.method} {request.scheme} {request.full_path} {response.status}"
)
return response
return app
if __name__ == "__main__":
import sqlite3
create_app().run(host="0.0.0.0", port=8080)