def gen_verse(rhymescheme, antirhymescheme, meters, state=None): if state is None: state = start_state order = babble.the_order() lines = [list(state)] linerhymes = [None] i = 1 length = len(rhymescheme) rhymescheme = [[]] + rhymescheme antirhymescheme = [[]] + antirhymescheme meters = [None] + meters while i <= length: state_i = tuple(reduce(operator.add, lines, [])[-order:]) rhymes = [linerhymes[j] for j in rhymescheme[i]] antirhymes = [linerhymes[j] for j in antirhymescheme[i]] if i == 1: # XXX hacky state_i = babble.start(state_i) m = match_words(tuple(t for t in state_i if t != '<S>'), meters[i]) if m is None: continue line, rhyme = gen_line(meter=m, rhymes=rhymes, antirhymes=antirhymes, state=state_i, cutoff=10000) if line: line = list(state_i) + line else: line, rhyme = gen_line(meter=meters[i], rhymes=rhymes, antirhymes=antirhymes, state=state_i, cutoff=60000) if line is None: # XXX hacky: i = max(1, max(rhymescheme[i]) if rhymescheme[i] else i-1) lines = lines[:i] linerhymes = linerhymes[:i] print >>sys.stderr, 'Backing up to %d' % i else: print >>sys.stderr, '%4d' % i, format_line(line) lines.append(line) linerhymes.append(rhyme) i += 1 return lines[1:]
def gen_verse(rhymescheme, antirhymescheme, meters, state=None): if state is None: state = start_state order = babble.the_order() lines = [list(state)] linerhymes = [None] i = 1 length = len(rhymescheme) rhymescheme = [[]] + rhymescheme antirhymescheme = [[]] + antirhymescheme meters = [None] + meters while i <= length: state_i = tuple(reduce(operator.add, lines, [])[-order:]) rhymes = [linerhymes[j] for j in rhymescheme[i]] antirhymes = [linerhymes[j] for j in antirhymescheme[i]] if i == 1: # XXX hacky state_i = babble.start(state_i) m = match_words(tuple(t for t in state_i if t != '<S>'), meters[i]) if m is None: continue line, rhyme = gen_line(meter=m, rhymes=rhymes, antirhymes=antirhymes, state=state_i, cutoff=10000) if line: line = list(state_i) + line else: line, rhyme = gen_line(meter=meters[i], rhymes=rhymes, antirhymes=antirhymes, state=state_i, cutoff=60000) if line is None: # XXX hacky: i = max(1, max(rhymescheme[i]) if rhymescheme[i] else i-1) lines = lines[:i] linerhymes = linerhymes[:i] print >>sys.stderr, 'Backing up to %d' % i else: print >>sys.stderr, '%4d' % i, format_line(line) lines.append(line) linerhymes.append(rhyme) i += 1 return lines[1:]
def start(): return babble.start(start_state)
def start(): return babble.start(start_state)