Question: fix the following python code so that it does the following-all that's left is to run our functions with tokens from a text corpus. The

fix the following python code so that it does the following-all that's left is to run our functions with tokens from a text corpus. The following snippets included in the skeleton code will return a list containing the word tokens in all of the works of Shakespeare and all the tweets of Trump.

their is a loop somewhere that is a infinite loop and I cant find it other then that the code should work

make sure it runs

def construct_tweet(word, table): """Prints a random sentence starting with word, sampling from table. """ import random result = ' ' S=' ' while word not in ['.', '!', '?']: result += word + ' ' word == random.choice(table[word]) 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()

def trump_tokens(path='trumptweets.txt', url='http://pastebin.com/raw/nWvFKcH7'): """Return the words found in tweets of Trump as a list.""" import os from urllib.request import urlopen if os.path.exists(path): return open('trumptweets.txt', encoding='ascii').read().split() else: trump = urlopen(url) return trump.read().decode(encoding='ascii').split()

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 i in tokens: if prev not in table: table[prev]=[] table[prev]+=[i] prev=i return table def random_tweet(table): import random return construct_tweet(random.choice(table['.']), table)

# Uncomment the following lines shakestokens = shakespeare_tokens() shakestable = build_successors_table(shakestokens) trumptokens = trump_tokens() trumptable = build_successors_table(trumptokens) construct_tweet('The', shakestable) " The plebeians have done us must be news-cramm'd "

construct_tweet('The', shakestable) " The ravish'd thee , with the mercy of beauty "

construct_tweet('The', trumptable) " The State of Justice Ginsburg going to Nominate Congressman John Kasich B+, Kasich 13% Bush called terrible (and boring) ."

construct_tweet('The', trumptable) " The CIA Chief John Lewis should be an Independent, say that Podesta & keep & another four more in fact, it approved "

random_tweet(shakestable) " Long live by thy name , then , Dost thou more angel , good Master Deep-vow , And tak'st more ado but following her , my sight Of speaking false !"

random_tweet(shakestable) " Yes , why blame him , as is as I shall find a case , That plays at the public weal or the ghost ."

random_tweet(trumptable) " Will devote ZERO investments in Toledo, Ohio Funny how bad to happen - so many millions of the future, Donald Trump Intends to #MakeAmericaGreatAgain ! "

random_tweet(trumptable) " They say it as this simple fact that I have no action or in her poor performance by a genius in record . " for _ in range(5): print("ShakespeareBot: ", random_tweet(shakestable)); print() print("TrumpBot: ", random_tweet(trumptable)); print() def _test(): import doctest doctest.testmod(verbose=True)

if __name__ == "__main__": _test()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!