I wrote codes for text generating from a given text file. I use markov first order model. First create dictionary from text file. In case of punctuation ('.','?','!') it key is '$'. After creating dictionary I generate text randomly from the created dictionary. When it detect '$' it start new sentence. My code is given below:
import random
def createDictionary(fileName):
'''Creates dictionary with following words for a word using a text input'''
file = open(fileName, "r")
text = file.read()
file.close()
LoW = text.split()
LoW = ["$"] + LoW
wd = {}
index = 0
while index < len(LoW): ##Make dictionary entries
word = LoW[index]
if word not in wd:
if word[-1] == "?" or word[-1] =="." or word[-1] =="!":
word = "$"
wd[word] = []
index += 1
index = 0
while index < (len(LoW) - 1): #Make list for each of those entries
word = LoW[index]
if word[-1] == "?" or word[-1] =="." or word[-1] =="!":
word = "$"
nextWord = LoW[index + 1]
wd[word] += [nextWord]
index += 1
return wd
def generateText(d,n):
"""
Return a genWord no more than the specified length using a first_order Markov model
"""
current_word = random.choice(d['$'])
genWord = current_word
for i in range(n-1):
if current_word not in d:
break
next_word = random.choice(d[current_word])
current_word = next_word
genWord = genWord + " " + next_word
return genWord
My text file('a.txt') is:
I am doing markov first order text processing in python. Will it work in my code? Nor I seek help from someone. I believe I made a naive mistake! but I could not fix it.
Input: d = createDictionary( 'a.txt' )
print generateText(d,50)
Output: 1 line out of 4 randomly.
Could anyone suggest me how could I fix this code so that it correctly generate inputted text?