thiyagab
commited on
Commit
•
92f59fe
1
Parent(s):
16eb088
Exception handled
Browse files- semanticsearch.py +20 -16
semanticsearch.py
CHANGED
@@ -46,23 +46,27 @@ def preprocess(input: str):
|
|
46 |
|
47 |
|
48 |
def find_similarities(input: str):
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
return response
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
similarity_matrix = cosine_similarity(
|
56 |
-
[input_embeddings[0]],
|
57 |
-
sen_embeddings[1:]
|
58 |
-
)
|
59 |
-
indices = [numpy.argpartition(similarity_matrix[0], -3)[-3:]]
|
60 |
-
indices=sorted(indices[0],key=lambda x:-similarity_matrix[0][x])
|
61 |
-
response = ''
|
62 |
-
for index in indices:
|
63 |
-
print(similarity_matrix[0][index])
|
64 |
-
response += kural_definition(index + 1)
|
65 |
-
return response
|
66 |
|
67 |
|
68 |
def kural_definition(index: int):
|
|
|
46 |
|
47 |
|
48 |
def find_similarities(input: str):
|
49 |
+
try:
|
50 |
+
response = preprocess(input)
|
51 |
+
if response:
|
52 |
+
return response
|
53 |
+
input_embeddings = model.encode([input.lower()])
|
54 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
55 |
+
# let's calculate cosine similarity for sentence 0:
|
56 |
+
similarity_matrix = cosine_similarity(
|
57 |
+
[input_embeddings[0]],
|
58 |
+
sen_embeddings[1:]
|
59 |
+
)
|
60 |
+
indices = [numpy.argpartition(similarity_matrix[0], -3)[-3:]]
|
61 |
+
indices=sorted(indices[0],key=lambda x:-similarity_matrix[0][x])
|
62 |
+
response = ''
|
63 |
+
for index in indices:
|
64 |
+
print(similarity_matrix[0][index])
|
65 |
+
response += kural_definition(index + 1)
|
66 |
return response
|
67 |
+
except:
|
68 |
+
return "Try again with different query"
|
69 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
|
72 |
def kural_definition(index: int):
|