Exemplo n.º 1
0
    def test_append(self):
        utt_a = Utterance()

        word = Word('the', 0.05, 0.25, ['dh', 'iy'], ['dh'], 'DT')
        utt_a.append(word)

        assert_equal(utt_a.words(), [word])
        assert_equal(utt_a.beg(), 0.05)
        assert_equal(utt_a.end(), 0.25)
        assert_equal(utt_a.dur(), 0.25 - 0.05)

        uh = Word('uh', 0.25, 0.45, ['ah'], ['ah'], 'UH')
        utt_a.append(uh)

        assert_equal(utt_a.words(), [word, uh])
        assert_equal(utt_a.beg(), 0.05)
        assert_equal(utt_a.end(), 0.45)
        assert_equal(utt_a.dur(), 0.45 - 0.05)
Exemplo n.º 2
0
    def test_append(self):
        utt_a = Utterance()

        word = Word('the', 0.05, 0.25, ['dh', 'iy'], ['dh'], 'DT')
        utt_a.append(word)

        assert_equal(utt_a.words(), [word])
        assert_equal(utt_a.beg(), 0.05)
        assert_equal(utt_a.end(), 0.25)
        assert_equal(utt_a.dur(), 0.25 - 0.05)

        uh = Word('uh', 0.25, 0.45, ['ah'], ['ah'], 'UH')
        utt_a.append(uh)

        assert_equal(utt_a.words(), [word, uh])
        assert_equal(utt_a.beg(), 0.05)
        assert_equal(utt_a.end(), 0.45)
        assert_equal(utt_a.dur(), 0.45 - 0.05)
