12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

130 Chapter 13. Casestudy: datastructure selectiondef random_word(h):t = []for word, freq in h.items():t.extend([word] * freq)return random.choice(t)The expression [word] * freq creates a list with freq copies of the string word. The extendmethod issimilartoappendexcept that theargument isasequence.Exercise 13.7 This algorithm works, but it is not very efficient; each time you choose a randomword, it rebuilds the list, which is as big as the original book. An obvious improvement is to buildthelistonce and then make multipleselections, but thelistisstillbig.Analternative is:1. Usekeystoget alistof the words inthebook.2. Buildalistthatcontainsthecumulativesumofthewordfrequencies(seeExercise10.1). Thelast item inthis lististhe total number of words inthebook, n.3. Choose a random number from 1 to n. Use a bisection search (See Exercise 10.8) to find theindex where the random number would beinserted inthecumulative sum.4. Usethe index tofind thecorresponding wordinthe wordlist.Writeaprogram that uses thisalgorithm tochoose arandom wordfrom thebook.13.8 MarkovanalysisIf you choose words from the book at random, you can get a sense of the vocabulary, you probablywon’t get asentence:this the small regard harriet which knightley's it most thingsA series of random words seldom makes sense because there is no relationship between successivewords. For example, in a real sentence you would expect an article like “the” to be followed by anadjective oranoun, and probably not averb or adverb.One way to measure these kinds of relationships is Markov analysis 1 , which characterizes, for agiven sequence of words, the probability of the word that comes next. For example, the song Eric,theHalf a Bee begins:Half abee, philosophically,Must, ipsofacto, half not be.But half thebee has got tobeVisavis, itsentity. D’you see?But can abee be saidtobeOr not tobe an entirebeeWhen half thebee isnot abeeDue tosome ancient injury?1 This case study is based onanexamplefromKernighanand Pike, The Practice of Programming, 1999.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!