FomoFix / bm25_retreive_question.py
ASledziewska
Update bm25_retreive_question.py
8ff5a08
from rank_bm25 import BM25Okapi
import nltk
from nltk.tokenize import word_tokenize
import numpy as np
import streamlit as st
import random
# Download NLTK data for tokenization
nltk.download('punkt')
class QuestionRetriever:
def __init__(self):
self.question_history = []
self.depression_questions = [
"How often have you felt persistently low in mood or sad for most of the day?",
"How often have you lost interest or pleasure in activities you used to enjoy?",
"How often have you experienced significant changes in appetite or weight (up or down)?",
"How often have you had trouble sleeping or sleeping too much?",
"How often have you felt restless or slowed down most of the time?",
"How often have you felt worthless or excessively guilty, even for minor things?",
"How often have you had difficulty thinking, concentrating, or making decisions?",
"How often have you had recurrent thoughts of death or suicide?",
"How often have you felt hopeless or like there's no point in life?",
"How often have you felt isolated or withdrawn from others due to your mood?",
"How often have you felt down, depressed, or hopeless?",
"How often have you had little interest or pleasure in doing things over the past two weeks?",
"Have you experienced changes in your appetite or weight due to feeling depressed in the last two weeks?",
"How often have you had trouble falling asleep, staying asleep, or sleeping too much because of feeling depressed?",
"Have you felt tired or had little energy most days over the past two weeks due to depression?",
"How often have you felt bad about yourself or that you are a failure or have let yourself or your family down in the last two weeks?",
"Have you had trouble concentrating on things like reading, watching TV, or engaging in conversations due to feeling depressed?",
"How often have you thought that you would be better off dead or hurting yourself in some way over the past two weeks?",
"Do you feel restless or slowed down physically most days because of feeling depressed in the last two weeks?",
"How often have you found it difficult to make decisions or felt indecisive due to feeling depressed recently?",
"How often do you feel sad or hopeless?",
"Do you have trouble getting out of bed or motivating yourself to do things?",
"Have you experienced a loss of interest in activities or hobbies?",
"Do you feel like you're not good enough or that you've failed at things?",
"Have you noticed any changes in your appetite or sleep patterns?",
"Do you feel like you're isolated from others or that you don't have any support?",
"Have you experienced any thoughts of self-harm or suicide?",
"Do you feel like you're stuck in a rut or that you can't see a way out of your current situation?",
"Have you noticed any physical symptoms like fatigue or loss of energy?",
"Have you considered seeking professional help for your depression?"
]
self.adhd_questions = [
"How often do you find yourself having trouble focusing on tasks or activities?",
"How often do you easily get sidetracked by noises, movements, or unrelated thoughts?",
"How often do you have difficulty finishing tasks or following through on instructions?",
"How often do you feel restless or fidgety, often unable to sit still for long periods?",
"How often do you talk excessively or blurt out things before thinking?",
"How often do you struggle with waiting for your turn or interrupting others frequently?",
"How often do you lose things you need for work or school (keys, phone, etc.)?",
"How often are you forgetful or disorganized?",
"How often do you have difficulty planning ahead or organizing tasks?",
"How often do you have difficulty controlling strong emotions or impulsive behaviors?",
"How often have you found it difficult to pay attention to details or make careless mistakes in your work or activities due to ADHD symptoms?",
"How often have you had trouble staying focused on tasks or activities like work, school, or hobbies because of ADHD symptoms in the last two weeks?",
"Have you experienced difficulties in organizing tasks and activities, such as messy workspaces or missed deadlines, because of ADHD recently?",
"How often have you avoided or been reluctant to engage in tasks that require sustained mental effort due to ADHD symptoms over the past two weeks?",
"Have you been forgetful in daily activities, such as missing appointments or losing items, because of ADHD symptoms in the last two weeks?",
"How often have you felt restless or fidgety in situations where it is inappropriate due to ADHD symptoms recently?",
"Do you find it challenging to engage in leisure activities quietly or feel driven by a motor due to ADHD most days?",
"How often have you interrupted others or blurted out answers before questions have been completed because of ADHD symptoms over the past two weeks?",
"Have you had difficulties waiting your turn or remaining seated in situations where it is expected due to ADHD symptoms recently?",
"How often have you felt impatient or had difficulty engaging in activities quietly due to ADHD symptoms in the last two weeks?",
"How often do you have trouble sitting still or staying focused?",
"Do you have trouble following instructions or completing tasks?",
"Have you noticed any difficulty with organization or time management?",
"Do you find yourself easily distracted or forgetful?",
"Have you experienced any impulsivity or difficulty with self-control?",
"Do you have trouble waiting your turn or behaving in line with social rules?",
"Have you noticed any changes in your mood or emotional state?",
"Do you have trouble with word retrieval or difficulty finding the right words?",
"Have you experienced any difficulty with reading or comprehension?",
"Have you considered seeking professional help for your ADHD?"
]
self.anxiety_questions = [
"How often have you been feeling excessive worry or nervousness, even about everyday things?",
"How often do you experience physical symptoms like racing heart, sweating, or shortness of breath when feeling anxious?",
"How often do you encounter certain situations or triggers that cause you significant anxiety?",
"How often do you find yourself avoiding places or activities due to anxiety?",
"How often have you had trouble sleeping or concentrating because of anxious thoughts?",
"How often do you feel a constant need to be in control or have things perfect to avoid anxiety?",
"How often do you have intrusive thoughts that are difficult to stop?",
"How often do you experience sudden feelings of intense fear or panic (panic attacks)?",
"How often has your anxiety significantly impacted your daily life or relationships?",
"How much does anxiety interfere with your daily life?",
"How often have you felt nervous, anxious, or on edge?",
"How often have you found it difficult to stop or control worrying thoughts in the last two weeks?",
"Have you experienced restlessness or felt keyed up or on edge due to anxiety in the past two weeks?",
"How often have you felt easily fatigued or had difficulty concentrating because of anxiety recently?",
"Have you noticed irritability or muscle tension as a result of feeling anxious over the past two weeks?",
"How often have you had trouble falling asleep, staying asleep, or restless sleep due to anxiety in the last two weeks?",
"Do you find yourself easily startled or feeling on edge most days because of anxiety?",
"How often have you experienced physical symptoms like sweating, trembling, or a racing heart due to anxiety recently?",
"Have you found it challenging to relax or felt restless most days because of anxiety in the past two weeks?",
"How often have you felt a sense of impending doom or danger because of anxiety over the past two weeks?",
"How often do you feel nervous or on edge?",
"Do you have trouble relaxing or controlling your worries?",
"Have you experienced a racing or irregular heartbeat?",
"Do you feel like you're constantly on guard or on high alert?",
"Have you noticed any physical symptoms like trembling or sweating?",
"Do you feel like you're having trouble concentrating or making decisions?",
"Have you experienced any feelings of detachment or disconnection from others?",
"Do you feel like you're having trouble sleeping or experiencing vivid dreams?",
"Have you noticed any changes in your appetite or eating habits?",
"Have you considered seeking professional help for your anxiety?"
]
def get_response(self, user_query, predicted_mental_category):
if predicted_mental_category == "depression":
knowledge_base = self.depression_questions
elif predicted_mental_category == "adhd":
knowledge_base = self.adhd_questions
elif predicted_mental_category == "anxiety":
knowledge_base = self.anxiety_questions
else:
print("Sorry, I didn't understand that.")
tokenized_docs = [word_tokenize(doc.lower()) for doc in knowledge_base] # Ensure lowercase for consistency
bm25 = BM25Okapi(tokenized_docs)
tokenized_query = word_tokenize(user_query.lower()) # Ensure lowercase for consistency
doc_scores = bm25.get_scores(tokenized_query)
# Get the index of the most relevant document
# most_relevant_doc_index = doc_scores.argmax()
# Get the indices of documents sorted by score in descending order
sorted_indices = np.argsort(doc_scores)[::-1]
# Flag to keep track of whether a relevant document index is found
found_new_question = False
self.question_history = st.session_state.asked_questions
print(self.question_history)
for i in range(len(sorted_indices)):
print(f"Checking {i+1} question - Index {sorted_indices[i]}th - {knowledge_base[sorted_indices[i]]}")
most_relevant_doc_index = sorted_indices[i]
if knowledge_base[most_relevant_doc_index] not in self.question_history:
found_new_question = True
question = knowledge_base[most_relevant_doc_index]
st.session_state.asked_questions.append(question)
print(f">>> Returning Top #{i+1} question")
return question
# If no relevant document index is found, return None or handle the case accordingly
if not found_new_question:
question = random.choice(knowledge_base)
# Fetch the corresponding response from the knowledge base
# response = knowledge_base[most_relevant_doc_index]
return question
if __name__ == "__main__":
knowledge_base = depression_questions
model = QuestionRetriever(knowledge_base)
user_input = input("User: ")
response = model.get_response(user_input)
print("Chatbot:", response)