コード例 #1
0
ファイル: ignore.py プロジェクト: anassalamah/cl1-hw
from limerick import LimerickDetector

ld = LimerickDetector()
print ld.num_syllables("dog")
print ld.rhymes("dog", "bog")
print ld.guess_syllables("splorkatuk")
コード例 #2
0
class TestSequenceFunctions(unittest.TestCase):
    def setUp(self):
        self.ld = LimerickDetector()

    def test_guess_syllables(self):
        s = []

        try:
            self.assertEqual(self.ld.guess_syllables("kogo"), 2)
        except:
            s.append(1)
        try:
            self.assertEqual(self.ld.guess_syllables("213"), 1)
        except:
            s.append(2)

        print '\nNumber of failed guess_syllables tests:', str(len(s))
        if len(s) != 0:
            print 'Failed guess_syllables tests:', ','.join(
                [str(x) for x in s])

    def test_apostrophe(self):
        s = []

        self.assertEqual(
            self.ld.apstrophe_tokenize("I can't wouldn't isn't won't won;t"),
            ['I', "can't", "wouldn't", "isn't", "won't", 'won', ';', 't'])

        try:
            self.assertEqual(
                self.ld.apstrophe_tokenize(
                    "I can't wouldn't isn't won't won;t"),
                ['I', "can't", "wouldn't", "isn't", "won't", 'won', ';', 't'])
        except:
            s.append(1)

        print '\nNumber of failed apostrophe tests:', str(len(s))
        if len(s) != 0:
            print 'Failed apostrophe tests:', ','.join([str(x) for x in s])

    def test_rhyme(self):
        s = []

        ##        self.assertEqual(self.ld.rhymes("awoke", "broke"), False)

        try:
            self.assertEqual(self.ld.rhymes("dog", "bog"), True)
        except:
            s.append(1)
        try:
            self.assertEqual(self.ld.rhymes("eleven", "seven"), True)
        except:
            s.append(2)
        try:
            self.assertEqual(self.ld.rhymes("nine", "wine"), True)
        except:
            s.append(3)
        try:
            self.assertEqual(self.ld.rhymes("dine", "fine"), True)
        except:
            s.append(4)
        try:
            self.assertEqual(self.ld.rhymes("wine", "mine"), True)
        except:
            s.append(5)
        try:
            self.assertEqual(self.ld.rhymes("dock", "sock"), True)
        except:
            s.append(6)
        try:
            self.assertEqual(self.ld.rhymes("weigh", "fey"), True)
        except:
            s.append(7)
        try:
            self.assertEqual(self.ld.rhymes("tree", "debris"), True)
        except:
            s.append(8)
        try:
            self.assertEqual(self.ld.rhymes("niece", "peace"), True)
        except:
            s.append(9)
        try:
            self.assertEqual(self.ld.rhymes("read", "need"), True)
        except:
            s.append(10)
        try:
            self.assertEqual(self.ld.rhymes("dog", "cat"), False)
        except:
            s.append(11)
        try:
            self.assertEqual(self.ld.rhymes("bagel", "sail"), False)
        except:
            s.append(12)
        try:
            self.assertEqual(self.ld.rhymes("wine", "rind"), False)
        except:
            s.append(13)
        try:
            self.assertEqual(self.ld.rhymes("failure", "savior"), False)
        except:
            s.append(14)
        try:
            self.assertEqual(self.ld.rhymes("cup", "duck"), False)
        except:
            s.append(15)
        try:
            self.assertEqual(self.ld.rhymes("og", "bog"), True)
        except:
            s.append(16)
        try:
            self.assertEqual(self.ld.rhymes("awoke", "broke"), True)
        except:
            s.append(17)

        print '\nNumber of failed rhyme tests:', str(len(s))
        if len(s) != 0:
            print 'Failed rhyme tests:', ','.join([str(x) for x in s])

    def test_syllables(self):
        s = []
        try:
            self.assertEqual(self.ld.num_syllables("dog"), 1)
        except:
            s.append(1)
        try:
            self.assertEqual(self.ld.num_syllables("asdf"), 1)
        except:
            s.append(2)
        try:
            self.assertEqual(self.ld.num_syllables("letter"), 2)
        except:
            s.append(3)
        try:
            self.assertEqual(self.ld.num_syllables("washington"), 3)
        except:
            s.append(4)
        try:
            self.assertEqual(self.ld.num_syllables("dock"), 1)
        except:
            s.append(5)
        try:
            self.assertEqual(self.ld.num_syllables("dangle"), 2)
        except:
            s.append(6)
        try:
            self.assertEqual(self.ld.num_syllables("thrive"), 1)
        except:
            s.append(7)
        try:
            self.assertEqual(self.ld.num_syllables("fly"), 1)
        except:
            s.append(8)
        try:
            self.assertEqual(self.ld.num_syllables("placate"), 2)
        except:
            s.append(9)
        try:
            self.assertEqual(self.ld.num_syllables("renege"), 2)
        except:
            s.append(10)
        try:
            self.assertEqual(self.ld.num_syllables("reluctant"), 3)
        except:
            s.append(11)

        print '\nNumber of failed syllables tests:', str(len(s))
        if len(s) != 0:
            print 'Failed syllables tests:', ','.join([str(x) for x in s])

    def test_examples(self):

        a = """
a woman whose friends called a prude
on a lark when bathing all nude
saw a man come along
and unless we are wrong
you expected this line to be lewd
        """

        b = """while it's true all i've done is delay
in defense of myself i must say
today's payoff is great
while the workers all wait
"""

        c = """
this thing is supposed to rhyme
but I simply don't got the time
who cares if i miss,
nobody will read this
i'll end this here poem potato
"""

        d = """There was a young man named Wyatt
whose voice was exceedingly quiet
And then one day
it faded away"""

        e = """An exceedingly fat friend of mine,
When asked at what hour he'd dine,
Replied, "At eleven,     
At three, five, and seven,
And eight and a quarter past nine"""

        f = """A limerick fan from Australia
regarded his work as a failure:
his verses were fine
until the fourth line"""

        g = """There was a young lady one fall
Who wore a newspaper dress to a ball.
The dress caught fire
And burned her entire
Front page, sporting section and all."""

        h = "dog\ndog\ndog\ndog\ndog"

        i = """Technic back in time was so broke
Everyone think computer is just a joke
but machine learning grow
make everybody know
the new time of era has awoke"""

        s = []

        ##        self.assertEqual(self.ld.is_limerick(a), True)

        try:
            self.assertEqual(self.ld.is_limerick(a), True)
        except:
            s.append('a')
        try:
            self.assertEqual(self.ld.is_limerick(b), False)
        except:
            s.append('b')
        try:
            self.assertEqual(self.ld.is_limerick(c), False)
        except:
            s.append('c')
        try:
            self.assertEqual(self.ld.is_limerick(d), False)
        except:
            s.append('d')
        try:
            self.assertEqual(self.ld.is_limerick(e), True)
        except:
            s.append('e')
        try:
            self.assertEqual(self.ld.is_limerick(f), False)
        except:
            s.append('f')
        try:
            self.assertEqual(self.ld.is_limerick(g), True)
        except:
            s.append('g')
        try:
            self.assertEqual(self.ld.is_limerick(h), False)
        except:
            s.append('h')
        try:
            self.assertEqual(self.ld.is_limerick(i), True)
        except:
            s.append('i')

        print 'Number of failed limerick tests:', str(len(s))
        if len(s) != 0: print 'Failed limerick tests:', ','.join(s)
