Markov chain project_ please review


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<In what way does your code behave incorrectly? Include ALL error messages.>

No errors from what I tested. I believe my python program works fine

# -*- Encoding:Latin-1 -*- import random from fetch_data import find_substring from markov_python.cc_markov import MarkovChain from urllib.request import urlopen

#my_file = open(“1.txt”,“r”)
#text = my_file.read()
#my_file.close()

#my_file2 = open(“2.txt”,“r”)
#text += my_file2.read()
#my_file2.close()

#my_file3 = open(“3.txt”,“r”)
#text += my_file3.read()
#my_file3.close()

response = urlopen(“http://python.org/”)
html = str(response.read())

#print(find_substring(‘Harry’, text))
target = ‘python’
target_binary = ‘:’.join(format(ord©, ‘b’) for c in target)
print(target_binary)

html_binary = ‘:’.join(format(ord©, ‘b’) for c in html)

(indices, choices ) = find_substring(target,html)
print (len(choices))
print( choices[random.randrange(len(choices))])

html_str = “”"

Markov Chain

%s

""" % (choices[random.randrange(len(choices))])

Html_file= open(“output.html”,“a”)
Html_file.write(html_str)
Html_file.close()

-- Encoding:Latin-1 --

#
#Returns list of indices where substring begins in string

#>>> find_substring('me', "The cat says meow, meow")
#[13, 19]

def find_substring(substring, string):
indices = []
rests= []
index = -1 # Begin at -1 so index + 1 is 0
while True:
# Find next index of substring, by starting search from index + 1
index = string.find(substring, index + 1)
count = 0
while True :
if (index+ 2+count) >= len(string):
break
elif string[index + 1 + count] in (".",",", “;”,"?","!",":", “<”,">"):
rest = string[(index ): (index + 2+ count) ]
break

		count += 1
	
	if index == -1:  
		break  # All occurrences have been found
	indices.append(index)
	rests.append(rest)
rests.sort()	
return indices, rests