Updated for index search
Browse files- semanticsearch.py +18 -16
semanticsearch.py
CHANGED
@@ -33,31 +33,32 @@ sen_embeddings = model.encode(en_translations)
|
|
33 |
def preprocess(input:str):
|
34 |
if input.startswith('/'):
|
35 |
#TODO
|
36 |
-
return
|
37 |
values = [int(s) for s in re.findall(r'-?\d+\.?\d*', input)]
|
38 |
-
|
39 |
if values:
|
40 |
index=values[0]
|
41 |
return kural_definition(index)
|
42 |
else:
|
43 |
return False
|
44 |
def find_similarities(input:str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
sen_embeddings[1:]
|
53 |
-
)
|
54 |
|
55 |
-
indices=[numpy.argpartition(similarity_matrix[0],-3)[-3:]]
|
56 |
-
indices.sort(reverse=True)
|
57 |
-
response=''
|
58 |
-
for index in indices[0]:
|
59 |
-
response+=kural_definition(index)
|
60 |
-
return response
|
61 |
|
62 |
def kural_definition(index:int):
|
63 |
response=''
|
@@ -65,6 +66,7 @@ def kural_definition(index:int):
|
|
65 |
response += "\n".join(kurals[index + 1]) + "\n"
|
66 |
response += en_translations[index + 1] + "\n\n"
|
67 |
print("\n".join(kurals[index + 1]))
|
|
|
68 |
|
69 |
# while True:
|
70 |
# text=input('Ask valluvar: ')
|
|
|
33 |
def preprocess(input:str):
|
34 |
if input.startswith('/'):
|
35 |
#TODO
|
36 |
+
return False
|
37 |
values = [int(s) for s in re.findall(r'-?\d+\.?\d*', input)]
|
|
|
38 |
if values:
|
39 |
index=values[0]
|
40 |
return kural_definition(index)
|
41 |
else:
|
42 |
return False
|
43 |
def find_similarities(input:str):
|
44 |
+
response = preprocess(input)
|
45 |
+
if response:
|
46 |
+
return response
|
47 |
+
input_embeddings = model.encode([input.lower()])
|
48 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
49 |
+
#let's calculate cosine similarity for sentence 0:
|
50 |
+
similarity_matrix=cosine_similarity(
|
51 |
+
[input_embeddings[0]],
|
52 |
+
sen_embeddings[1:]
|
53 |
+
)
|
54 |
|
55 |
+
indices=[numpy.argpartition(similarity_matrix[0],-3)[-3:]]
|
56 |
+
indices.sort(reverse=True)
|
57 |
+
response=''
|
58 |
+
for index in indices[0]:
|
59 |
+
response+=kural_definition(index)
|
60 |
+
return response
|
|
|
|
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
def kural_definition(index:int):
|
64 |
response=''
|
|
|
66 |
response += "\n".join(kurals[index + 1]) + "\n"
|
67 |
response += en_translations[index + 1] + "\n\n"
|
68 |
print("\n".join(kurals[index + 1]))
|
69 |
+
return response
|
70 |
|
71 |
# while True:
|
72 |
# text=input('Ask valluvar: ')
|