Write a program to read a text from a file and perform Markov analysis. The result should be
a dictionary that maps from prefixes to a collection of possible suffixes. The collection might
be a list, tuple, or dictionary; it is up to you to make an appropriate choice. You can test your
program with prefix length two, but you should write the program in a way that makes it easy
to try other lengths.
Here's what I have so far:
Here is the file: http://gutenberg.org/files/63437/63437-0.txt
a dictionary that maps from prefixes to a collection of possible suffixes. The collection might
be a list, tuple, or dictionary; it is up to you to make an appropriate choice. You can test your
program with prefix length two, but you should write the program in a way that makes it easy
to try other lengths.
Here's what I have so far:
import string def word_dict(f,length): # takes length and performs markov analysis, returns dictionary fin=open(f,encoding='utf-8') l=((fin.read()).strip(string.punctuation).split(' ')) d={} for i in range(len(l)-length-1): # adds combined strings to dictionary, then assigns the word following it to the key s=' '.join(l[i:i+length]) d.setdefault(s,[]).append(l[i+length]); return d print(word_dict('words.txt',3))]It's not working correctly.
Here is the file: http://gutenberg.org/files/63437/63437-0.txt