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'