예제 #1
0
    def format(self, syncmap):
        from lxml import etree
        # get language
        language = None
        if (self.parameters is not None) and ("language" in self.parameters):
            language = self.parameters["language"]
        elif len(syncmap.fragments) > 0:
            language = syncmap.fragments[0].text_fragment.language
        if language is None:
            language = ""

        # namespaces
        ttml_ns = "http://www.w3.org/ns/ttml"
        xml_ns = "http://www.w3.org/XML/1998/namespace"
        ns_map = {None: ttml_ns}

        # build tree
        tt_elem = etree.Element("{%s}tt" % ttml_ns, nsmap=ns_map)
        tt_elem.attrib["{%s}lang" % xml_ns] = language
        # TODO add metadata from parameters here?
        # COMMENTED head_elem = etree.SubElement(tt_elem, "{%s}head" % ttml_ns)
        body_elem = etree.SubElement(tt_elem, "{%s}body" % ttml_ns)
        div_elem = etree.SubElement(body_elem, "{%s}div" % ttml_ns)

        if syncmap.is_single_level:
            # single level
            for fragment in syncmap.fragments:
                text = fragment.text_fragment
                p_string = u"<p xml:id=\"%s\" begin=\"%s\" end=\"%s\">%s</p>" % (
                    text.identifier, gf.time_to_ttml(fragment.begin),
                    gf.time_to_ttml(fragment.end), u"<br/>".join(text.lines))
                p_elem = etree.fromstring(p_string)
                div_elem.append(p_elem)
        else:
            # TODO support generic multiple levels
            # multiple levels
            for par_child in syncmap.fragments_tree.children_not_empty:
                text = par_child.value.text_fragment
                p_elem = etree.SubElement(div_elem, "{%s}p" % ttml_ns)
                p_elem.attrib["id"] = text.identifier
                for sen_child in par_child.children_not_empty:
                    text = sen_child.value.text_fragment
                    sen_span_elem = etree.SubElement(p_elem,
                                                     "{%s}span" % ttml_ns)
                    sen_span_elem.attrib["id"] = text.identifier
                    for wor_child in sen_child.children_not_empty:
                        fragment = wor_child.value
                        wor_span_elem = etree.SubElement(
                            sen_span_elem, "{%s}span" % ttml_ns)
                        wor_span_elem.attrib[
                            "id"] = fragment.text_fragment.identifier
                        wor_span_elem.attrib["begin"] = gf.time_to_ttml(
                            fragment.begin)
                        wor_span_elem.attrib["end"] = gf.time_to_ttml(
                            fragment.end)
                        wor_span_elem.text = u"<br/>".join(
                            fragment.text_fragment.lines)
        # write tree
        return self._tree_to_string(tt_elem)
예제 #2
0
    def format(self, syncmap):
        from lxml import etree
        # get language
        language = None
        if (self.parameters is not None) and ("language" in self.parameters):
            language = self.parameters["language"]
        elif len(syncmap.fragments) > 0:
            language = syncmap.fragments[0].text_fragment.language
        if language is None:
            language = ""

        # namespaces
        ttml_ns = "http://www.w3.org/ns/ttml"
        xml_ns = "http://www.w3.org/XML/1998/namespace"
        ns_map = {None: ttml_ns}

        # build tree
        tt_elem = etree.Element("{%s}tt" % ttml_ns, nsmap=ns_map)
        tt_elem.attrib["{%s}lang" % xml_ns] = language
        # TODO add metadata from parameters here?
        # COMMENTED head_elem = etree.SubElement(tt_elem, "{%s}head" % ttml_ns)
        body_elem = etree.SubElement(tt_elem, "{%s}body" % ttml_ns)
        div_elem = etree.SubElement(body_elem, "{%s}div" % ttml_ns)

        if syncmap.is_single_level:
            # single level
            for fragment in syncmap.fragments:
                text = fragment.text_fragment
                p_string = u"<p xml:id=\"%s\" begin=\"%s\" end=\"%s\">%s</p>" % (
                    text.identifier,
                    gf.time_to_ttml(fragment.begin),
                    gf.time_to_ttml(fragment.end),
                    u"<br/>".join(text.lines)
                )
                p_elem = etree.fromstring(p_string)
                div_elem.append(p_elem)
        else:
            # TODO support generic multiple levels
            # multiple levels
            for par_child in syncmap.fragments_tree.children_not_empty:
                text = par_child.value.text_fragment
                p_elem = etree.SubElement(div_elem, "{%s}p" % ttml_ns)
                p_elem.attrib["id"] = text.identifier
                for sen_child in par_child.children_not_empty:
                    text = sen_child.value.text_fragment
                    sen_span_elem = etree.SubElement(p_elem, "{%s}span" % ttml_ns)
                    sen_span_elem.attrib["id"] = text.identifier
                    for wor_child in sen_child.children_not_empty:
                        fragment = wor_child.value
                        wor_span_elem = etree.SubElement(sen_span_elem, "{%s}span" % ttml_ns)
                        wor_span_elem.attrib["id"] = fragment.text_fragment.identifier
                        wor_span_elem.attrib["begin"] = gf.time_to_ttml(fragment.begin)
                        wor_span_elem.attrib["end"] = gf.time_to_ttml(fragment.end)
                        wor_span_elem.text = u"<br/>".join(fragment.text_fragment.lines)
        # write tree
        return self._tree_to_string(tt_elem)
