def test_generate(): c = Chain() c.grow(s1) c.grow(s2) for i in range(10): print('%s.' % ' '.join(c.generate()))
def test_save(tmp_file): c = Chain() c.grow(s1) c.grow(s2) c.save(tmp_file) c = load(tmp_file) for i in range(10): print('%s.' % ' '.join(c.generate()))
def test_generate_rms(): c = Chain() d = os.path.dirname(__file__) filename = os.path.join(d, 'files/rms.txt') with open(filename, 'r') as f: content = f.read() for sentence in content.split('.'): words = sentence.split() c.grow(words) for i in range(10): print('%s.' % ' '.join(c.generate()))
class WordGen(object): def __init__(self): """Reads in phonetic data and creates a markov chain""" data = [] for x in open('data/phonmap.txt').readlines(): data.extend(x.split()) #Build up a markov chain with it self.chain = Chain(data, 2) def make_word(self): """Constructs words""" fragments = self.chain.generate(random.randint(2, 6)) return "".join(fragments)
class ShaqFu(object): def __init__(self): data = open('training_data.txt').read() self.chain = Chain(tokens(data), 5) def generate_line(self, rhymes): words = self.chain.generate(100) lines = ' '.join([x for x in words]).splitlines(False) return random.choice(lines) def generate_chorus(self): return ['[CHORUS]'] + [self.generate_line(True) for x in xrange(5)] + ['\n'] def generate_verse(self, min_length, max_length): length = random.randint(min_length, max_length) return ['[VERSE]'] + [self.generate_line(False) for x in xrange(length)] + ['\n'] def generate_song(self): chorus = self.generate_chorus() verses = [self.generate_verse(5, 10) for x in xrange(3)] return verses[0] + chorus + verses[1] + chorus + verses[2]
def main(): chain = Chain(tokens(poem), 4) words = chain.generate(2000) print "".join([x for x in words])