Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update chat_bot.py #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 29 additions & 49 deletions chat_bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import re
import pandas as pd
import pyttsx3
from sklearn import preprocessing
from sklearn.tree import DecisionTreeClassifier,_tree
import numpy as np
Expand Down Expand Up @@ -37,10 +36,7 @@

clf1 = DecisionTreeClassifier()
clf = clf1.fit(x_train,y_train)
# print(clf.score(x_train,y_train))
# print ("cross result========")
scores = cross_val_score(clf, x_test, y_test, cv=3)
# print (scores)
print (scores.mean())


Expand All @@ -53,17 +49,6 @@
indices = np.argsort(importances)[::-1]
features = cols

def readn(nstr):
engine = pyttsx3.init()

engine.setProperty('voice', "english+f5")
engine.setProperty('rate', 130)

engine.say(nstr)
engine.runAndWait()
engine.stop()


severityDictionary=dict()
description_list = dict()
precautionDictionary=dict()
Expand All @@ -72,14 +57,18 @@ def readn(nstr):

for index, symptom in enumerate(x):
symptoms_dict[symptom] = index
def calc_condition(exp,days):
sum=0
def calc_condition(exp, days):
total_severity = 0
for item in exp:
sum=sum+severityDictionary[item]
if((sum*days)/(len(exp)+1)>13):
print("You should take the consultation from doctor. ")
if item in severityDictionary:
total_severity += severityDictionary[item]
else:
print(f"System Warning: Symptom '{item}' not found in severityDictionary.")

if (total_severity * days) / (len(exp) + 1) > 13:
print("Carey: I would suggest consultating a doctor! ")
else:
print("It might not be that bad but you should take precautions.")
print("Carey: It seems like the situation is not that bad, but I think you should still take certain precautions.")


def getDescription():
Expand All @@ -91,9 +80,6 @@ def getDescription():
_description={row[0]:row[1]}
description_list.update(_description)




def getSeverityDict():
global severityDictionary
with open('MasterData/symptom_severity.csv') as csv_file:
Expand All @@ -107,7 +93,6 @@ def getSeverityDict():
except:
pass


def getprecautionDict():
global precautionDictionary
with open('MasterData/symptom_precaution.csv') as csv_file:
Expand All @@ -118,12 +103,11 @@ def getprecautionDict():
_prec={row[0]:[row[1],row[2],row[3],row[4]]}
precautionDictionary.update(_prec)


def getInfo():
print("-----------------------------------HealthCare ChatBot-----------------------------------")
print("\nYour Name? \t\t\t\t",end="->")
name=input("")
print("Hello, ",name)
#print("-----------------------------------HealthCare ChatBot-----------------------------------")
print("\nCarey: Hello! I am Carey: The Get Well Bot and I am here to ensure you that All is Well!! What is your name? \n")
name=input("You: ")
print("Carey: Hello, ",name, "! How may I help you today~?")

def check_pattern(dis_list,inp):
pred_list=[]
Expand Down Expand Up @@ -169,34 +153,31 @@ def tree_to_code(tree, feature_names):

while True:

print("\nEnter the symptom you are experiencing \t\t",end="->")
disease_input = input("")
print("\nCarey: Don't worry! We'll figure this out together. Firstly, can you tell me the symptom that you are experiencing ? \n")
disease_input = input("You: ")
conf,cnf_dis=check_pattern(chk_dis,disease_input)
if conf==1:
print("searches related to input: ")
print("Carey: I found the following searches related to your symptom: ")
for num,it in enumerate(cnf_dis):
print(num,")",it)
if num!=0:
print(f"Select the one you meant (0 - {num}): ", end="")
conf_inp = int(input(""))
print(f"Carey: Can you select the one you exactly meant? You can select by sending the corresponding number from (0 - {num})! \n")
conf_inp = int(input("You: "))
else:
conf_inp=0

disease_input=cnf_dis[conf_inp]
break
# print("Did you mean: ",cnf_dis,"?(yes/no) :",end="")
# conf_inp = input("")
# if(conf_inp=="yes"):
# break
else:
print("Enter valid symptom.")
print("Carey: I am so sorry, I'm not sure about that symptom. Can you try re-phrasing or perhaps mentioning a different symptom instead?")

while True:
try:
num_days=int(input("Okay. From how many days ? : "))
print("Carey: Oh, I see... For how many days have you been experiencing this? :\n ")
num_days=int(input("You: "))
break
except:
print("Enter valid input.")
print("Carey: I don't think that's a number ;--;")
def recurse(node, depth):
indent = " " * depth
if tree_.feature[node] != _tree.TREE_UNDEFINED:
Expand All @@ -221,7 +202,7 @@ def recurse(node, depth):
# if len(dis_list)!=0:
# print("symptoms present " + str(list(symptoms_present)))
# print("symptoms given " + str(list(symptoms_given)) )
print("Are you experiencing any ")
print("Carey: By any chance, are you experiencing any")
symptoms_exp=[]
for syms in list(symptoms_given):
inp=""
Expand All @@ -231,33 +212,32 @@ def recurse(node, depth):
if(inp=="yes" or inp=="no"):
break
else:
print("provide proper answers i.e. (yes/no) : ",end="")
print("Carey: You can let me know by either saying yes or no :) : \n")
if(inp=="yes"):
symptoms_exp.append(syms)

second_prediction=sec_predict(symptoms_exp)
# print(second_prediction)
calc_condition(symptoms_exp,num_days)
if(present_disease[0]==second_prediction[0]):
print("You may have ", present_disease[0])
print("Carey: It seems to me that you may have ", present_disease[0])
print(description_list[present_disease[0]])

# readn(f"You may have {present_disease[0]}")
# readn(f"{description_list[present_disease[0]]}")

else:
print("You may have ", present_disease[0], "or ", second_prediction[0])
print("Carey: It seems to me that you may have", present_disease[0], "or", second_prediction[0])
print(description_list[present_disease[0]])
print(description_list[second_prediction[0]])

# print(description_list[present_disease[0]])
precution_list=precautionDictionary[present_disease[0]]
print("Take following measures : ")
print("Carey: You don't have to panic! This is merely an assumption. Regardless, I suggest taking following measures! : \n")
for i,j in enumerate(precution_list):
print(i+1,")",j)

# confidence_level = (1.0*len(symptoms_present))/len(symptoms_given)
# print("confidence level is " + str(confidence_level))
print("\nCarey: Having flucating health is not uncommon. It can be stressful, but I want you to remember that things will improve and I am always here for you. Get Well Soon!")

recurse(0, 1)
getSeverityDict()
Expand Down