我正在创建一个谈论国际象棋的机器人,现在我正在进行预处理。问题是,当Python程序阻塞字符串中的单词时,除非单词是最后一个单词,否则它将不会执行任何操作。那是输出。我所期待的是,词干分析器阻止用户输入的句子中的每个单词。我做错了什么?这是代码:
import re
from nltk.stem import PorterStemmer
port = PorterStemmer()
print("Hello! I am the ChessBot. I will talk about chess, but can only talk about chess. Nothing else. I can't even understand a greeting! Now, initiate this conversation!")
while True:
userinput = input('> ')
userinput = re.sub('(\?|\.|!|,)', '', userinput)
userinput = str.lower(userinput)
userinput = userinput.split('\s')
userinput = " ".join([port.stem(word) for word in userinput])
print(userinput)