Python Forum
Markov Analysis not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Markov Analysis not working
#1
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:

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
Reply


Messages In This Thread
Markov Analysis not working - by 5Head - Oct-15-2020, 05:25 PM
RE: Markov Analysis not working - by deanhystad - Oct-15-2020, 07:06 PM
RE: Markov Analysis not working - by 5Head - Oct-15-2020, 10:59 PM
RE: Markov Analysis not working - by jefsummers - Oct-16-2020, 01:34 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020