Skip to content

Instantly share code, notes, and snippets.

@newkdukem
Last active August 29, 2015 14:25
Show Gist options
  • Save newkdukem/82452661ede2ea34c33e to your computer and use it in GitHub Desktop.
Save newkdukem/82452661ede2ea34c33e to your computer and use it in GitHub Desktop.
import re
import pprint
from collections import Counter
import random
Rechnik = {}
txtfile = re.findall(r'\w+', open('hamlet.txt').read().lower())
def give_me_pairs(words):
prev = None
for word in words:
if not prev is None:
yield prev, word
prev = word
def weightedRandom(counte):
dalist = list(counte.elements())
return random.choice(dalist)
for segashen, sleden in give_me_pairs(txtfile):
if not segashen in Rechnik:
Rechnik[segashen] = Counter()
cnt = Rechnik[segashen]
cnt[sleden] += 1
i = 0
# wordot = random.choice(Rechnik.keys())
wordot = raw_input("Say thy word: ")
if not wordot:
wordot = random.choice(Rechnik.keys())
while i < 15:
print wordot + " "
wordot = weightedRandom(Rechnik[wordot])
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment