[Tutor] Random Words.

Rick Pasotto rick@niof.net
Sun, 3 Jun 2001 18:48:50 -0400


How about:

import random,string,os
stat = os.stat('/usr/share/dict/words')
# the filesize if the 7th element of the array
flen = stat[6]
f = open('/usr/share/dict/words')
words = []
while len(words) < 5:
# seek to a random offset in the file
	f.seek(int(random.random() * flen))
# do a single read with sufficient characters
	chars = f.read(50)
# split it on white space
	wrds = string.split(chars)
# the first element may be only a partial word so use the second
# you can also make other tests on the word here
	if len(wrds[1]) > 3 and len(wrds[1]) < 9:
	   words.append(wrds[1])
print words

This uses minimal memory and I suspect is about as fast as you're
going to get.
	
On Sun, Jun 03, 2001 at 05:13:47PM -0500, Tesla Coil wrote:
> Way to read the file more efficiently if it were
> larger? (/usr/share/dict/words has 45,424 entries;
> I also have /usr/dict/words with 79,339 entries,
> and /usr/dict/words.ott with 172,822).  
> 
> Where would be preferable to filter out undesired 
> words?  Remove them from the list before choosing,
> or, reject them if chosen and replace them?  I'm 
> inclined toward the latter, but wonder if that's
> best when there's fair chance of rejection, say,
> refusing len(words) <=3 and >=9.
> 
> And just interested in any different approach...
> 
> #!/usr/bin/env python
> # randword.py -- Quick and Dirty  
> # return a list of random words.
> import random
> 
> def readfile(lexfile):
>     file = open(lexfile)
>     lexlist = file.readlines()
>     file.close()
>     return lexlist
> 
> def choosewords(lexlist):
>     randwords = []
>     entries = len(lexlist)
>     for iteration in range(20):
>     	word = random.randint(0, entries)
> 	randwords.append(wordlist[word])
>     return randwords	
> 
> wordsource = '/usr/share/dict/words'
> wordlist = readfile(wordsource)
> randlist = choosewords(wordlist)
> for word in randlist: print word,
>

-- 
When under the pretext of fraternity, the legal code imposes
mutual sacrifices on the citizens, human nature is not thereby
abrogated. Everyone will then direct his efforts toward
contributing little to, and taking much from, the common fund of
sacrifices. Now, is it the most unfortunate who gains from this
struggle? Certainly not, but rather the most influential and
calculating.
	-- Frédéric Bastiat (1801-1850)
    Rick Pasotto    rickp@telocity.com    http://www.niof.net