I've created a NLP chatbot for mental health. I've created an intents file in json for user inputs and responses. I have a script for the normal chatbot and a script for a questionnaire. How would I be able to get the main chatbot script to run the questionnaire when it identifies the trigger phrase? I have a list of trigger phrases which I've included in the json. Below is a sample of the trigger phrase I want the script to identify and the chatbot code.
{
"tag": "triggerphq9",
"patterns": [
"I am sad",
"I'm not feeling great",
"sad",
"i'm depressed",
"i feel like shit",
"i feel terrible",
"i feel horrible",
"i feel like everything is against me"
],
"responses": [
"Oh no, I'm so sorry to hear that, why don't you tell me more",
"I'm here to listen",
"I'm here for you!"
]
},
The chatbot:
with open('intents.json', 'r') as json_data:
intents = json.load(json_data)
bot_name = "DMH Bot"
print("Chatbot is active! To stop the session, type 'quit'")
while True:
sentence = input("You: ")
if sentence == "quit":
break
sentence = tokenize(sentence)
X = bag_of_words(sentence, all_words)
X = X.reshape(1, X.shape[0])
X = torch.from_numpy(X).to(device)
output = model(X)
_, predicted = torch.max(output, dim=1)
tag = tags[predicted.item()]
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
if prob.item() > 0.75:
for intent in intents['intents']:
if tag == intent["tag"]:
print(f"{bot_name}: {random.choice(intent['responses'])}")
else:
print(f"{bot_name}: Sorry, I did not understand that.")
The questionnaire I want to trigger:
def phq_9():
num1= int(input("Little interest or pleasure in doing things\n"))
num2= int(input("Feeling down, depressed, or hopeless\n"))
num3 = int(input("Trouble falling or staying asleep, or sleeping too much\n"))
phqscore = num1+num2+num3+num4+num5+num6+num7+num8+num9
print("Thank you for answering these quesitons.")
def phq_9map():
if phqscore <= 4:
return print("Minimal depression.")
elif phqscore >=5 & phqscore <=9:
return print("mild depression.")
elif phqscore >=10 & phqscore <=14:
return print("moderate depression. ")
elif phqscore >=15 & phqscore <=19:
return print("moderately severe. ")
else:
return print("severe depression. ")
phq_9map()
print(phq_9map()) )
phq_9()
I would create a python-dictionary with all the functions and tags:
trigger_dictionary = {
"triggerphq9":phq_9
}
and then run the function that corresponds to the tag using:
trigger_dictionary[tag]()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.