-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
72 lines (42 loc) · 1.56 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import requests
import json
import time
import sys
import numpy as np
# Contributed by https://github.com/P6A9
def get_word_embed(word):
#starttime=time.time()
#arg=sys.argv[1]
if word=="#":
url="http://35.184.36.137/embed?id=%23"
else:
url="http://35.184.36.137/embed?id=" + word
response = requests.get(url)
if (response.text==word):
return np.zeros(300,dtype=np.float32 )
embedding_string = response.text.split(' ')
embedding = [float(0) for _ in range(300)]
for x in range(1,len(embedding_string)):
if '\n' in embedding_string[x]:
embedding[(x-1)] = float( embedding_string[x].replace('\n',''))
else:
embedding[(x-1)] = float( embedding_string[x] )
#if x == 300:
# embedding_string[300] = float( embedding_string[x].replace('\n',''))
#else:
# embedding_string[x] = float( embedding_string[x] )
#embedding = embedding_string[1:]
return embedding
#convert_to_list(response.text)
#print ("---------------%s-------------" %(time.time() - starttime))
def api_question_embed(question):
question_embed = []
for word in question:
if word.isdigit():
total_sum = np.zeros(300,dtype=np.float32 )
for digit in word:
total_sum += get_word_embed(str(digit))
question_embed.append(total_sum/len(word))
else:
question_embed.append(get_word_embed(word))
return question_embed