from lab05 import * ## Optional Questions ## # pyTunes (optional) def delete(t, target): """Returns the tree that results from deleting TARGET from t. If TARGET is a category, delete everything inside of it. >>> my_account = tree('kpop_king', ... [tree('korean', ... [tree('gangnam style'), ... tree('wedding dress')]), ... tree('pop', ... [tree('t-swift', ... [tree('blank space')]), ... tree('uptown funk'), ... tree('see you again')])]) >>> new = delete(my_account, 'pop') >>> print_tree(new) kpop_king korean gangnam style wedding dress """ "*** YOUR CODE HERE ***" # Shakespeare and Dictionaries def build_successors_table(tokens): """Return a dictionary: keys are words; values are lists of successors. >>> text = ['We', 'came', 'to', 'investigate', ',', 'catch', 'bad', 'guys', 'and', 'to', 'eat', 'pie', '.'] >>> table = build_successors_table(text) >>> sorted(table) [',', '.', 'We', 'and', 'bad', 'came', 'catch', 'eat', 'guys', 'investigate', 'pie', 'to'] >>> table['to'] ['investigate', 'eat'] >>> table['pie'] ['.'] >>> table['.'] ['We'] """ table = {} prev = '.' for word in tokens: if prev not in table: "*** YOUR CODE HERE ***" "*** YOUR CODE HERE ***" 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 ['.', '!', '?']: "*** YOUR CODE HERE ***" return result + word def shakespeare_tokens(path='shakespeare.txt', url='http://composingprograms.com/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 shakespeare.read().decode(encoding='ascii').split() # Uncomment the following two lines # tokens = shakespeare_tokens() # table = build_successors_table(tokens) def random_sent(): import random return construct_sent(random.choice(table['.']), table)