Assignment Solution

In this lesson, we will discuss the solutions for the assignments in this chapter.

We'll cover the following...

Problem one

Build a model with K = 6, K = 8 and K = 10 and check the text generated.

We will use the same code and functions that we created in our previous lessons.

Press + to interact
import numpy as np
# Generate the Lookup Table
def generateTable(data,k):
T = {}
for i in range(len(data)-k):
X = data[i:i+k]
Y = data[i+k]
if T.get(X) is None:
T[X] = {}
T[X][Y] = 1
else:
if T[X].get(Y) is None:
T[X][Y] = 1
else:
T[X][Y] += 1
return T
# Convert the Frequencies into Probability
def convertFreqIntoProb(T):
for kx in T.keys():
s = float(sum(T[kx].values()))
for k in T[kx].keys():
T[kx][k] = T[kx][k]/s
return T
# Load the training data
def load_text(filename):
with open(filename,encoding='utf8') as f:
return f.read().lower()
# Create the Markov Chains
def MarkovChain(text,k):
T = generateTable(text,k)
T = convertFreqIntoProb(T)
return T
# Perform Sampling
def sample_next(ctx,model,k):
ctx = ctx[-k:]
if model.get(ctx) is None:
return " "
possible_Chars = list(model[ctx].keys())
possible_values = list(model[ctx].values())
return np.random.choice(possible_Chars,p=possible_values)
# Generate the Text
def generateText(starting_sent,k ,maxLen):
model = MarkovChain(text, k)
sentence = starting_sent
ctx = starting_sent[-k:]
for ix in range(maxLen):
next_prediction = sample_next(ctx,model,k)
sentence += next_prediction
ctx = sentence[-k:]
return sentence
text = load_text("train_corpus.txt")
generated_text = generateText("dear my countrymen",k=8,maxLen=2000)
print(generated_text)

Explanation: ...