Exemplo n.º 1
0
 def test_phrase_splitting_multiple_prons(self):
     text = 'If you favorite that last tweet of mine you are the scum I called out'
     haiku = Haiku(text)
     self.assertEqual(
         haiku.formatted(),
         'If you favorite / that last tweet of mine you are / the scum I called out'
     )
Exemplo n.º 2
0
class TestHaiku(unittest.TestCase):
    def setUp(self):
        self.valid_haiku = Haiku('at the age old pond a frog leaps into water a deep resonance')
        self.valid_haiku_multiple_prons = Haiku('word word probably {0}'.format('word ' * 12))
        self.invalid_haiku = Haiku('at the age old pond')
        self.tricky_invalid_haiku = Haiku('word word word elephant {0}'.format('word ' * 11))
        self.haiku_with_oov = Haiku('at the age old pond a frog GROOOG leaps into water a deep resonance')

    def test_invalid_haiku(self):
        self.assertFalse(self.invalid_haiku.is_valid())

    def test_valid_haiku(self):
        self.assertTrue(self.valid_haiku.is_valid())

    def test_valid_haiku_multiple_prons(self):
        self.assertTrue(self.valid_haiku_multiple_prons.is_valid())

    def test_right_syllables_wrong_phrasing(self):
        """ a potential haiku can have 17 syllables but a word that breaks
            between lines
        """
        self.assertFalse(self.tricky_invalid_haiku.is_valid())

    def test_oov_word(self):
        self.assertFalse(self.haiku_with_oov.is_valid())

    def test_phrase_splitting(self):
        self.assertEqual(
            self.valid_haiku.formatted(),
            'at the age old pond / a frog leaps into water / a deep resonance'
        )

    def test_phrase_splitting_multiple_prons(self):
        text = 'If you favorite that last tweet of mine you are the scum I called out'
        haiku = Haiku(text)
        self.assertEqual(
            haiku.formatted(),
            'If you favorite / that last tweet of mine you are / the scum I called out'
        )
Exemplo n.º 3
0
from __future__ import print_function
import json
import codecs
import sys

from haiku import Haiku

sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
sys.stderr = codecs.getwriter("utf-8")(sys.stderr)

for line in open("sample"):
    if not line.strip():
        continue

    tweet = json.loads(line)

    if not (tweet.get("lang") and tweet["lang"] == "en"):
        continue

    try:
        haiku = Haiku(tweet["text"])
        if haiku.is_valid():
            print(haiku.formatted())
    except Exception as e:
        print("error: " + tweet["text"], file=sys.stderr)
        print(e, file=sys.stderr)