#!/usr/bin/env python2 # This is -*- Python -*- import sys, string, time, random sys.path.append("../src") sys.path.append("../engine/build/lib.linux-i686-2.2") import gnoetics #import markov #gnoetics.dictionary_load("../dict/cmudict.0.6") sonnet_template = [ ("start", ), ("language", 10), ("break", ), ("language", 10), ("break", ), ("language", 10), ("break", ), ("language", 10), ("stop", ) ] haiku_template = [ ("start", ), ("language", 5), ("break", ), ("language", 7), ("break", ), ("language", 5), ("stop", ) ] #m = markov.Markov() m = gnoetics.markov_new() for filename in sys.argv[1:]: print "Loading", filename txt = gnoetics.text_new(filename) m.add_text(txt) def prev_word(template, i): i -= 1 while i >= 0: type = template[i][0] if type == "word": return template[i][1] elif type != "break": return None i -= 1 return None def next_word(template, i): i += 1 while i < len(template): type = template[i][0] if type == "word": return template[i][1] elif type != "break": return None i += 1 return None def spew_template(template, now=-1): for i in range(len(template)): if i == now: print "*", else: print " ", print i, if template[i][0] == "word": print template[i][2], "(%d)" % template[i][1].syllables(), else: print template[i], print def template_free_syllables(template): count = 0 for w in template: if w[0] == "language": count += w[1] return count def template_is_finished(template): for w in template: if w[0] not in ("word", "break"): return 0 return 1 def initialize_template(m, template): for i in range(len(template)): type = template[i][0] if type == "start": template[i] = ("word", gnoetics.token_lookup(""), "") elif type == "stop": template[i] = ("word", gnoetics.token_lookup(""), "") return template def iterate_template(m, template): for i in range(len(template)): if template[i][0] == "language": syllables = template[i][1] if syllables == 0: return template[:i] + template[i+1:] prev = prev_word(template, i) next = next_word(template, i) filter = {} filter["min_syllables"] = 0 filter["max_syllables"] = syllables filter["force_starter"] = prev and prev.is_start() filter["force_stopper"] = next and next.is_stop() if prev: word, fc, ufc = m.choose_next(prev, filter) if word: fragment = [("word", word, word.to_string()), ("language", syllables - word.syllables())] return template[:i] + fragment + template[i+1:] if next: word, fc, ufc = m.choose_prev(next, filter) if word: fragment = [("language",syllables - word.syllables()), ("word", word, word.to_string())] return template[:i] + fragment + template[i+1:] return None i += 1 # Hmm... we shouldn't fall through return template def solve_template(m, template): history = [initialize_template(m, template)] while not template_is_finished(history[-1]): evolved = iterate_template(m, history[-1]) if evolved: history.append(evolved) else: # go back to a random point in our history and restart i = random.randint(1, len(history)-1) history = history[:i] return history[-1] def pretty_print_template(template): poem = "" for w in template: type = w[0] if type == "word": poem += w[1].to_string() + " " elif type == "break": poem += "\n" print poem count = 0 t1 = time.time() while 1: template = solve_template(m, sonnet_template) pretty_print_template(template) print sys.exit(0) count += 1 if count % 50 == 0: t2 = time.time() print "hps = %.3f" % (count / float(t2-t1))