Python snippet to get list of words

February 20, 2008

Here are a quick little snippet to get a Python generator object of the words from the Unix wordfile:

def get_wordgen(fname="/usr/share/dict/words"):
    return (line.strip() for line in open(fname))

You can pass this through Set() if you need to do membership tests (in a simple spell check, for example), or even use list() if you need the whole thing in memory.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: