Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON (2.7) Question texts: https://drive.google.com/folderviewid=0Bz_aB61VIRZA

ID: 667041 • Letter: P

Question

PYTHON (2.7) Question

texts:

https://drive.google.com/folderviewid=0Bz_aB61VIRZAfnNoamZmZVdVR19TNmdmVDBwbk5aN1JsNkQyNTVRLUgwVVB2elVIUHBfcFE&usp=sharing

Assignment Description The goal of this assignment is, given an input text file, use a Markov Chain algorithm to restructure the text, and save the results to another text file A Markov Chain is a way to predict "what to do next" based on "what you did recently" (the details of the process can be found on http://en.wikipedia.org/wiki/Markov_chain) A Markov Chain maintains a fixed series of states, and based on those states determines what to do next, something like walking through a path. The number of steps that are remembered is fixed at the beginning of the process Such a method can easily be applied to bodies of text to create, humorous constructions of reasonably sensible sentences. This construction is based on the sampling of existing text to create a Markov chain. An existing document is scanned, observing the previous n words, and remembering the word that follows. Using this information, one can create a new document based on the Markov chain created from the previous document In this assignment, you are asked to construct a Markov chain that represents a Markov chain of a document given as input to vour program. For the purpose of the assiqnment you can set the value of n to two. Thus, for every word in the given document, you should record and keep in an appropriate data structure, the previous two words in the document (Please note that, same two word combination may occur before several different words in the same document; hence your data structure should be able to handle a list of words corresponding to the same word pairs) To create a new text document, the process begins by selecting the first two words from the original text file as the first two words of the new document. The word recorded originally corresponding to this two word combination will be the third word of the new document. If there are more than one words corresponding to the two word combination, then one of them is selected as randomly. Then, the second and third words from the new document are selected to form a pair, and the word recorded originally corresponding to

Explanation / Answer

comments added

-----------------------------------------------------

import sys
from pprint import pprint
from random import choice

EOS = ['.', '?', '!']


def dictionary_building(words):
temp = {}
for i, word in enumerate(words): #iterating words
try:
first, second, third = words[i], words[i+1], words[i+2] #storing words
except IndexError:
break
key = (first, second) #checking each words
if key not in d:
temp[key] = []
temp[key].append(third)
return temp


def sentence_generator(dict): #sentence Generating from words
li = [key for res in dict.keys() if res[0][0].isupper()]
res = choice(li)

list = []
one, two = res
list.append(one) #appending first two words to list
list.append(two)
while True:
try:
third = choice(d[res])
except KeyError:
break
list.append(third)
if third[-1] in EOS:
break
key = (two, third)
one, two = res

return ' '.join(li) #returning in string text form


def main():
filename = "test.txt" #you can give your own file name
with open(filename, "r") as f: #reading file
text = f.read()

words = text.split() #splitting sentence
dictionary = dictionary_building(words) #build dictionary for those words
pprint(dictionary)
print()
sent = sentence_generator(dictionary)#sentence generating fromdictionary words
print(sent)