# 与聊天机器人交互 |
while True : |
message = input ( "You: " ) |
if message = = "quit" : |
break |
# 预处理消息 |
message_words = tokenize_words(message) |
message_words = lemmatize_words(message_words) |
# 将消息转换为词袋向量 |
message_bag = bag_of_words(message_words, words) |
# 预测意图 |
results = model.predict(np.array([message_bag]))[ 0 ] |
results_index = np.argmax(results) |
tag = classes[results_index] |
# 随机选择一个回答 |
if results[results_index] > 0.7 : |
for intent in intents[ 'intents' ]: |
if intent[ 'tag' ] = = tag: |
print ( "Bot:" , random.choice(intent[ 'responses' ])) |
break |
else : |
print ( "Bot: Sorry, I didn't understand that. Please try again." ) |
by: Python自学 发表于:2023-07-12 02:39:09 顶(1) | 踩(1) 回复
{ |
"intents" : [ |
{ |
"tag" : "greeting" , |
"patterns" : [ "Hi" , "Hello" , "Hey" ], |
"responses" : [ "Hello, how can I help you?" , "Hi there, how can I assist you?" , "Hey, how may I help you?" ] |
}, |
{ |
"tag" : "goodbye" , |
"patterns" : [ "Bye" , "See you later" , "Goodbye" ], |
"responses" : [ "Goodbye, have a nice day!" , "See you later, take care." , "Bye, have a good one!" ] |
} |
] |
} |
高级设计师
by: Python自学 发表于:2023-07-12 02:38:02 顶(2) | 踩(2) 回复
import
nltk
from
nltk.stem
import
WordNetLemmatizer
lemmatizer
=
WordNetLemmatizer()
def
tokenize_words(text):
return
nltk.word_tokenize(text)
def
lemmatize_words(words):
return
[lemmatizer.lemmatize(word.lower())
for
word
in
words]
回复评论