コード例 #1
0
ファイル: test.py プロジェクト: dannysullivan/mcmarkov
 def test_double_word_not_found(self):
     # It should look for a match to both a & c first,
     # and when it doesn't find anything, should return the match for c
     mc = MarkovChain(self.corpus, 2)
     mc.fit()
     self.assertEqual(mc.next_word(['a','c']),'a')
     self.assertEqual(mc.next_word(['alef','gimel']),'alef')
コード例 #2
0
ファイル: mc.py プロジェクト: dannysullivan/mcmarkov
class MCMarkov():

    def __init__(self, corpus, n_order=1, reverse=True):
        if reverse:
            self.corpus = [line[::-1] for line in corpus]
        else:
            self.corpus = corpus
        self.markovchain = MarkovChain(self.corpus, n_order)
        self.markovchain.fit()
        self.starting_words = [line[0] for line in self.corpus]
コード例 #3
0
ファイル: test.py プロジェクト: dannysullivan/mcmarkov
    def test_single_word(self):
        mc = MarkovChain(self.corpus, 1)
        mc.fit()

        # First line
        self.assertEqual(mc.next_word(['a']),'b')
        self.assertEqual(mc.next_word(['b']),'c')
        self.assertEqual(mc.next_word(['c']),'a')

        # Second line
        self.assertEqual(mc.next_word(['alef']),'bet')
        self.assertEqual(mc.next_word(['bet']),'gimel')
        self.assertEqual(mc.next_word(['gimel']),'alef')
コード例 #4
0
ファイル: test.py プロジェクト: mikekaminsky/mcmarkov
    def test_single_word(self):
        mc = MarkovChain(self.corpus, 1)
        mc.fit()

        # First line
        self.assertEqual(mc.next_word(["a"]), "b")
        self.assertEqual(mc.next_word(["b"]), "c")
        self.assertEqual(mc.next_word(["c"]), "a")

        # Second line
        self.assertEqual(mc.next_word(["alef"]), "bet")
        self.assertEqual(mc.next_word(["bet"]), "gimel")
        self.assertEqual(mc.next_word(["gimel"]), "alef")
コード例 #5
0
ファイル: test.py プロジェクト: dannysullivan/mcmarkov
 def test_double_word_found(self):
     # It should look for a match to both a & b first,
     mc = MarkovChain(self.corpus, 2)
     mc.fit()
     self.assertEqual(mc.next_word(['a','b']),'c')
     self.assertEqual(mc.next_word(['b','c']),'a')
     self.assertEqual(mc.next_word(['alef','bet']),'gimel')
     self.assertEqual(mc.next_word(['bet','gimel']),'alef')
コード例 #6
0
ファイル: test.py プロジェクト: mikekaminsky/mcmarkov
 def test_double_word_found(self):
     # It should look for a match to both a & b first,
     mc = MarkovChain(self.corpus, 2)
     mc.fit()
     self.assertEqual(mc.next_word(["a", "b"]), "c")
     self.assertEqual(mc.next_word(["b", "c"]), "a")
     self.assertEqual(mc.next_word(["alef", "bet"]), "gimel")
     self.assertEqual(mc.next_word(["bet", "gimel"]), "alef")
コード例 #7
0
ファイル: mc.py プロジェクト: mikekaminsky/mcmarkov
 def __init__(self, corpus, n_order=1, reverse=True):
     if reverse:
         self.corpus = [line[::-1] for line in corpus]
     else:
         self.corpus = corpus
     self.reverse = reverse
     self.markovchain = MarkovChain(self.corpus, n_order)
     self.markovchain.fit()
     self.starting_words = [line[0] for line in self.corpus if line]
     self.rhymedict = {}
     for word in self.starting_words:
         keys = rhymesyls(word)
         for key in keys:
             if key in self.rhymedict:
                 self.rhymedict[key].append(word)
             else:
                 self.rhymedict[key] = [word]
     self.words_to_rhyme = {}
     for key in self.rhymedict.keys():
         if len(set(self.rhymedict[key]))>1:
             self.words_to_rhyme[key] = self.rhymedict[key]