コード例 #3
0
from limerick import LimerickDetector

ld = LimerickDetector()
print ld.num_syllables("dog")
print ld.rhymes("dog", "bog")
print ld.guess_syllables("splorkatuk")
コード例 #4
0
class TestSequenceFunctions(unittest.TestCase):
    def setUp(self):
        self.ld = LimerickDetector()

    def test_rhyme(self):
        s = []
        try:
            self.assertEqual(self.ld.rhymes("dog", "bog"), True)
        except:
            s.append(1)
        try:
            self.assertEqual(self.ld.rhymes("eleven", "seven"), True)
        except:
            s.append(2)
        try:
            self.assertEqual(self.ld.rhymes("nine", "wine"), True)
        except:
            s.append(3)
        try:
            self.assertEqual(self.ld.rhymes("dine", "fine"), True)
        except:
            s.append(4)
        try:
            self.assertEqual(self.ld.rhymes("wine", "mine"), True)
        except:
            s.append(5)
        try:
            self.assertEqual(self.ld.rhymes("dock", "sock"), True)
        except:
            s.append(6)
        try:
            self.assertEqual(self.ld.rhymes("weigh", "fey"), True)
        except:
            s.append(7)
        try:
            self.assertEqual(self.ld.rhymes("tree", "debris"), True)
        except:
            s.append(8)
        try:
            self.assertEqual(self.ld.rhymes("niece", "peace"), True)
        except:
            s.append(9)
        try:
            self.assertEqual(self.ld.rhymes("read", "need"), True)
        except:
            s.append(10)
        try:
            self.assertEqual(self.ld.rhymes("dog", "cat"), False)
        except:
            s.append(11)
        try:
            self.assertEqual(self.ld.rhymes("bagel", "sail"), False)
        except:
            s.append(12)
        try:
            self.assertEqual(self.ld.rhymes("wine", "rind"), False)
        except:
            s.append(13)
        try:
            self.assertEqual(self.ld.rhymes("failure", "savior"), False)
        except:
            s.append(14)
        try:
            self.assertEqual(self.ld.rhymes("cup", "duck"), False)
        except:
            s.append(15)
        try:
            self.assertEqual(self.ld.rhymes("infinity", "affinity"), False)
        except:
            s.append(16)
        try:
            self.assertEqual(self.ld.rhymes("you", "ewe"), True)
        except:
            s.append(17)
        try:
            self.assertEqual(self.ld.rhymes("anita", "juanita"), True)
        except:
            s.append(18)
        try:
            self.assertEqual(self.ld.rhymes("fire", "conspire"), True)
        except:
            s.append(19)
        try:
            self.assertEqual(self.ld.rhymes("guessing", "messing"), True)
        except:
            s.append(20)
        try:
            self.assertEqual(self.ld.rhymes("cant", "pant"), True)
        except:
            s.append(21)

        print '\nNumber of failed rhyme tests:', str(len(s))
        if len(s) != 0:
            print 'Failed rhyme tests:', ','.join([str(x) for x in s])

    def test_syllables(self):
        s = []
        try:
            self.assertEqual(self.ld.num_syllables("dog"), 1)
        except:
            s.append(1)
        try:
            self.assertEqual(self.ld.num_syllables("asdf"), 1)
        except:
            s.append(2)
        try:
            self.assertEqual(self.ld.num_syllables("letter"), 2)
        except:
            s.append(3)
        try:
            self.assertEqual(self.ld.num_syllables("washington"), 3)
        except:
            s.append(4)
        try:
            self.assertEqual(self.ld.num_syllables("dock"), 1)
        except:
            s.append(5)
        try:
            self.assertEqual(self.ld.num_syllables("dangle"), 2)
        except:
            s.append(6)
        try:
            self.assertEqual(self.ld.num_syllables("thrive"), 1)
        except:
            s.append(7)
        try:
            self.assertEqual(self.ld.num_syllables("fly"), 1)
        except:
            s.append(8)
        try:
            self.assertEqual(self.ld.num_syllables("placate"), 2)
        except:
            s.append(9)
        try:
            self.assertEqual(self.ld.num_syllables("renege"), 2)
        except:
            s.append(10)
        try:
            self.assertEqual(self.ld.num_syllables("reluctant"), 3)
        except:
            s.append(11)
        try:
            self.assertEqual(self.ld.num_syllables("eyes"), 1)
        except:
            s.append(12)
        try:
            self.assertEqual(self.ld.num_syllables("you"), 1)
        except:
            s.append(13)
        try:
            self.assertEqual(self.ld.guess_syllables("Placate"), 2)
        except:
            s.append(14)
        try:
            self.assertEqual(self.ld.guess_syllables("eyes"), 1)
        except:
            s.append(15)
        try:
            self.assertEqual(self.ld.guess_syllables("boys"), 1)
        except:
            s.append(16)
        try:
            self.assertEqual(self.ld.num_syllables("boys"), 1)
        except:
            s.append(17)
        try:
            self.assertEqual(self.ld.guess_syllables("buyer"), 2)
        except:
            s.append(18)
        try:
            self.assertEqual(self.ld.num_syllables("buyer"), 2)
        except:
            s.append(19)
        try:
            self.assertEqual(self.ld.guess_syllables("year"), 1)
        except:
            s.append(20)

        print '\nNumber of failed syllables tests:', str(len(s))
        if len(s) != 0:
            print 'Failed syllables tests:', ','.join([str(x) for x in s])

    def test_examples(self):

        a = """
a woman whose friends called a prude
on a lark when bathing all nude
saw a man come along
and unless we are wrong
you expected this line to be lewd
        """

        b = """while it's true all i've done is delay
in defense of myself i must say
today's payoff is great
while the workers all wait
"""

        c = """
this thing is supposed to rhyme
but I simply don't got the time
who cares if i miss,
nobody will read this
i'll end this here poem potato
"""

        d = """There was a young man named Wyatt
whose voice was exceedingly quiet
And then one day
it faded away"""

        e = """An exceedingly fat friend of mine,
When asked at what hour he'd dine,
Replied, "At eleven,     
At three, five, and seven,
And eight and a quarter past nine"""

        f = """A limerick fan from Australia
regarded his work as a failure:
his verses were fine
until the fourth line"""

        g = """There was a young lady one fall
Who wore a newspaper dress to a ball.
The dress caught fire
And burned her entire
Front page, sporting section and all."""

        h = "dog\ndog\ndog\ndog\ndog"

        j = """Can't believe it's true, must be a ruse.
It seems kids these days actually choose.
It's a very strange fad,
to dress up just like Dad.
Bell-bottom pants and big clunky shoes."""

        i = """There once was a young lady named bright

Whose speed was much faster than light
She set out one day
In a relative way
And returned on the previous night."""
        k = """There was a young lady from space
who grew at a frightening pace
and her mother said "Nelly
There's more in your belly
Than ever went in through your face"""
        l = """A pocket full of random guessing
I never ever had a model messing
Love without my intervention
Such a way of comprehension
Is that or not a hidden blessing"""
        m = """asdf asdf asdf asdf asdf asdf asdf bright
        asdf asdf asdf asdf asdf asdf light
        asdf asdf asdf asdf can't
        asdf asdf asdf pant
        asdf asdf asdf's asdf asdf night"""

        s = []

        try:
            self.assertEqual(self.ld.is_limerick(a), True)
        except:
            s.append('a')
        try:
            self.assertEqual(self.ld.is_limerick(b), False)
        except:
            s.append('b')
        try:
            self.assertEqual(self.ld.is_limerick(c), False)
        except:
            s.append('c')
        try:
            self.assertEqual(self.ld.is_limerick(d), False)
        except:
            s.append('d')
        try:
            self.assertEqual(self.ld.is_limerick(e), True)
        except:
            s.append('e')
        try:
            self.assertEqual(self.ld.is_limerick(f), False)
        except:
            s.append('f')
        try:
            self.assertEqual(self.ld.is_limerick(g), True)
        except:
            s.append('g')
        try:
            self.assertEqual(self.ld.is_limerick(h), False)
        except:
            s.append('h')
        try:
            self.assertEqual(self.ld.is_limerick(i), True)
        except:
            s.append('i')
        try:
            self.assertEqual(self.ld.is_limerick(j), True)
        except:
            s.append('j')
        try:
            self.assertEqual(self.ld.is_limerick(k), True)
        except:
            s.append('k')
        try:
            self.assertEqual(self.ld.is_limerick(m), True)
        except:
            s.append('m')

        print 'Number of failed limerick tests:', str(len(s))
        if len(s) != 0: print 'Failed limerick tests:', ','.join(s)
コード例 #5
0
class TestSequenceFunctions(unittest.TestCase):

    def setUp(self):
        self.ld = LimerickDetector()

    def test_rhyme(self):
        print 'RHYMETEST_BELOW'
        print self.ld.rhymes("defiance", "compliance")

    def test_syllables(self):
        s = []
        #try: self.assertEqual(self.ld.guess_syllables("dog"), 1)
        #except: s.append(1)
        try: self.assertEqual(self.ld.guess_syllables("asdf"), 1)
        except: s.append(2)
        try: self.assertEqual(self.ld.guess_syllables("letter"), 2)
        except: s.append(3)
        try: self.assertEqual(self.ld.guess_syllables("washington"), 3)
        except: s.append(4)
        try: self.assertEqual(self.ld.guess_syllables("dock"), 1)
        except: s.append(5)
        try: self.assertEqual(self.ld.guess_syllables("dangle"), 2)
        except: s.append(6)
        try: self.assertEqual(self.ld.guess_syllables("thrive"), 1)
        except: s.append(7)
        try: self.assertEqual(self.ld.guess_syllables("fly"), 1)
        except: s.append(8)
        try: self.assertEqual(self.ld.guess_syllables("placate"), 2)
        except: s.append(9)
        try: self.assertEqual(self.ld.guess_syllables("renege"), 2)
        except: s.append(10)
        
        try: self.assertEqual(self.ld.guess_syllables("advertisement"), 5)
        except: s.append(11)

        print '\nNumber of failed syllables tests:', str(len(s))
        if len(s)!=0: print 'Failed syllables tests:', ','.join([str(x) for x in s])

    def test_examples(self):

        a = """
a woman whose friends called a prude
on a lark when bathing all nude
saw a man come along
and unless we are wrong
you expected this line to be lewd
        """

        b = """while it's true all i've done is delay
in defense of myself i must say
today's payoff is great
while the workers all wait
"""

        c =  """
THIS THING IS SUPPOSED TO RHYME
BUT I SIMPLY DON'T GOT THE TIME
WHO CARES IF I MISS,
NOBODY WILL READ THIS
I'LL END THIS HERE POEM POTATO
"""

        d = """There was a young man named Wyatt
whose voice was exceedingly quiet
And then one day
it faded away"""

        e = """An exceedingly fat friend of mine,
When asked at what hour he'd dine,
Replied, "At eleven,     
At three, five, and seven,
And eight and a quarter past nine"""

        f = """A limerick fan from Australia
regarded his work as a failure:
his verses were fine
until the fourth line"""

        g = """There was a young lady one fall
Who wore a newspaper dress to a ball.
The dress caught fire
And burned her entire
Front page, sporting section and all."""

        h = "dog\ndog\ndog\ndog\ndog"
        
        i = """When we shook our hands with science
the digital era came in compliance
as algorithms came along
vision of computer became strong
it's enthralling to see that machine and humans have such a beautiful alliance"""


        
        s = []

        try: self.assertEqual(self.ld.is_limerick(a), True)
        except: s.append('a')
        try: self.assertEqual(self.ld.is_limerick(b), False)
        except: s.append('b')
        try: self.assertEqual(self.ld.is_limerick(c), False)
        except: s.append('c')
        try: self.assertEqual(self.ld.is_limerick(d), False)
        except: s.append('d')
        try: self.assertEqual(self.ld.is_limerick(f), False)
        except: s.append('e')
        try: self.assertEqual(self.ld.is_limerick(e), True)
        except: s.append('f')
        try: self.assertEqual(self.ld.is_limerick(g), True)
        except: s.append('g')
        try: self.assertEqual(self.ld.is_limerick(h), True)
        except: s.append('h')
        
        print self.ld.is_limerick(i)

        print 'Number of failed limerick tests:', str(len(s))
        if len(s)!=0: print 'Failed limerick tests:', ','.join(s)
