-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgenerate_word.py
44 lines (34 loc) · 1009 Bytes
/
generate_word.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# example of word generation using trigrams
import abbrase
import random
import re
graph = abbrase.WordGraph('wordlist_bigrams.txt')
words = [' %s.' % word.lower()
for word in graph.wordlist if re.match('^[A-za-z]*$', word)]
# find common trigrams
trigrams = set()
for word in words[:10000]:
for pos in xrange(0, len(word) - 2):
trigrams.add(word[pos:pos + 3])
trigrams = sorted(trigrams)
print len(trigrams)
def gen_word(length):
gen = ' '
while True:
possible = [ngram for ngram in trigrams if ngram.startswith(gen[-2:])]
if len(gen) == length:
if any('.' in word for word in possible):
return gen
else:
# restart
possible = []
if not possible:
gen = ' '
continue
choice = random.choice(possible)
if len(gen) == 1:
gen = choice[1:]
else:
gen += choice[-1]
for _ in range(20):
print gen_word(10),