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
#2
You need to tell us what you are trying to do, and I don't mean "Write a program to read a text from a file and perform Markov analysis." How is your program supposed to work? What do you expect this code to do?
    fin=open(f,encoding='utf-8') 
    l=((fin.read()).strip(string.punctuation).split(' ')) 
The comment says add combind strings to dictionary, but what combined strings? What are these strings expected to look like?
    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]);
And what does d.setdefaults(s, []) do? Do you want to do that many times?
Reply
#3
(Oct-15-2020, 07:06 PM)deanhystad Wrote: You need to tell us what you are trying to do, and I don't mean "Write a program to read a text from a file and perform Markov analysis." How is your program supposed to work? What do you expect this code to do?
    fin=open(f,encoding='utf-8') 
    l=((fin.read()).strip(string.punctuation).split(' ')) 
The comment says add combind strings to dictionary, but what combined strings? What are these strings expected to look like?
    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]);
And what does d.setdefaults(s, []) do? Do you want to do that many times?

I don't know what the complicated parts do, I just copied the code from my friend. I don't think he was finished though, so I'm trying to get some help.
Reply
#4
Write out in plain English a list of the steps you need to do in order to accomplish your task.
Then, work out the Python code to do each step. If you get stuck, we can help with the individual steps. You should not be using someone elses code that you do not understand - that is not learning, puts you farther behind, and doing that you will never catch up.
Reply


Forum Jump:

User Panel Messages

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