예제 #3
0
 def test_time_to_ttml(self):
     tests = [
         [None, "0.000s"],
         [0, "0.000s"],
         [1, "1.000s"],
         [1.234, "1.234s"],
     ]
     for test in tests:
         self.assertEqual(gf.time_to_ttml(test[0]), test[1])
예제 #4
0
 def test_time_to_ttml(self):
     tests = [
         (None, "0.000s"),
         (0, "0.000s"),
         (1, "1.000s"),
         (1.234, "1.234s"),
     ]
     for test in tests:
         self.assertEqual(gf.time_to_ttml(test[0]), test[1])
예제 #5
0
 def test_time_to_ttml(self):
     tests = [
         [None, "0.000s"],
         [0, "0.000s"],
         [1, "1.000s"],
         [1.234, "1.234s"],
     ]
     for test in tests:
         self.assertEqual(gf.time_to_ttml(test[0]), test[1])
예제 #6
0
 def test_time_to_ttml(self):
     tests = [
         (None, "0.000s"),
         (0, "0.000s"),
         (1, "1.000s"),
         (1.234, "1.234s"),
     ]
     for test in tests:
         self.assertEqual(gf.time_to_ttml(test[0]), test[1])
예제 #7
0
    def _write_ttml(self, output_file, parameters):
        """
        Write to TTML file
        """
        # get language
        language = None
        if (parameters is not None) and ("language" in parameters):
            language = parameters["language"]
        elif len(self.fragments) > 0:
            language = self.fragments[0].text_fragment.language
        if language is None:
            language = ""

        # namespaces
        ttml_ns = "http://www.w3.org/ns/ttml"
        xml_ns = "http://www.w3.org/XML/1998/namespace"
        ns_map = {None : ttml_ns}

        # build tree
        tt_elem = etree.Element("{%s}tt" % ttml_ns, nsmap=ns_map)
        tt_elem.attrib["{%s}lang" % xml_ns] = language
        # TODO add metadata from parameters here?
        #head_elem = etree.SubElement(tt_elem, "{%s}head" % ttml_ns)
        body_elem = etree.SubElement(tt_elem, "{%s}body" % ttml_ns)
        div_elem = etree.SubElement(body_elem, "{%s}div" % ttml_ns)
        for fragment in self.fragments:
            text = fragment.text_fragment
            p_string = u"<p xml:id=\"%s\" begin=\"%s\" end=\"%s\">%s</p>" % (
                text.identifier,
                gf.time_to_ttml(fragment.begin),
                gf.time_to_ttml(fragment.end),
                u"<br/>".join(text.lines)
            )
            p_elem = etree.fromstring(p_string)
            div_elem.append(p_elem)

        # write tree
        self._write_tree_to_file(tt_elem, output_file)