Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I am trying to understand the markov chain. This is my code:

import numpy as np

import random as rm

#import io

trump = open('C:\Users\sharma\PycharmProjects\machine-learning\speeches.txt').read()

# display the data

#print(trump)

#split the data

corpus = trump.split()

#print(corpus)

#Make pairs

def make_pairs(corpus):

    for i in range(len(corpus) - 1):

        yield (corpus[i] , corpus[i + 1])

pairs = make_pairs(corpus)

#append dictionary

word_dict = {}

for word1, word2 in pairs:

    if word1 in word_dict:

        word_dict[word1].append(word2)

    else:

        word_dict[word1] = [word2]

#Randomly pick first word

first_word = np.random.choice(corpus)

while first_word.islower():

    chain = [first_word]

    n_words = 20

    for i in range(n_words):

        chain.append(np.random.choice(word_dict[chain - 1]))

        print (' '.join(chain))

Ending up with the following error:

chain.append(np.random.choice(word_dict[chain - 1]))

TypeError: unsupported operand type(s) for -: 'list' and 'int'

1 Answer

0 votes
by (25.1k points)

The issue with your code is that on the last second line you are trying to subtract 1 from a list, which is you you are getting this error. I think what you are trying to do is this:

chain.append(np.random.choice(word_dict[chain[-1]]))

If you wish to get a more in depth understanding of python you can watch this YouTube video:

Browse Categories

...