def dummy_sync_map(self): sync_map = SyncMap() frag = TextFragment("f001", Language.EN, "Fragment 1") sync_map.append(SyncMapFragment(frag, 0, 12.345)) frag = TextFragment("f002", Language.EN, "Fragment 2") sync_map.append(SyncMapFragment(frag, 12.345, 23.456)) frag = TextFragment("f003", Language.EN, "Fragment 3") sync_map.append(SyncMapFragment(frag, 23.456, 34.567)) return sync_map
def dummy_sync_map(self): sync_map = SyncMap() frag = TextFragment(u"f001", Language.ENG, [u"Fragment 1"]) sync_map.add_fragment(SyncMapFragment(frag, 0, 12.345)) frag = TextFragment(u"f002", Language.ENG, [u"Fragment 2"]) sync_map.add_fragment(SyncMapFragment(frag, 12.345, 23.456)) frag = TextFragment(u"f003", Language.ENG, [u"Fragment 3"]) sync_map.add_fragment(SyncMapFragment(frag, 23.456, 34.567)) return sync_map
def test_is_single_level_false(self): smf2 = SyncMapFragment() child2 = Tree(value=smf2) smf = SyncMapFragment() child = Tree(value=smf) child.add_child(child2) tree = Tree() tree.add_child(child) syn = SyncMap(tree=tree) self.assertFalse(syn.is_single_level)
def test_fragment_nonspeech_rate_slack(self): params = [ ("20.000", "0.000", "1.000"), ("10.000", "0.000", "1.000"), ("5.000", "0.000", "1.000") ] text = TextFragment() for r, e_zero, e_nonzero in params: frag = SyncMapFragment(text_fragment=text, fragment_type=SyncMapFragment.NONSPEECH, begin=TimeValue("1.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_zero)) frag = SyncMapFragment(text_fragment=text, fragment_type=SyncMapFragment.NONSPEECH, begin=TimeValue("0.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_nonzero))
def test_fragment_regular_rate_slack(self): params = [ ("20.000", "-0.500", "0.500"), ("10.000", "-1.000", "0.000"), ("5.000", "-2.000", "-1.000") ] text = TextFragment(lines=[u"Hello", u"World"]) for r, e_zero, e_nonzero in params: frag = SyncMapFragment(text_fragment=text, fragment_type=SyncMapFragment.REGULAR, begin=TimeValue("1.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_zero)) frag = SyncMapFragment(text_fragment=text, fragment_type=SyncMapFragment.REGULAR, begin=TimeValue("0.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_nonzero))
def test_fragment_not_regular_rate_lack(self): params = [ ("20.000", "0.000", "0.000"), ("10.000", "0.000", "0.000"), ("5.000", "0.000", "0.000") ] text = TextFragment() for t in SyncMapFragment.NOT_REGULAR_TYPES: for r, e_zero, e_nonzero in params: frag = SyncMapFragment(text_fragment=text, fragment_type=t, begin=TimeValue("1.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_lack(Decimal(r)), TimeValue(e_zero)) frag = SyncMapFragment(text_fragment=text, fragment_type=t, begin=TimeValue("0.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_lack(Decimal(r)), TimeValue(e_nonzero))
def test_fragment_head_tail_rate_slack(self): params = [ ("20.000", "0.000", "0.000"), ("10.000", "0.000", "0.000"), ("5.000", "0.000", "0.000") ] text = TextFragment() for t in [SyncMapFragment.HEAD, SyncMapFragment.TAIL]: for r, e_zero, e_nonzero in params: frag = SyncMapFragment(text_fragment=text, fragment_type=t, begin=TimeValue("1.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_zero)) frag = SyncMapFragment(text_fragment=text, fragment_type=t, begin=TimeValue("0.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_nonzero))
def test_fragments_tree_not_empty(self): smf = SyncMapFragment() child = Tree(value=smf) tree = Tree() tree.add_child(child) syn = SyncMap(tree=tree) self.assertEqual(len(syn.fragments_tree), 1)
def test_fragment_constructor_interval_nonzero(self): interval = TimeInterval(begin=TimeValue("1.000"), end=TimeValue("3.000")) frag = SyncMapFragment(interval=interval) self.assertEqual(frag.length, TimeValue("2.000")) self.assertEqual(frag.chars, 0) self.assertEqual(frag.rate, 0)
def test_is_single_level_true_not_empty(self): smf = SyncMapFragment() child = Tree(value=smf) tree = Tree() tree.add_child(child) syn = SyncMap(tree=tree) self.assertTrue(syn.is_single_level)
def test_fragment_rate_zero(self): text = TextFragment(lines=[u"Hello", u"World"]) frag = SyncMapFragment(text_fragment=text, begin=TimeValue("1.234"), end=TimeValue("1.234")) self.assertEqual(frag.audio_duration, 0) self.assertEqual(frag.chars, 10) self.assertIsNone(frag.rate, None)
def test_fragment_length(self): text = TextFragment(lines=[u"Hello", u"World"]) frag = SyncMapFragment(text_fragment=text, begin=TimeValue("1.234"), end=TimeValue("6.234")) self.assertEqual(frag.chars, 10) self.assertEqual(frag.length, 5) self.assertEqual(frag.has_zero_length, False)
def test_fragment_rate_valid(self): text = TextFragment(lines=[u"Hello", u"World"]) frag = SyncMapFragment(text_fragment=text, begin=TimeValue("1.234"), end=TimeValue("6.234")) self.assertEqual(frag.audio_duration, 5) self.assertEqual(frag.chars, 10) self.assertEqual(frag.rate, 2.000) self.assertEqual(frag.rate, Decimal("2.000"))
def test_fragment_regular_rate_zero_length(self): text = TextFragment(lines=[u"Hello", u"World"]) frag = SyncMapFragment(text_fragment=text, fragment_type=SyncMapFragment.REGULAR, begin=TimeValue("1.234"), end=TimeValue("1.234")) self.assertEqual(frag.length, 0) self.assertEqual(frag.chars, 10) self.assertIsNone(frag.rate)
def dummy_sync_map(self): sync_map = SyncMap() frag = TextFragment(u"f001", Language.ENG, [u"Fragment 1"]) sync_map.add_fragment( SyncMapFragment(text_fragment=frag, begin=TimeValue("0.000"), end=TimeValue("12.345"))) frag = TextFragment(u"f002", Language.ENG, [u"Fragment 2"]) sync_map.add_fragment( SyncMapFragment(text_fragment=frag, begin=TimeValue("12.345"), end=TimeValue("23.456"))) frag = TextFragment(u"f003", Language.ENG, [u"Fragment 3"]) sync_map.add_fragment( SyncMapFragment(text_fragment=frag, begin=TimeValue("23.456"), end=TimeValue("34.567"))) return sync_map
def test_fragment_regular_rate_zero_text(self): text = TextFragment() frag = SyncMapFragment(text_fragment=text, fragment_type=SyncMapFragment.REGULAR, begin=TimeValue("1.234"), end=TimeValue("6.234")) self.assertEqual(frag.length, 5) self.assertEqual(frag.chars, 0) self.assertEqual(frag.rate, 0)
def test_fragment_not_regular_rate_zero_length(self): for t in SyncMapFragment.NOT_REGULAR_TYPES: text = TextFragment() frag = SyncMapFragment(text_fragment=text, fragment_type=t, begin=TimeValue("1.234"), end=TimeValue("1.234")) self.assertEqual(frag.length, 0) self.assertEqual(frag.chars, 0) self.assertIsNone(frag.rate)
def test_fragment_regular_rate_non_zero(self): text = TextFragment(lines=[u"Hello", u"World"]) frag = SyncMapFragment(text_fragment=text, fragment_type=SyncMapFragment.REGULAR, begin=TimeValue("1.234"), end=TimeValue("6.234")) self.assertEqual(frag.length, 5) self.assertEqual(frag.chars, 10) self.assertEqual(frag.rate, 2.000) self.assertEqual(frag.rate, Decimal("2.000"))
def test_has_zero_length_leaves(self): params = [ ([("0.000", "0.000"), ("0.000", "0.000")], True), ([("0.000", "0.000"), ("0.000", "1.000")], True), ([("0.000", "1.000"), ("1.000", "1.000")], True), ([("0.000", "1.000"), ("1.000", "2.000")], False), ([("0.000", "0.000"), ("1.000", "1.000")], True), ([("0.000", "0.000"), ("1.000", "2.000")], True), ([("0.000", "1.000"), ("2.000", "2.000")], True), ([("0.000", "1.000"), ("2.000", "3.000")], False), ] for l, exp in params: tree = Tree() for b, e in l: interval = TimeInterval(begin=TimeValue(b), end=TimeValue(e)) smf = SyncMapFragment(interval=interval) child = Tree(value=smf) tree.add_child(child, as_last=True) syn = SyncMap(tree=tree) self.assertEqual(syn.has_zero_length_leaves, exp)
def test_leaves_are_consistent(self): params = [ ([("0.000", "0.000"), ("0.000", "0.000")], True), ([("0.000", "0.000"), ("0.000", "1.000")], True), ([("0.000", "1.000"), ("1.000", "1.000")], True), ([("0.000", "1.000"), ("1.000", "2.000")], True), ([("0.000", "0.000"), ("1.000", "1.000")], True), ([("0.000", "0.000"), ("1.000", "2.000")], True), ([("0.000", "1.000"), ("2.000", "2.000")], True), ([("0.000", "1.000"), ("2.000", "3.000")], True), ([("0.000", "1.000"), ("1.000", "1.000"), ("1.000", "2.000")], True), ([("0.000", "1.000"), ("1.000", "1.000"), ("2.000", "2.000")], True), ([("0.000", "1.000"), ("2.000", "3.000"), ("1.500", "1.500")], True), ([("0.000", "1.000"), ("2.000", "3.000"), ("1.500", "1.750")], True), ([("0.000", "1.000"), ("1.040", "2.000")], True), ([("0.000", "1.000"), ("0.000", "0.500")], False), ([("0.000", "1.000"), ("0.000", "1.000")], False), ([("0.000", "1.000"), ("0.000", "1.500")], False), ([("0.000", "1.000"), ("0.500", "0.500")], False), ([("0.000", "1.000"), ("0.500", "0.750")], False), ([("0.000", "1.000"), ("0.500", "1.000")], False), ([("0.000", "1.000"), ("0.500", "1.500")], False), ([("0.000", "1.000"), ("2.000", "2.000"), ("1.500", "2.500")], False), ([("0.000", "1.000"), ("2.000", "3.000"), ("1.500", "2.500")], False), ([("0.000", "1.000"), ("0.960", "2.000")], False), ] for l, exp in params: tree = Tree() for b, e in l: interval = TimeInterval(begin=TimeValue(b), end=TimeValue(e)) smf = SyncMapFragment(interval=interval) child = Tree(value=smf) tree.add_child(child, as_last=True) syn = SyncMap(tree=tree) self.assertEqual(syn.leaves_are_consistent, exp)
def test_fragment_nonspeech_rate_slack(self): params = [("20.000", "0.000", "1.000"), ("10.000", "0.000", "1.000"), ("5.000", "0.000", "1.000")] text = TextFragment() for r, e_zero, e_nonzero in params: frag = SyncMapFragment(text_fragment=text, fragment_type=SyncMapFragment.NONSPEECH, begin=TimeValue("1.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_zero)) frag = SyncMapFragment(text_fragment=text, fragment_type=SyncMapFragment.NONSPEECH, begin=TimeValue("0.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_nonzero))
def test_fragment_regular_rate_slack(self): params = [("20.000", "-0.500", "0.500"), ("10.000", "-1.000", "0.000"), ("5.000", "-2.000", "-1.000")] text = TextFragment(lines=[u"Hello", u"World"]) for r, e_zero, e_nonzero in params: frag = SyncMapFragment(text_fragment=text, fragment_type=SyncMapFragment.REGULAR, begin=TimeValue("1.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_zero)) frag = SyncMapFragment(text_fragment=text, fragment_type=SyncMapFragment.REGULAR, begin=TimeValue("0.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_nonzero))
def _level_time_map_to_tree(self, text_file, time_map, tree=None, add_head_tail=True): """ Convert a level time map into a Tree of SyncMapFragments. The time map is a list of pairs ``[start_time, end_time]``, of length equal to number of fragments + 2, where the two extra elements are for the HEAD (first) and TAIL (last). :param text_file: the text file object :type text_file: :class:`~aeneas.textfile.TextFile` :param list time_map: the time map :param tree: the tree; if ``None``, a new Tree will be built :type tree: :class:`~aeneas.tree.Tree` :rtype: :class:`~aeneas.tree.Tree` """ if tree is None: tree = Tree() if add_head_tail: fragments = ([ TextFragment(u"HEAD", self.task.configuration["language"], [u""]) ] + text_file.fragments + [ TextFragment(u"TAIL", self.task.configuration["language"], [u""]) ]) i = 0 else: fragments = text_file.fragments i = 1 for fragment in fragments: interval = time_map[i] sm_frag = SyncMapFragment(fragment, interval[0], interval[1]) tree.add_child(Tree(value=sm_frag)) i += 1 return tree
def test_fragment_not_regular_rate_lack(self): params = [("20.000", "0.000", "0.000"), ("10.000", "0.000", "0.000"), ("5.000", "0.000", "0.000")] text = TextFragment() for t in SyncMapFragment.NOT_REGULAR_TYPES: for r, e_zero, e_nonzero in params: frag = SyncMapFragment(text_fragment=text, fragment_type=t, begin=TimeValue("1.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_lack(Decimal(r)), TimeValue(e_zero)) frag = SyncMapFragment(text_fragment=text, fragment_type=t, begin=TimeValue("0.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_lack(Decimal(r)), TimeValue(e_nonzero))
def test_fragment_head_tail_rate_slack(self): params = [("20.000", "0.000", "0.000"), ("10.000", "0.000", "0.000"), ("5.000", "0.000", "0.000")] text = TextFragment() for t in [SyncMapFragment.HEAD, SyncMapFragment.TAIL]: for r, e_zero, e_nonzero in params: frag = SyncMapFragment(text_fragment=text, fragment_type=t, begin=TimeValue("1.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_zero)) frag = SyncMapFragment(text_fragment=text, fragment_type=t, begin=TimeValue("0.000"), end=TimeValue("1.000")) self.assertEqual(frag.rate_slack(Decimal(r)), TimeValue(e_nonzero))
def test_fragment_ordering(self): t_0_0 = SyncMapFragment(begin=TimeValue("0.000"), end=TimeValue("0.000")) t_0_1 = SyncMapFragment(begin=TimeValue("0.000"), end=TimeValue("1.000")) t_0_3 = SyncMapFragment(begin=TimeValue("0.000"), end=TimeValue("3.000")) q_0_3 = SyncMapFragment(begin=TimeValue("0.000"), end=TimeValue("3.000")) t_2_2 = SyncMapFragment(begin=TimeValue("2.000"), end=TimeValue("2.000")) q_2_2 = SyncMapFragment(begin=TimeValue("2.000"), end=TimeValue("2.000")) self.assertTrue(t_0_0 <= t_0_0) self.assertTrue(t_0_0 == t_0_0) self.assertTrue(t_0_0 >= t_0_0) self.assertFalse(t_0_0 != t_0_0) self.assertTrue(t_0_1 <= t_0_1) self.assertTrue(t_0_1 == t_0_1) self.assertTrue(t_0_1 >= t_0_1) self.assertTrue(t_0_0 < t_0_1) self.assertTrue(t_0_0 < t_0_3) self.assertTrue(t_0_0 < t_2_2) self.assertTrue(t_0_0 <= t_0_1) self.assertTrue(t_0_0 <= t_0_3) self.assertTrue(t_0_0 <= t_2_2) self.assertFalse(t_0_3 < q_0_3) self.assertTrue(t_0_3 <= q_0_3) self.assertTrue(t_0_3 == q_0_3) self.assertTrue(t_0_3 >= q_0_3) self.assertFalse(t_0_3 > q_0_3) self.assertFalse(t_0_3 != q_0_3) self.assertFalse(t_2_2 < q_2_2) self.assertTrue(t_2_2 <= q_2_2) self.assertTrue(t_2_2 == q_2_2) self.assertTrue(t_2_2 >= q_2_2) self.assertFalse(t_2_2 > q_2_2) self.assertFalse(t_2_2 != q_2_2)
def test_fragment_identifier_empty_bis(self): text = TextFragment() frag = SyncMapFragment(text_fragment=text) self.assertIsNone(frag.identifier)
def test_fragment_identifier_empty(self): frag = SyncMapFragment() self.assertIsNone(frag.identifier)
def test_fragment_pretty_print(self): interval = TimeInterval(begin=TimeValue("1.000"), end=TimeValue("3.000")) text = TextFragment(identifier=u"f001", lines=[u"Hello", u"World"]) frag = SyncMapFragment(text_fragment=text, interval=interval) self.assertEqual(frag.pretty_print, u"f001\t1.000\t3.000\tHello World")
def test_fragment_pretty_print_empty_interval(self): text = TextFragment(lines=[u"Hello", u"World"]) frag = SyncMapFragment(text_fragment=text) self.assertEqual(frag.pretty_print, u"\t-2.000\t-1.000\tHello World")
def test_fragment_pretty_print_empty_text(self): interval = TimeInterval(begin=TimeValue("1.000"), end=TimeValue("3.000")) frag = SyncMapFragment(interval=interval) self.assertEqual(frag.pretty_print, u"\t1.000\t3.000\t")
def test_fragment_pretty_print_empty(self): frag = SyncMapFragment() self.assertEqual(frag.pretty_print, u"\t-2.000\t-1.000\t")
def _create_syncmap(self, adjusted_map): """ Create a sync map out of the provided interval map, and store it in the task object. Return a success bool flag. """ self._log("Creating sync map") self._log([ "Number of fragments in adjusted map (including HEAD and TAIL): %d", len(adjusted_map) ]) # adjusted map has 2 elements (HEAD and TAIL) more than text_file if len(adjusted_map) != len(self.task.text_file.fragments) + 2: self._log( "The number of sync map fragments does not match the number of text fragments (+2)", Logger.CRITICAL) return False try: sync_map = SyncMap() head = adjusted_map[0] tail = adjusted_map[-1] # get language language = Language.EN self._log(["Language set to default: %s", language]) if len(self.task.text_file.fragments) > 0: language = self.task.text_file.fragments[0].language self._log(["Language read from text_file: %s", language]) # get head/tail format head_tail_format = self.task.configuration.os_file_head_tail_format # note that str() is necessary, as head_tail_format might be None self._log(["Head/tail format: %s", str(head_tail_format)]) # add head sync map fragment if needed if head_tail_format == SyncMapHeadTailFormat.ADD: head_frag = TextFragment(u"HEAD", language, [u""]) sync_map_frag = SyncMapFragment(head_frag, head[0], head[1]) sync_map.append(sync_map_frag) self._log(["Adding head (ADD): %.3f %.3f", head[0], head[1]]) # stretch first and last fragment timings if needed if head_tail_format == SyncMapHeadTailFormat.STRETCH: self._log([ "Stretching (STRETCH): %.3f => %.3f (head) and %.3f => %.3f (tail)", adjusted_map[1][0], head[0], adjusted_map[-2][1], tail[1] ]) adjusted_map[1][0] = head[0] adjusted_map[-2][1] = tail[1] i = 1 for fragment in self.task.text_file.fragments: start = adjusted_map[i][0] end = adjusted_map[i][1] sync_map_frag = SyncMapFragment(fragment, start, end) sync_map.append(sync_map_frag) i += 1 # add tail sync map fragment if needed if head_tail_format == SyncMapHeadTailFormat.ADD: tail_frag = TextFragment(u"TAIL", language, [u""]) sync_map_frag = SyncMapFragment(tail_frag, tail[0], tail[1]) sync_map.append(sync_map_frag) self._log(["Adding tail (ADD): %.3f %.3f", tail[0], tail[1]]) self.task.sync_map = sync_map self._log("Creating sync map: succeeded") return True except Exception as e: self._log("Creating sync map: failed") self._log(["Message: %s", str(e)]) return False
def test_fragment_constructor_begin_end_nonzero(self): frag = SyncMapFragment(begin=TimeValue("1.000"), end=TimeValue("3.000")) self.assertEqual(frag.length, TimeValue("2.000")) self.assertEqual(frag.chars, 0) self.assertEqual(frag.rate, 0)