Skip to content

Instantly share code, notes, and snippets.

@Lambdanaut
Created September 22, 2015 02:27
Show Gist options
  • Select an option

  • Save Lambdanaut/786923a53d9ec895cd73 to your computer and use it in GitHub Desktop.

Select an option

Save Lambdanaut/786923a53d9ec895cd73 to your computer and use it in GitHub Desktop.
import random
class MM(object):
def __init__(self):
# Wordlist form = ['this','is','words']
# Graph form = [((1,1)],[(2,1)],[]]
# [((count, index)],[(count, index)],[]]
self.wordlist = []
self.graphlist = []
def get_words(self, i):
i = ''.join(c for c in i if c.isalnum() or c == ' ' or c=='.').lower()
words = i.split(' ')
return words
def add_to_graph(self, word, next_word=None):
if next_word is not None:
try:
next_word_i = self.wordlist.index(next_word)
except ValueError:
# Next word hasn't been indexed
next_word_i = self.add_to_graph(next_word)
try:
# Word has been indexed
word_i = self.wordlist.index(word)
word_neighbors = self.graphlist[word_i]
if next_word is not None:
# Messy updating code
next_word_neighbors_i = 0
for _, i in word_neighbors:
if i == next_word_i:
break
else:
next_word_neighbors_i += 1
else:
# First time next_word has appeared after word
word_neighbors.append([0, next_word_i])
next_word_neighbors_count, _ = word_neighbors.pop(next_word_neighbors_i)
word_neighbors.append((next_word_neighbors_count + 1, next_word_i))
self.graphlist[word_i] = word_neighbors
except ValueError:
# Word hasn't been indexed
if next_word is None:
self.graphlist.append([])
else:
self.graphlist.append([(1, next_word_i)])
self.wordlist.append(word)
word_i = len(self.wordlist) - 1
self.graphlist[word_i].sort(reverse=True)
return word_i
def learn(self, i):
words = self.get_words(i)
for w in range(len(words)):
try:
next_word = words[w+1]
except: break
word = words[w]
self.add_to_graph(word, next_word)
def generate(self, starting_word=None, max_cycles=5000):
generated = ''
if starting_word is None:
word = random.choice(self.wordlist)
else:
word = starting_word
for cycle in range(max_cycles):
generated += '{} '.format(word)
try:
word_i = self.wordlist.index(word)
except ValueError:
'ERROR'
word_neighbors = self.graphlist[word_i]
try:
next_word_i = int(max(0, min(len(word_neighbors)-1, random.gauss(0, len(word_neighbors)/2))))
_, best_neighbor = word_neighbors[next_word_i]
except IndexError:
break
word = self.wordlist[best_neighbor]
return generated
if __name__ == '__main__':
mm = MM()
f = open('lambdanaut.txt', 'r')
mm.learn(f.read())
while True:
i = raw_input('Input?')
mm.learn(i)
print mm.generate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment