Skip to content

Instantly share code, notes, and snippets.

@dtuominen
Created October 15, 2012 23:16
Show Gist options
  • Save dtuominen/3896280 to your computer and use it in GitHub Desktop.
Save dtuominen/3896280 to your computer and use it in GitHub Desktop.
import random
from collections import defaultdict
def get_words(fname='fyad.txt'):
with open(fname) as f:
return f.read().split()
def triplets(words):
for i in xrange(len(words) - 2):
yield (words[i], words[i+1], words[i+2])
def chain(words):
d = defaultdict(list)
for w1, w2, w3 in triplets(words):
pair = (w1, w2)
d[pair].append(w3)
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment