Random words

To choose a random word from the histogram, the simplest algorithm is to build a list with multiple copies of each word, according to the observed frequency, and then choose from the list:

                       

The expression[word] * freq creates a list with freq copies of the string word (actually, to be more precise, the elements are references to the same string). The extend method is similar to append expect that the argument is a sequence.

This algorithm works, but it is wildly inefficient; each time you choose a random word, it rebuilds the list, which is as big as the original book.

If you generate a series of words from the book, you can get a sense of the vocabulary, but it probably won’t make much sense. The next section is about generating random text that makes more sense.

 

 

from Thinking in Python

posted @ 2014-08-17 18:22  平静缓和用胸音说爱  阅读(291)  评论(0编辑  收藏  举报