Create a Markov Model that can generate text simulations by studying Donald Trump speech data set.

0 votes
I have a project and I need help with it. I have the following problem statement

"To apply Markov Property and create a Markov Model that can generate text simulations by studying Donald Trump speech data set."
Aug 1 in Machine Learning by Sameer
21 views

1 answer to this question.

0 votes

The logic here is simple. Apply Markov Property to generate Donald’s Trump’s speech by considering each word used in the speech and for each word, create a dictionary of words that are used next.

I am not just giving you the code for your project, I think you should understand the concept and I am going to try my best for that. 

1. Start with importing the required libraries, with the following command

import numpy as np

2. Read the datasets

trump = open('C://Users//NeelTemp//Desktop//demos//speeches.txt', encoding='utf8').read()
#display the data
print(trump)

3. Split the datasets into individual words

corpus = trump.split()
 #Display the corpus
print(corpus)

4. Next, create a function that generates the different pairs of words in the speeches. To save up space, we’ll use a generator object.

def make_pairs(corpus):
for i in range(len(corpus) - 1):
yield (corpus[i], corpus[i + 1])
pairs = make_pairs(corpus)

5. Next, let’s initialize an empty dictionary to store the pairs of words.

word_dict = {}
for word_1, word_2 in pairs:
if word_1 in word_dict.keys():
word_dict[word_1].append(word_2)
else:
word_dict[word_1] = [word_2]

6. Build the model. We'll randomly start picking up words from the corpus and start forming the chain. 

#randomly pick the first word
first_word = np.random.choice(corpus)
 #Pick the first word as a capitalized word so that the picked word is not taken from in between a sentence
while first_word.islower():
 #Start the chain from the picked word
chain = [first_word]
 #Initialize the number of stimulated words
n_words = 20

7. Finally, let's display the stimulated text

#Join returns the chain as a string
print(' '.join(chain))

And you are done! Congratulations. Have a look at this blog for a better understanding of this concept. 

answered Aug 1 by Zaid

Related Questions In Machine Learning

0 votes
1 answer
0 votes
1 answer

Can an ML model be recreated or retrained?

Of course, your model is retrainable, in ...READ MORE

answered May 13 in Machine Learning by Hemant
20 views
0 votes
1 answer

How do I create a decision tree?

Let us consider the following example. Suppose a ...READ MORE

answered May 13 in Machine Learning by Fatima
24 views
0 votes
0 answers

Train-test split with 20% of data set aside - ML

I have an assignment of Making a ...READ MORE

May 24 in Machine Learning by Kim
17 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 175 views
+4 votes
6 answers
0 votes
1 answer