""" CS61A Summer 2012 Skeleton code, intended for use in Lab 5. Uses IDicts instead of the built-in dict. """ from idict import idict_select, idict_insert def build_successors_table(tokens): """Return a dictionary: keys are words; values are tuples of successors.""" table = make_idict() prev = '.' for word in tokens: if idict_select(table, prev): "**FILL THIS IN**" else: "**FILL THIS IN**" prev = word return table def construct_sent(word, table): """Prints a random sentence starting with word, sampling from table""" import random result = ' ' while word not in ('.', '!', '?'): "** FILL THIS IN**" return result + word def shakespeare_tokens(path = 'shakespeare.txt', url = 'http://inst.eecs.berkeley.edu/~cs61a/fa11/shakespeare.txt'): """Return the words of Shakespeare's plays as a list""" import os from urllib.request import urlopen if os.path.exists(path): return open('shakespeare.txt', encoding='ascii').read().split() else: shakespeare = urlopen(url) # Return first 2000 words, to avoid bringing IDicts to its # knees (originally returned ~900,000 words!) return shakespeare.read().decode(encoding='ascii').split()[:2000]