簡體   English   中英

python的觸發腳本

[英]Trigger script for python

我創建了一個用於心理健康的 NLP 聊天機器人。 我在 json 中為用戶輸入和響應創建了一個意圖文件。 我有一個用於普通聊天機器人的腳本和一個用於問卷調查的腳本。 當它識別觸發短語時,我如何能夠讓主聊天機器人腳本運行問卷? 我有一個包含在 json 中的觸發短語列表。 下面是我希望腳本識別的觸發短語和聊天機器人代碼的示例。

  {
    "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!"
    ]
  },

聊天機器人:

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.")

我要觸發的問卷:

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()

我將創建一個包含所有函數和標簽的 python 字典:

trigger_dictionary = {
 "triggerphq9":phq_9
}

然后使用以下命令運行與標簽對應的函數:

trigger_dictionary[tag]()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM