Created
October 15, 2012 23:16
-
-
Save dtuominen/3896280 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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