How to make this random text generator more efficient in Python?

巧了我就是萌 提交于 2019-12-04 14:23:16

Some suggested improvements:

  1. The while loop will run forever, you should probably remove it.
  2. Use max and generator expressions to generate the longest word in a memory-efficient manner.
  3. 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.

    sents = [" ".join(sent) for sent in listOfSents if longestWord in sent and len(sent) > 40]

  4. 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

If all you need is generate random text (I guess, with requirement that it should contain meaningful sentences) you can do it much simpler: Just generate random numbers and use them as index to retrieve sentences from your text database (be it Project Gutenberg or whatever).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!