Exemplo n.º 3
0
class TestUtterance(object):
    def setup(self):
        self.words = [
            Word('the', 0, 0.10, ['dh', 'iy'], ['dh'], 'DT'),
            Word('cat', 0.10, 0.39, ['k', 'ae', 't'], ['k', 'ae', 't'], 'NN'),
            Word('is', 0.39, 0.55, ['ih', 'z'], ['ih', 'z'], 'VB'),
            Word('on', 0.55, 0.73, ['aa', 'n'], ['aan'], 'IN'),
            Word('the', 0.73, 0.80, ['dh', 'iy'], ['dh', 'uh'], 'DT'),
            Word('mat', 0.80, 1.25, ['m', 'ae', 't'], ['m', 'ae', 't'], 'NN')
        ]

        self.utt = Utterance(self.words)
        self.empty_utt = Utterance()

    def test_utterance(self):
        assert_equal(self.utt.beg(), 0)
        assert_equal(self.utt.end(), 1.25)
        assert_equal(self.utt.dur(), 1.25 - 0)
        assert_equal(self.utt.words(), self.words)

    def test_empty_utterance(self):
        assert_raises(IndexError, self.empty_utt.beg)
        assert_raises(IndexError, self.empty_utt.end)
        assert_raises(IndexError, self.empty_utt.dur)
        assert_equal(self.empty_utt.words(), [])

    def test_backwards_utterance(self):
        utt_backwards = Utterance(self.words[::-1])
        assert_equal(utt_backwards._words, self.words)

    @raises(TypeError)
    def test_bad_word_types(self):
        words = range(4)
        utt = Utterance(words)

    @raises(ValueError)
    def test_flipped_word(self):
        word = Word('the', 0.10, 0, ['dh', 'iy'], ['dh'], 'DT')
        utt = Utterance([word])

    @raises(ValueError)
    def test_overlapping_words(self):
        words = [
            Word('the', 0, 0.10, ['dh', 'iy'], ['dh'], 'DT'),
            Word('cat', 0.05, 0.39, ['k', 'ae', 't'], ['k', 'ae', 't'], 'NN')
        ]

        utt = Utterance(words)

    def test_append(self):
        utt_a = Utterance()

        word = Word('the', 0.05, 0.25, ['dh', 'iy'], ['dh'], 'DT')
        utt_a.append(word)

        assert_equal(utt_a.words(), [word])
        assert_equal(utt_a.beg(), 0.05)
        assert_equal(utt_a.end(), 0.25)
        assert_equal(utt_a.dur(), 0.25 - 0.05)

        uh = Word('uh', 0.25, 0.45, ['ah'], ['ah'], 'UH')
        utt_a.append(uh)

        assert_equal(utt_a.words(), [word, uh])
        assert_equal(utt_a.beg(), 0.05)
        assert_equal(utt_a.end(), 0.45)
        assert_equal(utt_a.dur(), 0.45 - 0.05)

    @raises(TypeError)
    def test_append_missing(self):
        self.empty_utt.append('the')

    @raises(ValueError)
    def test_append_overlap(self):
        word = Word('uh', 0.25, 0.45, ['ah'], ['ah'], 'UH')
        self.utt.append(word)

    @raises(TypeError)
    def test_append_beg_none(self):
        word = Word('uh', None, 0.45, ['ah'], ['ah'], 'UH')
        self.utt.append(word)

    @raises(TypeError)
    def test_append_end_none(self):
        word = Word('uh', 0.25, None, ['ah'], ['ah'], 'UH')
        self.utt.append(word)

    @raises(ValueError)
    def test_append_backwards(self):
        word = Word('uh', 1.95, 1.65, ['ah'], ['ah'], 'UH')
        self.empty_utt.append(word)

    def test_append_none(self):
        word = Word('the', 0.05, 0.25, ['dh', 'iy'], ['dh'], 'DT')
        utt_append_none = Utterance([word])

        none_word = Word('uh', None, None, ['ah'], ['ah'], 'UH')
        assert_raises(TypeError, utt_append_none.append, none_word)

        none_beg_word = Word('uh', None, 0.5, ['ah'], ['ah'], 'UH')
        assert_raises(TypeError, utt_append_none.append, none_beg_word)

        none_end_word = Word('uh', 0.65, None, ['ah'], ['ah'], 'UH')
        assert_raises(TypeError, utt_append_none.append, none_end_word)

    def test_len(self):
        assert_equal(len(self.utt), 6)

    def test_speech_rate(self):
        assert_equal(self.utt.speech_rate(), 4.0)
        assert_equal(self.utt.speech_rate(use_phonetic=False), 4.8)
        assert_raises(ValueError, self.empty_utt.speech_rate)
        assert_raises(ValueError, self.empty_utt.speech_rate, False)

    def test_speech_rate_pause_at_beg(self):
        words = [Pause(beg=0, end=0.39)] + self.words[2:]
        utt = Utterance(words)

        # raises
        assert_raises(ValueError, utt.speech_rate)
        assert_raises(ValueError, utt.speech_rate, False, 'raises')

        # zero
        assert_equal(utt.speech_rate(no_syllables='zero'), 3.2)

        # squeeze
        assert_equal(utt.speech_rate(no_syllables='squeeze'), 4 / 0.86)

    def test_speech_rate_pause_at_beg_and_middle(self):
        words = [Pause(beg=0, end=0.39)] + self.words[2:]
        words[2] = Pause(beg=0.55, end=0.73)
        utt = Utterance(words)

        # raises
        assert_raises(ValueError, utt.speech_rate)
        assert_raises(ValueError, utt.speech_rate, False, 'raises')

        # zero
        assert_equal(utt.speech_rate(no_syllables='zero'), 2.4)

        # squeeze
        assert_equal(utt.speech_rate(no_syllables='squeeze'), 3 / 0.86)

    def test_speech_rate_pause_at_end(self):
        utt = Utterance(self.words)
        utt.append(Pause(beg=1.25, end=1.50))

        # raises
        assert_raises(ValueError, utt.speech_rate)
        assert_raises(ValueError, utt.speech_rate, False, 'raises')

        # zero
        assert_equal(utt.speech_rate(no_syllables='zero'), 10 / 3)

        # squeeze
        assert_equal(utt.speech_rate(no_syllables='squeeze'), 4.0)

    def test_speech_rate_pause_at_end_and_middle(self):
        words = self.words + [Pause(beg=1.25, end=1.50)]
        words[-3] = Pause(beg=0.73, end=0.80)

        utt = Utterance(words)

        # raises
        assert_raises(ValueError, utt.speech_rate)
        assert_raises(ValueError, utt.speech_rate, False, 'raises')

        # zero
        assert_equal(utt.speech_rate(no_syllables='zero'), 8 / 3)

        # squeeze
        assert_equal(utt.speech_rate(no_syllables='squeeze'), 4 / 1.25)

    def test_speech_rate_none_at_edge(self):
        utt = Utterance(self.words[:])
        word = Word('the', None, 0.10, ['dh', 'iy'], ['dh'], 'DT')

        # normally there couldn't be a None-duration word in the utterance
        # anyway
        utt._words[0] = word

        assert_raises(TypeError, utt.speech_rate)
        assert_raises(TypeError, utt.speech_rate, False, 'zero')
        assert_raises(TypeError, utt.speech_rate, False, 'squeeze')

    def test_strip_beg_pause(self):
        pause = Pause(beg=0, end=0.55)
        utt_strip = Utterance([pause] + self.words[3:])

        utt_strip.strip()

        assert_not_in(pause, utt_strip)
        assert_equal(utt_strip.words(), self.words[3:])

    def test_strip_end_pause(self):
        pause = Pause(beg=1.25, end=1.85)
        utt_strip = Utterance(self.words + [pause])

        utt_strip.strip()

        assert_not_in(pause, utt_strip)
        assert_equal(utt_strip.words(), self.words)

    def test_strip_beg_zero(self):
        zero = Word('', 0.0, 0.0)
        utt_strip = Utterance([zero] + self.words)

        utt_strip.strip()

        assert_not_in(zero, utt_strip)
        assert_equal(utt_strip.words(), self.words)

    def test_strip_end_zero(self):
        zero = Word('', 1.25, 1.25)
        utt_strip = Utterance(self.words + [zero])

        utt_strip.strip()

        assert_not_in(zero, utt_strip)
        assert_equal(utt_strip.words(), self.words)

    def test_strip_beg_multiple(self):
        pause = Pause(beg=0, end=0.39)
        zero = Word('', 0.39, 0.39)
        utt_strip = Utterance()
        utt_strip._words = [pause, zero] + self.words[2:]

        utt_strip.strip()

        for entry in {pause, zero}:
            assert_not_in(entry, utt_strip)

        assert_equal(utt_strip.words(), self.words[2:])

    def test_strip_end_multiple(self):
        pause = Pause(beg=1.25, end=1.95)
        zero = Word('', 1.95, 1.95)
        utt_strip = Utterance()
        utt_strip._words = self.words + [pause, zero]

        utt_strip.strip()

        for entry in {pause, zero}:
            assert_not_in(entry, utt_strip)

        assert_equal(utt_strip.words(), self.words)

    def test_strip_pause_only(self):
        pause = Pause(beg=1.25, end=1.95)
        utt_strip = Utterance([pause])

        utt_strip.strip()

        assert_equal(utt_strip.words(), [])

    def test_repr(self):
        expected_repr = (
            "Utterance(["
            "Word('the', 0, 0.1, ['dh', 'iy'], ['dh'], 'DT'), "
            "Word('cat', 0.1, 0.39, ['k', 'ae', 't'], ['k', 'ae', 't'], 'NN'), "
            "Word('is', 0.39, 0.55, ['ih', 'z'], ['ih', 'z'], 'VB'), "
            "Word('on', 0.55, 0.73, ['aa', 'n'], ['aan'], 'IN'), "
            "Word('the', 0.73, 0.8, ['dh', 'iy'], ['dh', 'uh'], 'DT'), "
            "Word('mat', 0.8, 1.25, ['m', 'ae', 't'], ['m', 'ae', 't'], 'NN')"
            "])")
        assert_equal(repr(self.utt), expected_repr)

    def test_str(self):
        assert_equal(str(self.utt), '<Utterance "the cat is on the mat">')
Exemplo n.º 4
0
class TestUtterance(object):

    def setup(self):
        self.words = [
            Word('the', 0, 0.10, ['dh', 'iy'], ['dh'], 'DT'),
            Word('cat', 0.10, 0.39, ['k', 'ae', 't'], ['k', 'ae', 't'], 'NN'),
            Word('is', 0.39, 0.55, ['ih', 'z'], ['ih', 'z'], 'VB'),
            Word('on', 0.55, 0.73, ['aa', 'n'], ['aan'], 'IN'),
            Word('the', 0.73, 0.80, ['dh', 'iy'], ['dh', 'uh'], 'DT'),
            Word('mat', 0.80, 1.25, ['m', 'ae', 't'], ['m', 'ae', 't'], 'NN')]

        self.utt = Utterance(self.words)
        self.empty_utt = Utterance()

    def test_utterance(self):
        assert_equal(self.utt.beg(), 0)
        assert_equal(self.utt.end(), 1.25)
        assert_equal(self.utt.dur(), 1.25 - 0)
        assert_equal(self.utt.words(), self.words)

    def test_empty_utterance(self):
        assert_raises(IndexError, self.empty_utt.beg)
        assert_raises(IndexError, self.empty_utt.end)
        assert_raises(IndexError, self.empty_utt.dur)
        assert_equal(self.empty_utt.words(), [])

    def test_backwards_utterance(self):
        utt_backwards = Utterance(self.words[::-1])
        assert_equal(utt_backwards._words, self.words)

    @raises(TypeError)
    def test_bad_word_types(self):
        words = range(4)
        utt = Utterance(words)

    @raises(ValueError)
    def test_flipped_word(self):
        word = Word('the', 0.10, 0, ['dh', 'iy'], ['dh'], 'DT')
        utt = Utterance([word])

    @raises(ValueError)
    def test_overlapping_words(self):
        words = [
            Word('the', 0, 0.10, ['dh', 'iy'], ['dh'], 'DT'),
            Word('cat', 0.05, 0.39, ['k', 'ae', 't'], ['k', 'ae', 't'], 'NN')]

        utt = Utterance(words)

    def test_append(self):
        utt_a = Utterance()

        word = Word('the', 0.05, 0.25, ['dh', 'iy'], ['dh'], 'DT')
        utt_a.append(word)

        assert_equal(utt_a.words(), [word])
        assert_equal(utt_a.beg(), 0.05)
        assert_equal(utt_a.end(), 0.25)
        assert_equal(utt_a.dur(), 0.25 - 0.05)

        uh = Word('uh', 0.25, 0.45, ['ah'], ['ah'], 'UH')
        utt_a.append(uh)

        assert_equal(utt_a.words(), [word, uh])
        assert_equal(utt_a.beg(), 0.05)
        assert_equal(utt_a.end(), 0.45)
        assert_equal(utt_a.dur(), 0.45 - 0.05)

    @raises(TypeError)
    def test_append_missing(self):
        self.empty_utt.append('the')

    @raises(ValueError)
    def test_append_overlap(self):
        word = Word('uh', 0.25, 0.45, ['ah'], ['ah'], 'UH')
        self.utt.append(word)

    @raises(TypeError)
    def test_append_beg_none(self):
        word = Word('uh', None, 0.45, ['ah'], ['ah'], 'UH')
        self.utt.append(word)

    @raises(TypeError)
    def test_append_end_none(self):
        word = Word('uh', 0.25, None, ['ah'], ['ah'], 'UH')
        self.utt.append(word)

    @raises(ValueError)
    def test_append_backwards(self):
        word = Word('uh', 1.95, 1.65, ['ah'], ['ah'], 'UH')
        self.empty_utt.append(word)

    def test_append_none(self):
        word = Word('the', 0.05, 0.25, ['dh', 'iy'], ['dh'], 'DT')
        utt_append_none = Utterance([word])

        none_word = Word('uh', None, None, ['ah'], ['ah'], 'UH')
        assert_raises(TypeError, utt_append_none.append, none_word)

        none_beg_word = Word('uh', None, 0.5, ['ah'], ['ah'], 'UH')
        assert_raises(TypeError, utt_append_none.append, none_beg_word)

        none_end_word = Word('uh', 0.65, None, ['ah'], ['ah'], 'UH')
        assert_raises(TypeError, utt_append_none.append, none_end_word)

    def test_len(self):
        assert_equal(len(self.utt), 6)

    def test_speech_rate(self):
        assert_equal(self.utt.speech_rate(), 4.0)
        assert_equal(self.utt.speech_rate(use_phonetic=False), 4.8)
        assert_raises(ValueError, self.empty_utt.speech_rate)
        assert_raises(ValueError, self.empty_utt.speech_rate, False)

    def test_speech_rate_pause_at_beg(self):
        words = [Pause(beg=0, end=0.39)] + self.words[2:]
        utt = Utterance(words)

        # raises
        assert_raises(ValueError, utt.speech_rate)
        assert_raises(ValueError, utt.speech_rate, False, 'raises')

        # zero
        assert_equal(utt.speech_rate(no_syllables='zero'), 3.2)

        # squeeze
        assert_equal(utt.speech_rate(no_syllables='squeeze'), 4/0.86)

    def test_speech_rate_pause_at_beg_and_middle(self):
        words = [Pause(beg=0, end=0.39)] + self.words[2:]
        words[2] = Pause(beg=0.55, end=0.73)
        utt = Utterance(words)

        # raises
        assert_raises(ValueError, utt.speech_rate)
        assert_raises(ValueError, utt.speech_rate, False, 'raises')

        # zero
        assert_equal(utt.speech_rate(no_syllables='zero'), 2.4)

        # squeeze
        assert_equal(utt.speech_rate(no_syllables='squeeze'), 3/0.86)

    def test_speech_rate_pause_at_end(self):
        utt = Utterance(self.words)
        utt.append(Pause(beg=1.25, end=1.50))

        # raises
        assert_raises(ValueError, utt.speech_rate)
        assert_raises(ValueError, utt.speech_rate, False, 'raises')

        # zero
        assert_equal(utt.speech_rate(no_syllables='zero'), 10/3)

        # squeeze
        assert_equal(utt.speech_rate(no_syllables='squeeze'), 4.0)

    def test_speech_rate_pause_at_end_and_middle(self):
        words = self.words + [Pause(beg=1.25, end=1.50)]
        words[-3] = Pause(beg=0.73, end=0.80)

        utt = Utterance(words)

        # raises
        assert_raises(ValueError, utt.speech_rate)
        assert_raises(ValueError, utt.speech_rate, False, 'raises')

        # zero
        assert_equal(utt.speech_rate(no_syllables='zero'), 8/3)

        # squeeze
        assert_equal(utt.speech_rate(no_syllables='squeeze'), 4/1.25)

    def test_speech_rate_none_at_edge(self):
        utt = Utterance(self.words[:])
        word = Word('the', None, 0.10, ['dh', 'iy'], ['dh'], 'DT')

        # normally there couldn't be a None-duration word in the utterance
        # anyway
        utt._words[0] = word

        assert_raises(TypeError, utt.speech_rate)
        assert_raises(TypeError, utt.speech_rate, False, 'zero')
        assert_raises(TypeError, utt.speech_rate, False, 'squeeze')

    def test_strip_beg_pause(self):
        pause = Pause(beg=0, end=0.55)
        utt_strip = Utterance([pause] + self.words[3:])

        utt_strip.strip()

        assert_not_in(pause, utt_strip)
        assert_equal(utt_strip.words(), self.words[3:])

    def test_strip_end_pause(self):
        pause = Pause(beg=1.25, end=1.85)
        utt_strip = Utterance(self.words + [pause])

        utt_strip.strip()

        assert_not_in(pause, utt_strip)
        assert_equal(utt_strip.words(), self.words)
    
    def test_strip_beg_zero(self):
        zero = Word('', 0.0, 0.0)
        utt_strip = Utterance([zero] + self.words)

        utt_strip.strip()

        assert_not_in(zero, utt_strip)
        assert_equal(utt_strip.words(), self.words)

    def test_strip_end_zero(self):
        zero = Word('', 1.25, 1.25)
        utt_strip = Utterance(self.words + [zero])

        utt_strip.strip()

        assert_not_in(zero, utt_strip)
        assert_equal(utt_strip.words(), self.words)

    def test_strip_beg_multiple(self):
        pause = Pause(beg=0, end=0.39)
        zero = Word('', 0.39, 0.39)
        utt_strip = Utterance()
        utt_strip._words = [pause, zero] + self.words[2:]
        
        utt_strip.strip()
        
        for entry in {pause, zero}:
            assert_not_in(entry, utt_strip)

        assert_equal(utt_strip.words(), self.words[2:])
    
    def test_strip_end_multiple(self):
        pause = Pause(beg=1.25, end=1.95)
        zero = Word('', 1.95, 1.95)
        utt_strip = Utterance()
        utt_strip._words = self.words + [pause, zero]

        utt_strip.strip()

        for entry in {pause, zero}:
            assert_not_in(entry, utt_strip)

        assert_equal(utt_strip.words(), self.words)

    def test_strip_pause_only(self):
        pause = Pause(beg=1.25, end=1.95)
        utt_strip = Utterance([pause])
        
        utt_strip.strip()
        
        assert_equal(utt_strip.words(), [])

    def test_repr(self):
        expected_repr = ("Utterance(["
                          "Word('the', 0, 0.1, ['dh', 'iy'], ['dh'], 'DT'), "
                          "Word('cat', 0.1, 0.39, ['k', 'ae', 't'], ['k', 'ae', 't'], 'NN'), "
                          "Word('is', 0.39, 0.55, ['ih', 'z'], ['ih', 'z'], 'VB'), "
                          "Word('on', 0.55, 0.73, ['aa', 'n'], ['aan'], 'IN'), "
                          "Word('the', 0.73, 0.8, ['dh', 'iy'], ['dh', 'uh'], 'DT'), "
                          "Word('mat', 0.8, 1.25, ['m', 'ae', 't'], ['m', 'ae', 't'], 'NN')"
                         "])")
        assert_equal(repr(self.utt), expected_repr)

    def test_str(self):
        assert_equal(str(self.utt), '<Utterance "the cat is on the mat">')