コード例 #6
0
class TestSequenceFunctions(unittest.TestCase):
    def setUp(self):
        self.ld = LimerickDetector()

    def test_rhyme(self):
        s = []
        try:
            self.assertEqual(self.ld.rhymes("dog", "bog"), True)
        except:
            s.append(1)
        try:
            self.assertEqual(self.ld.rhymes("eleven", "seven"), True)
        except:
            s.append(2)
        try:
            self.assertEqual(self.ld.rhymes("nine", "wine"), True)
        except:
            s.append(3)
        try:
            self.assertEqual(self.ld.rhymes("dine", "fine"), True)
        except:
            s.append(4)
        try:
            self.assertEqual(self.ld.rhymes("wine", "mine"), True)
        except:
            s.append(5)
        try:
            self.assertEqual(self.ld.rhymes("dock", "sock"), True)
        except:
            s.append(6)
        try:
            self.assertEqual(self.ld.rhymes("weigh", "fey"), True)
        except:
            s.append(7)
        try:
            self.assertEqual(self.ld.rhymes("tree", "debris"), True)
        except:
            s.append(8)
        try:
            self.assertEqual(self.ld.rhymes("niece", "peace"), True)
        except:
            s.append(9)
        try:
            self.assertEqual(self.ld.rhymes("read", "need"), True)
        except:
            s.append(10)
        try:
            self.assertEqual(self.ld.rhymes("dog", "cat"), False)
        except:
            s.append(11)
        try:
            self.assertEqual(self.ld.rhymes("bagel", "sail"), False)
        except:
            s.append(12)
        try:
            self.assertEqual(self.ld.rhymes("wine", "rind"), False)
        except:
            s.append(13)
        try:
            self.assertEqual(self.ld.rhymes("failure", "savior"), False)
        except:
            s.append(14)
        try:
            self.assertEqual(self.ld.rhymes("cup", "duck"), False)
        except:
            s.append(15)
        try:
            self.assertEqual(self.ld.rhymes("dog", "dog"), True)
        except:
            s.append(16)
        try:
            self.assertEqual(self.ld.rhymes("train", "plane"), True)
        except:
            s.append(17)
        try:
            self.assertEqual(self.ld.rhymes("thicket", "it"), True)
        except:
            s.append(18)
        try:
            self.assertEqual(self.ld.rhymes("thicket", "cricket"), True)
        except:
            s.append(19)
        try:
            self.assertEqual(self.ld.rhymes("conspire", "fire"), True)
        except:
            s.append(20)
        try:
            self.assertEqual(self.ld.rhymes("along", "wrong"), True)
        except:
            s.append(21)
        try:
            self.assertEqual(self.ld.rhymes("eyes", "oversize"), True)
        except:
            s.append(22)
        try:
            self.assertEqual(self.ld.rhymes("affinity", "infinity"), False)
        except:
            s.append(23)
        try:
            self.assertEqual(self.ld.rhymes("fly", "butterfly"), False)
        except:
            s.append(24)
        try:
            self.assertEqual(self.ld.rhymes("imperia", "asia"), False)
        except:
            s.append(25)
        try:
            self.assertEqual(self.ld.rhymes("footwear", "impair"), False)
        except:
            s.append(26)
        try:
            self.assertEqual(self.ld.rhymes("anatomical", "economical"), False)
        except:
            s.append(27)
        try:
            self.assertEqual(self.ld.rhymes("reader", "higher"), False)
        except:
            s.append(28)

        print '\nNumber of failed rhyme tests:', str(len(s))
        if len(s) != 0:
            print 'Failed rhyme tests:', ','.join([str(x) for x in s])

    def test_syllables(self):
        s = []
        try:
            self.assertEqual(self.ld.num_syllables("dog"), 1)
        except:
            s.append(1)
        try:
            self.assertEqual(self.ld.num_syllables("asdf"), 1)
        except:
            s.append(2)
        try:
            self.assertEqual(self.ld.num_syllables("letter"), 2)
        except:
            s.append(3)
        try:
            self.assertEqual(self.ld.num_syllables("washington"), 3)
        except:
            s.append(4)
        try:
            self.assertEqual(self.ld.num_syllables("dock"), 1)
        except:
            s.append(5)
        try:
            self.assertEqual(self.ld.num_syllables("dangle"), 2)
        except:
            s.append(6)
        try:
            self.assertEqual(self.ld.num_syllables("thrive"), 1)
        except:
            s.append(7)
        try:
            self.assertEqual(self.ld.num_syllables("fly"), 1)
        except:
            s.append(8)
        try:
            self.assertEqual(self.ld.num_syllables("placate"), 2)
        except:
            s.append(9)
        try:
            self.assertEqual(self.ld.num_syllables("renege"), 2)
        except:
            s.append(10)
        try:
            self.assertEqual(self.ld.num_syllables("reluctant"), 3)
        except:
            s.append(11)

        print '\nNumber of failed syllables tests:', str(len(s))
        if len(s) != 0:
            print 'Failed syllables tests:', ','.join([str(x) for x in s])

    def test_guess_syllables(self):
        s = []
        try:
            self.assertEqual(self.ld.guess_syllables("dog"), 1)
        except:
            s.append(1)
        try:
            self.assertEqual(self.ld.guess_syllables("asdf"), 1)
        except:
            s.append(2)
        try:
            self.assertEqual(self.ld.guess_syllables("letter"), 2)
        except:
            s.append(3)
        try:
            self.assertEqual(self.ld.guess_syllables("washington"), 3)
        except:
            s.append(4)
        try:
            self.assertEqual(self.ld.guess_syllables("dock"), 1)
        except:
            s.append(5)
        try:
            self.assertEqual(self.ld.guess_syllables("dangle"), 2)
        except:
            s.append(6)
        try:
            self.assertEqual(self.ld.guess_syllables("thrive"), 1)
        except:
            s.append(7)
        try:
            self.assertEqual(self.ld.guess_syllables("fly"), 1)
        except:
            s.append(8)
        try:
            self.assertEqual(self.ld.guess_syllables("placate"), 2)
        except:
            s.append(9)
        try:
            self.assertEqual(self.ld.guess_syllables("renege"), 2)
        except:
            s.append(10)
        try:
            self.assertEqual(self.ld.guess_syllables("reluctant"), 3)
        except:
            s.append(11)

        print '\nNumber of failed guess syllables tests:', str(len(s))
        if len(s) != 0:
            print 'Failed guess syllables tests:', ','.join(
                [str(x) for x in s])

    def test_examples(self):

        a = """
a woman whose friends called a prude
on a lark when bathing all nude
saw a man come along
and unless we are wrong
you expected this line to be lewd
        """

        b = """while it's true all i've done is delay
in defense of myself i must say
today's payoff is great
while the workers all wait
"""

        c = """
this thing is supposed to rhyme
but I simply don't got the time
who cares if i miss,
nobody will read this
i'll end this here poem potato
"""

        d = """There was a young man named Wyatt
whose voice was exceedingly quiet
And then one day
it faded away"""

        e = """An exceedingly fat friend of mine,
When asked at what hour he'd dine,
Replied, "At eleven,     
At three, five, and seven,
And eight and a quarter past nine"""

        f = """A limerick fan from Australia
regarded his work as a failure:
his verses were fine
until the fourth line"""

        g = """There was a young lady one fall
Who wore a newspaper dress to a ball.
The dress caught fire
And burned her entire
Front page, sporting section and all."""

        h = "dog\ndog\ndog\ndog\ndog"

        i = """There was a young lady one don't,
        Who wore a newspaper dress to a won't.
        The dress caught can't
        And burned her pant
        Front page, sporting section and don't."""

        j = """There was a young lady one coundn't
        Who wore a newspaper dress to a shouldn't.
        The dress caught fire
        And burned her entire
        Front page, sporting section and wouldn't."""

        k = """There was a young lady one fall
        Who wore a newspaper dress to a ball.
        The dress caught fire
        And burned her entire
        Front page, sporting section and all."""

        s = []

        try:
            self.assertEqual(self.ld.is_limerick(a), True)
        except:
            s.append('a')
        try:
            self.assertEqual(self.ld.is_limerick(b), False)
        except:
            s.append('b')
        try:
            self.assertEqual(self.ld.is_limerick(c), False)
        except:
            s.append('c')
        try:
            self.assertEqual(self.ld.is_limerick(d), False)
        except:
            s.append('d')
        try:
            self.assertEqual(self.ld.is_limerick(f), False)
        except:
            s.append('f')
        try:
            self.assertEqual(self.ld.is_limerick(e), True)
        except:
            s.append('e')
        try:
            self.assertEqual(self.ld.is_limerick(g), True)
        except:
            s.append('g')

        print 'Number of failed limerick tests:', str(len(s))
        if len(s) != 0: print 'Failed limerick tests:', ','.join(s)
コード例 #7
0
class TestSequenceFunctions(unittest.TestCase):
    def setUp(self):
        self.ld = LimerickDetector()

    def test_rhyme(self):
        self.assertEqual(self.ld.rhymes("dog", "bog"), True)
        self.assertEqual(self.ld.rhymes("eleven", "seven"), True)
        self.assertEqual(self.ld.rhymes("nine", "wine"), True)
        self.assertEqual(self.ld.rhymes("dine", "fine"), True)
        self.assertEqual(self.ld.rhymes("wine", "mine"), True)
        self.assertEqual(self.ld.rhymes("dock", "sock"), True)
        self.assertEqual(self.ld.rhymes("weigh", "fey"), True)
        self.assertEqual(self.ld.rhymes("tree", "debris"), True)
        self.assertEqual(self.ld.rhymes("niece", "peace"), True)
        self.assertEqual(self.ld.rhymes("read", "need"), True)

        self.assertEqual(self.ld.rhymes("dog", "cat"), False)
        self.assertEqual(self.ld.rhymes("bagel", "sail"), False)
        self.assertEqual(self.ld.rhymes("wine", "rind"), False)
        self.assertEqual(self.ld.rhymes("failure", "savior"), False)
        self.assertEqual(self.ld.rhymes("cup", "duck"), False)

    def test_rhyme_extra(self):
        self.assertEqual(self.ld.rhymes("fall", "ball"), True)
        self.assertEqual(self.ld.rhymes("along", "wrong"), True)
        self.assertEqual(self.ld.rhymes("eye", "dry"), True)
        self.assertEqual(self.ld.rhymes("scratch", "patch"), True)

    def test_syllables(self):
        self.assertEqual(self.ld.num_syllables("dog"), 1)
        self.assertEqual(self.ld.num_syllables("asdf"), 1)
        self.assertEqual(self.ld.num_syllables("letter"), 2)
        self.assertEqual(self.ld.num_syllables("washington"), 3)
        self.assertEqual(self.ld.num_syllables("dock"), 1)
        self.assertEqual(self.ld.num_syllables("dangle"), 2)
        self.assertEqual(self.ld.num_syllables("thrive"), 1)
        self.assertEqual(self.ld.num_syllables("fly"), 1)
        self.assertEqual(self.ld.num_syllables("placate"), 2)
        self.assertEqual(self.ld.num_syllables("renege"), 2)
        self.assertEqual(self.ld.num_syllables("reluctant"), 3)

    def test_guess_syllables(self):
        self.assertEqual(self.ld.guess_syllables("dog"), 1)
        self.assertEqual(self.ld.guess_syllables("letter"), 2)
        self.assertEqual(self.ld.guess_syllables("washington"), 3)
        self.assertEqual(self.ld.guess_syllables("dock"), 1)
        # self.assertEqual(self.ld.guess_syllables("dangle"), 2)
        self.assertEqual(self.ld.guess_syllables("thrive"), 1)
        self.assertEqual(self.ld.guess_syllables("fly"), 1)
        self.assertEqual(self.ld.guess_syllables("placate"), 2)
        self.assertEqual(self.ld.guess_syllables("renege"), 2)
        self.assertEqual(self.ld.guess_syllables("reluctant"), 3)
        self.assertEqual(self.ld.guess_syllables("Walter"), 2)

    def test_examples(self):

        a = """
a woman whose friends called a prude
on a lark when bathing all nude
saw a man come along
and unless we are wrong
you expected this line to be lewd
        """

        b = """while it's true all i've done is delay
in defense of myself i must say
today's payoff is great
while the workers all wait
"""

        c = """
this thing is supposed to rhyme
but I simply don't got the time
who cares if i miss,
nobody will read this
i'll end this here poem potato
"""

        d = """There was a young man named Wyatt
whose voice was exceedingly quiet
And then one day
it faded away"""

        e = """An exceedingly fat friend of mine,
When asked at what hour he'd dine,
Replied, "At eleven,
At three, five, and seven,
And eight and a quarter past nine"""

        f = """A limerick fan from Australia
regarded his work as a failure:
his verses were fine
until the fourth line"""

        g = """There was a young lady one fall
Who wore a newspaper dress to a ball.
The dress caught fire
And burned her entire
Front page, sporting section and all."""

        self.assertEqual(self.ld.is_limerick(a), True)
        self.assertEqual(self.ld.is_limerick(b), False)
        self.assertEqual(self.ld.is_limerick(c), False)
        self.assertEqual(self.ld.is_limerick(d), False)
        self.assertEqual(self.ld.is_limerick(f), False)
        self.assertEqual(self.ld.is_limerick(e), True)
        self.assertEqual(self.ld.is_limerick(g), True)