dictionary - markov first order text processing in python -


i wrote codes text generating given text file. use markov first order model. first create dictionary text file. in case of punctuation ('.','?','!') key '$'. after creating dictionary generate text randomly created dictionary. when detect '$' start new sentence. code given below:

import random  def createdictionary(filename):      '''creates dictionary following words word using 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 each of 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 genword no more specified length using first_order markov model                              """      current_word = random.choice(d['$'])     genword = current_word     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 doing markov first order text processing in python. work in code? nor seek someone. believe made naive mistake! not fix it.

input: d = createdictionary( 'a.txt' )

print generatetext(d,50)

output: 1 line out of 4 randomly.

could suggest me how fix code correctly generate inputted text?


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -