Remember asked questions
Browse files
bm25_retreive_question.py
CHANGED
@@ -48,17 +48,20 @@ class QuestionRetriever:
|
|
48 |
print("Sorry, I didn't understand that.")
|
49 |
|
50 |
if knowledge_base:
|
51 |
-
tokenized_docs = [word_tokenize(doc.lower()) for doc in knowledge_base]
|
52 |
bm25 = BM25Okapi(tokenized_docs)
|
53 |
tokenized_query = word_tokenize(user_query.lower()) # Ensure lowercase for consistency
|
54 |
doc_scores = bm25.get_scores(tokenized_query)
|
55 |
|
56 |
-
#
|
57 |
-
|
58 |
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
62 |
else:
|
63 |
return None
|
64 |
|
|
|
48 |
print("Sorry, I didn't understand that.")
|
49 |
|
50 |
if knowledge_base:
|
51 |
+
tokenized_docs = [word_tokenize(doc.lower()) for doc in knowledge_base] # Ensure lowercase for consistency
|
52 |
bm25 = BM25Okapi(tokenized_docs)
|
53 |
tokenized_query = word_tokenize(user_query.lower()) # Ensure lowercase for consistency
|
54 |
doc_scores = bm25.get_scores(tokenized_query)
|
55 |
|
56 |
+
# Sort document scores in descending order
|
57 |
+
sorted_doc_indices = sorted(range(len(doc_scores)), key=lambda i: doc_scores[i], reverse=True)
|
58 |
|
59 |
+
for doc_index in sorted_doc_indices:
|
60 |
+
response = knowledge_base[doc_index]
|
61 |
+
if response not in st.session_state.asked_questions:
|
62 |
+
return response
|
63 |
+
# If no response found that is not in asked_questions, return None
|
64 |
+
return None
|
65 |
else:
|
66 |
return None
|
67 |
|