-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaying.py
More file actions
45 lines (33 loc) · 1.11 KB
/
playing.py
File metadata and controls
45 lines (33 loc) · 1.11 KB
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
import subprocess
import json
import numpy as np
import os
import time
import random
import string
import requests
letters = string.ascii_lowercase
queries = [''.join(random.choice(letters)
for i in range(random.randint(1, 50))) for _ in range(100)]
def create_query_line(queries):
line = '{"queries": ["'
for i, query in enumerate(queries[:-1]):
line += query
line += '", "'
line += queries[-1]
line += '"]}'
return line
headers = {
'Ocp-Apim-Subscription-Key': sys.argv[1],
}
response = requests.post('https://api.msturing.org/gen/encode',
headers=headers,
data=create_query_line(queries))
data = json.loads(response.text)
vector_0 = np.asarray(data[0]['vector'], dtype=np.float32)
vector_1 = np.asarray(data[1]['vector'], dtype=np.float32)
def cosine_similarity(a, b):
return np.dot(a, b) / (np.sqrt(np.sum(np.square(a))) * np.sqrt(np.sum(np.square(b))))
print("Query 0: " + data[0]['query'])
print("Query 1: " + data[1]['query'])
print(cosine_similarity(vector_0, vector_1))