简体   繁体   English

python的触发脚本

[英]Trigger script for python

I've created a NLP chatbot for mental health.我创建了一个用于心理健康的 NLP 聊天机器人。 I've created an intents file in json for user inputs and responses.我在 json 中为用户输入和响应创建了一个意图文件。 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.我有一个包含在 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:我将创建一个包含所有函数和标签的 python 字典:

trigger_dictionary = {
 "triggerphq9":phq_9
}

and then run the function that corresponds to the tag using:然后使用以下命令运行与标签对应的函数:

trigger_dictionary[tag]()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM