我正在研究一種聊天機器人。我想知道是否有辦法在字典中使用inputas a value。例如:當輸入是“我的名字是 x”時,我想得到答案“你好 x”。我試過這個:"my name is (.*)": ["Hello ! % 1"]但效果不佳。我想,我需要定義 (.*) 和 %1 但我不知道如何實作。
如果您知道制作此方法的方法,請幫助我。謝謝你。
words = {"good night": ["nighty night", "good night", "sleep well"],
"good morning": ["good morning", "wakey-wakey!", "rise and shine!"],
"hi": ["hello", "hey", "hi"],
"how are you": ["I am good, thank you", "Pretty good,thank you", "Very well, thanks",
"I am doing great thanks", "Better than some, not as good as others",
"I am better now, it is good to talk to you"],
"bye": ["see you later", "bye", "bye-bye", "see you soon", "bye for now", "catch you later"],
"my name is (.*)": ["Hello ! % 1"]
}
text_punk = input("text something: ")
greet_words = words.keys() #check if the key words is in input_text.
word_available = [word for word in greet_words if word in text_punk]
if word_available: # if words are available take the first of key.
punk = random.choice(words[word_available[0]])
print(punk)
talk(punk) #this is for pyttsx3
else:
print("problem!")
uj5u.com熱心網友回復:
看起來你想要這樣的東西:
from random import choice
from re import match, sub
text_punk = ''
while 'bye' not in text_punk:
words = {"good night": ["nighty night", "good night", "sleep well"],
"good morning": ["good morning", "wakey-wakey!", "rise and shine!"],
"hi": ["hello", "hey", "hi"],
"how are you": ["I am good, thank you", "Pretty good,thank you", "Very well, thanks",
"I am doing great thanks", "Better than some, not as good as others",
"I am better now, it is good to talk to you"],
"bye": ["see you later", "bye", "bye-bye", "see you soon", "bye for now", "catch you later"],
"my name is (.*)": ["Hello ! {1}"]
}
text_punk = input("text something: ").lower()
response = 'I did not get that, sorry'
for expression in words:
if m := match(expression, text_punk):
response = choice(words[expression]).format(*(m.group(i) for i in range(len(m.groups()) 1)))
break
print(response)
使用一些輸入運行它:
text something: hi
hello
text something: my name is grismar
Hello ! grismar
text something: bye!
catch you later
當然,這就是魔法發生的地方:
choice(words[expression]).format(*(m.group(i) for i in range(len(m.groups()) 1)))
如果expression匹配 中的輸入text_punk,它會從該字典值中選擇一個隨機短語。然后它嘗試{}使用匹配組格式化所選值(替換 中的值)。
它通過列出所有組來做到這一點,從 0 到許多使用range(len(m.groups()) 1).
例如,如果輸入為'my name is grismar',則組0為'my name is grismar',組1為grismar。所以命令歸結為:
choice(words['my name is (.*)']).format('my name is grismar', 'grismar')
由于choice(words['my name is (.*)'])can only be 'Hello ! {1}',它真的是:
'Hello ! {1}'.format('my name is grismar', 'grismar')
它也適用于多個問候語:
"my name is (.*)": ["Hello {1}!", "Hey {1}, long time no see!"]
您可以有多個匹配項:
"my name is ([^\s] ) (.*)": ["Hello {1}!", "Greetings, {2}!", "Mx {2}, first name {1}, OK"]
例子:
text something: my name is jaap van der velde
Hello jaap!
text something: my name is jaap van der velde
Greetings, van der velde!
text something: my name is jaap van der velde
Mx van der velde, first name jaap, OK
text something:
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/401158.html
