Start paragraphs with any string

This commit is contained in:
Joscha 2016-09-19 05:42:42 +00:00
parent 932da577dd
commit 4fc83cff57

View file

@ -26,7 +26,7 @@ class Markov:
def hiccup(self, before): def hiccup(self, before):
if before not in self.matrix: if before not in self.matrix:
before = random.choice(self.matrix.keys()) before = random.choice(list(self.matrix))
selection = self.matrix[before] selection = self.matrix[before]
choice = random.randrange(sum(selection.values())) choice = random.randrange(sum(selection.values()))
@ -37,9 +37,7 @@ class Markov:
if choice < counter: if choice < counter:
return char return char
def regurgitate_line(self): def regurgitate_line(self, line=""):
line = ""
while True: while True:
before = line[-self.before:] before = line[-self.before:]
char = self.hiccup(before) char = self.hiccup(before)
@ -50,15 +48,24 @@ class Markov:
else: else:
line += char line += char
def regurgitate(self, lines=1): # set start if you want to have the lines start with a particular word/string
def regurgitate(self, lines=1, start=""):
speech = "" speech = ""
for i in range(lines): for i in range(lines):
speech += self.regurgitate_line() speech += self.regurgitate_line(line=start)
return speech return speech
def save(self, filename):
with open(filename, "w") as f:
json.dump(self.matrix, f)
def load(self, filename):
with open(filename) as f:
self.matrix = json.load(f)
def main(filename, paragraphs, char_lookback): def main(filename, paragraphs=1, char_lookback=10, start=""):
markov = Markov(char_lookback) markov = Markov(char_lookback)
markov.feed(filename) markov.feed(filename)
@ -66,7 +73,7 @@ def main(filename, paragraphs, char_lookback):
print("Entries:", len(markov.matrix)) print("Entries:", len(markov.matrix))
print("Regurgitating!") print("Regurgitating!")
print(markov.regurgitate(paragraphs)) print(markov.regurgitate(paragraphs, start))
if __name__ == "__main__": if __name__ == "__main__":
try: try:
@ -79,4 +86,9 @@ if __name__ == "__main__":
except (IndexError, ValueError): except (IndexError, ValueError):
char_lookback = 10 char_lookback = 10
main("trump_speech.txt", paragraphs, char_lookback) try:
start = sys.argv[3]
except IndexError:
start = ""
main("trump_speech.txt", paragraphs, char_lookback, start)