creative coding jam
#!/usr/bin/python | |
import random, sys | |
from numpy.random import choice | |
bible = open("bible.txt").read() | |
com = open("com.txt").read() | |
di = None | |
d = {} | |
bible_words = bible.split() | |
for i in range(len(bible_words)-1): | |
current_word = bible_words[i] | |
next_word = bible_words[i+1] | |
current_word_dict = d.get(current_word, {}) | |
current_word_dict[next_word] = current_word_dict.get(next_word, 0) + 1 | |
d[current_word] = current_word_dict | |
com_words = com.split() | |
c = {} | |
for i in range(len(com_words)-1): | |
current_word = com_words[i] | |
next_word = com_words[i+1] | |
current_word_dict = c.get(current_word, {}) | |
current_word_dict[next_word] = current_word_dict.get(next_word, 0) + 1 | |
c[current_word] = current_word_dict | |
di = d | |
#import pprint | |
#pp = pprint.PrettyPrinter(indent=4) | |
#pp.pprint(d) | |
def pg(word): | |
global di | |
keys = di[word].keys() | |
values =di[word].values() | |
pe = [n/sum(values) for n in values ] | |
cho = choice(list(keys), 1, pe) | |
#print (word, list(keys), pe, cho) | |
return cho[0] | |
def ps(word): | |
global di | |
keys = di[word].keys() | |
values =di[word].values() | |
pe = [ 1-(n/sum(values)) for n in values ] | |
cho = choice(list(keys), 1, pe) | |
#print (word, list(keys), pe, cho) | |
return cho[0] | |
import time | |
import curses | |
import os | |
def main(win): | |
global di, c, d | |
word = random.choice(bible_words) | |
win.nodelay(True) | |
key="" | |
win.clear() | |
while 1: | |
try: | |
key = win.getkey() | |
if key == os.linesep: | |
break | |
old_position = position = win.getyx() | |
if key == 'w': | |
position = (position[0], position[1]-len(word)) | |
position = (position[0]-1, position[1]) | |
word = pg(word) | |
if key == 's': | |
position = (position[0], position[1]-len(word)) | |
position = (position[0]+1, position[1]) | |
word = ps(word) | |
if key == 'a': | |
position = (position[0], position[1]-len(word)) | |
word = ps(word) | |
position = (position[0], position[1]-len(word)-1) | |
if key == 'd': | |
position = (position[0], position[1]+1) | |
word = pg(word) | |
if key == ' ': | |
win.clear() | |
if key == '1': | |
di = d | |
word = random.choose(bible_words) | |
if key == '2': | |
di = c | |
word = random.choose(com_words) | |
if position != old_position: | |
win.addstr(position[0], position[1], word) | |
except Exception as e: | |
# No input | |
pass | |
from importd import d as de | |
de(DEBUG=True) | |
@de("/pg") | |
def pgg(request): | |
return de.HttpResponse(pg(request.GET['word'])) | |
@de("/ps") | |
def pss(request): | |
return de.HttpResponse(ps(request.GET['word'])) | |
@de("/init") | |
def init(request): | |
return de.HttpResponse(random.choice(list(d.keys()))) | |
import sys | |
if len(sys.argv) > 1: | |
de.main() | |
else: | |
curses.wrapper(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment