jaelin215 commited on
Commit
c50568e
1 Parent(s): 0c66737

Remember asked questions

Browse files
Files changed (1) hide show
  1. bm25_retreive_question.py +9 -6
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] # 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
- # Get the index of the most relevant document
57
- most_relevant_doc_index = doc_scores.argmax()
58
 
59
- # Fetch the corresponding response from the knowledge base
60
- response = knowledge_base[most_relevant_doc_index]
61
- return response
 
 
 
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