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 33 views

1 answer to this question.

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

Why data set always divided in training data and test data in regressions?

Let me explain the process of prediction ...READ MORE

Can an ML model be recreated or retrained?

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

How do I create a decision tree?

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

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

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

how can i randomly select items from a list?

if you google it you can find. ...READ MORE

+1 vote

how can i count the items in a list?

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

how do i use the enumerate function inside a list?

can you give an example using a ...READ MORE

Lowercase in Python

You can simply the built-in function in ...READ MORE