-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentence_embeddings_use.py
53 lines (38 loc) · 1.29 KB
/
sentence_embeddings_use.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
import psycopg2
import psycopg2.extras
import numpy
import tensorflow_hub as hub
import winsound
import tensorflow as tf
connection = psycopg2.connect(user="postgres",
password="root",
host="127.0.0.1",
port="5432",
database="github")
cur = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
# embed = hub.load("F:/use/")
embed = hub.load("F:/universal-sentence-encoder-large_5/")
def write_sentence_embeddings(file, row):
try:
print(row['id'])
doc = row['tokens'].lower().replace(',', ' ')
sentence_embeddings = embed([doc])
file.write(f'{",".join(map(str, sentence_embeddings.numpy()[0]))},{"question" if row["question"] else "not_question"}\n')
except Exception as e:
print(f'error for id: {row["id"]}')
print(e)
def main():
fileBase = open("./dataset/use/use_large_v5_200t.csv", "a")
# write csv header line
for i in range(0,512):
fileBase.write(f'f{i},')
fileBase.write(f'label\n')
cur.execute("SELECT * from issues where eval_selected is true")
rows = cur.fetchall()
for row in rows:
write_sentence_embeddings(fileBase, row)
fileBase.close()
connection.close()
# notify
winsound.Beep(300, 200)
main()