Mercurial > hg > bitsyauth
comparison bitsyauth/dictionary.py @ 41:d4522af2c6e5
refactor
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sat, 28 Dec 2013 12:56:06 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
40:cb1d21648588 | 41:d4522af2c6e5 |
---|---|
1 import random | |
2 | |
3 class Dictionary(object): | |
4 def __init__(self, dictionary_file='/usr/share/dict/american-english'): | |
5 self.dictionary_file = dictionary_file | |
6 with open(dictionary_file) as f: | |
7 self.wordlist = [] | |
8 for line in f: | |
9 line = line.strip() | |
10 if not line or not line.isalpha(): | |
11 continue | |
12 line = line.lower() | |
13 self.wordlist.append(line) | |
14 self.wordlist = tuple(self.wordlist) | |
15 self._min_lengths = {} | |
16 | |
17 def random_word(self, min_length=5): | |
18 """ | |
19 choose random word | |
20 | |
21 min_length -- minimum word length | |
22 """ | |
23 | |
24 if min_length not in self._min_lengths: | |
25 # cache | |
26 self._min_lengths[min_lengths] = [i for i in self.wordlist | |
27 if len(i) > min_length] | |
28 return random.Random().choice(self._min_lengths[min_length]) | |
29 |