random statement generation with python trie
#statement trie
class strie (object):
def __init__(self):
self.trie = {}
def __repr__(self):
return str(self.trie)
def __str__(self):
return str(self.trie)
def statement(self, phrase):
words = phrase.split()
current = self.trie
while len(words) > 0:
word = words.pop(0)
if word in current.keys():
current = current[word]
else:
current[word] = {}
current = current[word]
def random_phrase(self, length):
import random
i = 0
phrase = ""
current = self.trie
while i < length:
path = random.choice(list(current.keys()))
phrase += path + " "
i += 1
current = current[path]
return phrase