コード例 #8
0
ファイル: mc.py プロジェクト: mikekaminsky/mcmarkov
class MCMarkov():

    def __init__(self, corpus, n_order=1, reverse=True):
        if reverse:
            self.corpus = [line[::-1] for line in corpus]
        else:
            self.corpus = corpus
        self.reverse = reverse
        self.markovchain = MarkovChain(self.corpus, n_order)
        self.markovchain.fit()
        self.starting_words = [line[0] for line in self.corpus if line]
        self.rhymedict = {}
        for word in self.starting_words:
            keys = rhymesyls(word)
            for key in keys:
                if key in self.rhymedict:
                    self.rhymedict[key].append(word)
                else:
                    self.rhymedict[key] = [word]
        self.words_to_rhyme = {}
        for key in self.rhymedict.keys():
            if len(set(self.rhymedict[key]))>1:
                self.words_to_rhyme[key] = self.rhymedict[key]


    def create_line(self, startingword, syllable_count): # To be changed to syllable count
        line = [startingword]
        remaining_syllable_count = syllable_count - nsyl(startingword)
        i = 0
        while remaining_syllable_count > 0:
            if i == 0:
                word = self.markovchain.next_word([startingword])
            else:
                word = self.markovchain.next_word([word])
            if word == '\n': # If previous word has no following words, backup and start again
                prevword = line.pop()
                remaining_syllable_count += nsyl(prevword)
            if nsyl(word) <= remaining_syllable_count:
                line.append(word)
                remaining_syllable_count -= nsyl(word)
                i += 1
        if self.reverse:
            line = line[::-1]
        return line

    def create_song(self, couplet_count, syllable_count, syllable_map = None):
        if syllable_map is not None:
            couplet_count = len(syllable_map)
        song = []
        for i in range(0,couplet_count):
            if syllable_map is not None:
                line_map = syllable_map[i]
                line1_syllable_count = line_map[0]
                line2_syllable_count = line_map[1]
            else:
                line1_syllable_count = syllable_count
                line2_syllable_count = syllable_count
            rhymewords = set(self.words_to_rhyme[random.choice(self.words_to_rhyme.keys())])
            thisrhymes = random.sample(rhymewords,2)
            startingword1 = thisrhymes[0]
            startingword2 = thisrhymes[1]
            line1 = self.create_line(startingword1, line1_syllable_count)
            line2 = self.create_line(startingword2, line2_syllable_count)
            couplet = [line1, line2]
            song.append(couplet)
        return song
コード例 #9
0
ファイル: demo.py プロジェクト: dannysullivan/mcmarkov
from markov.markov import MarkovChain
import pickle

corpus = pickle.load(open( "eminemcorpus.pickle", "rb" ) )
mc = MarkovChain(corpus, 4)
print "Fitting..."
mc.fit()
print "Fit complete!"

generated_lyrics = []
word = None
print('starting lyric generation!')
for i in range(0,50):
    print i
    if word:
        # NOTE: this argument has to a be a list
        word = mc.next_word([word])
        print word
    else:
        word = mc.next_word()
        print word
    generated_lyrics.append(word)

print(generated_lyrics)
コード例 #10
0
ファイル: test.py プロジェクト: dannysullivan/mcmarkov
 def test_no_match(self):
     #TODO: Make it so the markov chain always generates something?
     mc = MarkovChain(self.corpus, 1)
     mc.fit()
     self.assertRaises(ValueError, mc.next_word, ['d'])
コード例 #11
0
ファイル: test.py プロジェクト: dannysullivan/mcmarkov
 def test_no_word(self):
     # If no word is passed, markov should return something
     # TODO: Test this is actually random?
     mc = MarkovChain(self.corpus, 1)
     mc.fit()
     self.assertNotEqual(mc.next_word([]),None)