Skip to content

Instantly share code, notes, and snippets.

@1328
Created October 16, 2014 23:40
Show Gist options
  • Select an option

  • Save 1328/976f27262becf7f05eb4 to your computer and use it in GitHub Desktop.

Select an option

Save 1328/976f27262becf7f05eb4 to your computer and use it in GitHub Desktop.
round 3
from string import ascii_lowercase
from collections import defaultdict
from itertools import zip_longest
from pprint import pprint
from random import random
import bisect
class Matrix(object):
def __init__(self, words = None):
self.top = defaultdict(int)
self.m = defaultdict(lambda: defaultdict(int))
self.sto = None
if words is None:
return
for word in words:
self.add(word)
def add(self, word):
# add in the first letter into the top matrix
self.top[word[0]] += 1
# now add sequences
# - marks end of word for more natural endings
for a,b in zip_longest(word, word[1:], fillvalue = '-'):
self.m[a][b] += 1
def build_part_sto(self, subm):
total = sum([v for v in subm.values()])
matrix = {l:c/total for l,c in subm.items()}
return matrix
def build_sto(self):
self.top_sto = self.build_part_sto(self.top)
self.sto = {}
for letter in self.m:
self.sto[letter] = self.build_part_sto(self.m[letter])
def pick_from_sto(self, sto):
options = sorted(sto.items(), key = lambda x:x[1])
x = random()
total = 0
for c, item in enumerate(options):
total += item[1]
if x < total:
break
return options[c][0]
def pick_letter(self, predecessor = None):
if predecessor is None:
return self.pick_from_sto(self.top_sto)
return self.pick_from_sto(self.sto[predecessor])
def random_word(self, length = 5, minimum = 3):
'''
create a random word with specified max and min lenghts
words ends naturally based on '-'s
note: first letter is picked randomly based on precidence
'''
last = None
result = ''
for count in range(1, length):
letter = self.pick_letter(last)
if letter == '-':
if len(result)<minimum:
continue
break
result += letter
last = letter
return result
def main():
m = Matrix()
with open('dictionary.txt', mode='r') as fh:
for word in fh:
word = word.strip()
m.add(word)
m.build_sto()
for i in range(10):
print(m.random_word())
return
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment