Skip to content

Instantly share code, notes, and snippets.

@alyx
Last active December 12, 2015 08:59
Show Gist options
  • Save alyx/4748599 to your computer and use it in GitHub Desktop.
Save alyx/4748599 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import hyperclient, hashlib, random
Client = hyperclient.Client('192.157.253.11', 1982)
def add_words(String):
Words = []
for Word in String.split():
Words.append(Word)
if len(Words) == 3:
push_to_db(Words)
if len(Words):
print(str(len(Words))+"\n")
push_to_db(Words)
def push_to_db(Words):
Hash = hashlib.sha256()
if len(Words) == 3:
Previous = Words.pop(0)
Current = Words[0]
Next = Words[1]
Hash.update(Previous)
Hash.update(Current)
try:
Client.list_rpush('markov', Hash.hexdigest(), {'next':Next})
except:
Client.put('markov', str(Hash.hexdigest()), {'previous': Previous, 'current': Current, 'next': [Next]})
if len == 2:
Current = Words[0]
Next = Words[1]
Hash = hashlib.sha256()
Hash.update(Current)
try:
Client.list_rpush('markov', Hash.hexdigest(), {'next': Next})
except:
Client.put('markov', str(Hash.hexdigest()), {'current': Current, 'next':[Next]})
def follow_word(MyString, Word):
MyString.append(Word)
WordArray = [x for x in Client.search('markov', {'current':str(Word)})]
if (WordArray == []):
return 1
else:
print(len(WordArray))
W = WordArray[random.randint(0, len(WordArray)-1)]
while (W != []):
if type(W) is str:
break
W = W["next"][random.randint(0, (len(W["next"])-1))]
if (random.randint(0, 17)):
follow_word(MyString, W)
Stringthing = []
follow_word(Stringthing, "quick")
#print(Stringthing.join(' ') + "\n")
print(Stringthing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment