更新时间:2023-11-29 16:09:10
一些建议的改进:
while
循环将永远运行,您应该删除它.max
和生成器表达式以节省内存的方式生成最长的单词.您应生成一个长度大于40个字符的句子列表,其中包括具有列表理解的longestWord
.这也应该从while循环中删除,因为它只会发生.
while
loop will run forever, you should probably remove it.max
and generator expressions to generate the longest word in a memory-efficient manner.You should generate a list of sentences with a length greater than 40 characters that include longestWord
with a list comprehension. This should also be removed from the while loop, as it only happens.
已发送= [" ".join(sent) for sent in listOfSents if longestWord in sent and len(sent) > 40]
如果要打印出以随机顺序找到的每个句子,则可以尝试改组刚刚创建的列表:
If you want to print out every sentence that is found in a random order, then you could try shuffling the list you just created:
for sent in random.shuffle(sents): print sent
这是这些更改后代码的外观:
This is how the code could look with these changes:
import nltk
from nltk.corpus import gutenberg
from random import shuffle
listOfSents = gutenberg.sents()
triggerSentence = raw_input("Please enter the trigger sentence: ")
longestWord = max(triggerSentence.split(), key=len)
longSents = [" ".join(sent) for sent in listOfSents
if longestWord in sent
and len(sent) > 40]
for sent in shuffle(longSents):
print sent