Created
April 8, 2015 11:56
-
-
Save cpressey/c590e0a0e5b61f4b3e7c to your computer and use it in GitHub Desktop.
Fizzy Drinks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i had; | |
was a on the of the in the; | |
upon the | |
of his, | |
in his i could he had! | |
with a, | |
began to! | |
and i i was and the the squire | |
he was. | |
by the said the | |
the captain out of the doctor as i! | |
had been it was! | |
there was with the in a! | |
was the and a. | |
at the of a like a! | |
from the, | |
to be | |
for the to the and then into the; | |
that i. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import re | |
import sys | |
import random | |
def main(argv): | |
filenames = argv[1:] | |
def clean(word): | |
if word.endswith(('.', '!', '?', ';', ',')): | |
word = word[:-1] | |
if word.startswith(('"', "'", '(')): | |
word = word[1:] | |
if word.endswith(('"', "'", ')')): | |
word = word[:-1] | |
if word.endswith(('.', '!', '?', ';', ',')): | |
word = word[:-1] | |
return word.lower() | |
words = [] | |
for filename in filenames: | |
with open(filename, 'r') as f: | |
for line in f: | |
bits = line.strip().split() | |
for bit in bits: | |
for smidge in bit.split('--'): | |
words.append(clean(smidge)) | |
freq = {} | |
last = [] | |
for word in words: | |
if len(last) < 2: | |
last.append(word) | |
continue | |
key = (last[0], last[1]) | |
freq[key] = freq.setdefault(key, 0) + 1 | |
last.pop(0) | |
last.append(word) | |
freq_list = sorted([(v, k) for k, v in freq.iteritems() if v > 1], reverse=True) | |
freq_list = ['%s %s' % (v1, v2) for k, (v1, v2) in freq_list[0:40]] | |
random.shuffle(freq_list) | |
while freq_list: | |
num = random.randint(1, 5) | |
some = freq_list[:num] | |
freq_list = freq_list[num:] | |
line = ' '.join(some) | |
if not freq_list: | |
line += '.' | |
else: | |
line += random.choice([',', '.', ';', '', '!']) | |
print line | |
if __name__ == '__main__': | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment