def _output_tsv(self, output_file): """ Output to TSV """ for fragment in self.fragments: text = fragment.text_fragment output_file.write("%s\t%s\t%s\n" % ( gf.time_to_ssmmm(fragment.begin), gf.time_to_ssmmm(fragment.end), text.identifier ))
def format(self, syncmap): msg = [] msg.append(u"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>") msg.append(u"<map>") for fragment in syncmap.fragments: msg.append(u" <fragment>") msg.append(u" <identifier>%s</identifier>" % fragment.text_fragment.identifier) msg.append(u" <start>%s</start>" % gf.time_to_ssmmm(fragment.begin)) msg.append(u" <end>%s</end>" % gf.time_to_ssmmm(fragment.end)) msg.append(u" </fragment>") msg.append(u"</map>") return u"\n".join(msg)
def _output_txt(self, output_file): """ Output to TXT """ for fragment in self.fragments: text = fragment.text_fragment output_file.write("%s %s %s \"%s\"\n" % ( text.identifier, gf.time_to_ssmmm(fragment.begin), gf.time_to_ssmmm(fragment.end), text.text ))
def visit_children(node, parent_elem): """ Recursively visit the fragments_tree """ for child in node.children_not_empty: fragment = child.value fragment_elem = etree.SubElement(parent_elem, "fragment") fragment_elem.attrib["id"] = fragment.text_fragment.identifier fragment_elem.attrib["begin"] = gf.time_to_ssmmm(fragment.begin) fragment_elem.attrib["end"] = gf.time_to_ssmmm(fragment.end) for line in fragment.text_fragment.lines: line_elem = etree.SubElement(fragment_elem, "line") line_elem.text = line children_elem = etree.SubElement(fragment_elem, "children") visit_children(child, children_elem)
def _write_xml_legacy(self, output_file): """ Write to XML file (legacy format) """ output_file.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n") output_file.write("<map>\n") for fragment in self.fragments: text = fragment.text_fragment output_file.write(" <fragment>\n") output_file.write(" <identifier>%s</identifier>\n" % text.identifier) output_file.write(" <start>%s</start>\n" % gf.time_to_ssmmm(fragment.begin)) output_file.write(" <end>%s</end>\n" % gf.time_to_ssmmm(fragment.end)) output_file.write(" </fragment>\n") output_file.write("</map>")
def visit_children(node): """ Recursively visit the fragments_tree """ output_fragments = [] for child in node.children_not_empty: fragment = child.value text = fragment.text_fragment output_fragments.append({ "id": text.identifier, "language": text.language, "lines": text.lines, "begin": gf.time_to_ssmmm(fragment.begin), "end": gf.time_to_ssmmm(fragment.end), "children": visit_children(child) }) return output_fragments
def _write_xml(self, output_file): """ Write to XML file """ map_elem = etree.Element("map") for fragment in self.fragments: text = fragment.text_fragment fragment_elem = etree.SubElement(map_elem, "fragment") fragment_elem.attrib["id"] = text.identifier fragment_elem.attrib["begin"] = gf.time_to_ssmmm(fragment.begin) fragment_elem.attrib["end"] = gf.time_to_ssmmm(fragment.end) for line in text.lines: line_elem = etree.SubElement(fragment_elem, "line") line_elem.text = line self._write_tree_to_file(map_elem, output_file)
def _write_json(self, output_file): """ Write to JSON file """ output_fragments = [] for fragment in self.fragments: text = fragment.text_fragment output_fragment = {} output_fragment["id"] = text.identifier output_fragment["language"] = text.language output_fragment["lines"] = text.lines output_fragment["begin"] = gf.time_to_ssmmm(fragment.begin) output_fragment["end"] = gf.time_to_ssmmm(fragment.end) output_fragments.append(output_fragment) output_dict = {"fragments": output_fragments} output_file.write(json.dumps(output_dict, indent=1, sort_keys=True))
def test_time_to_ssmm(self): tests = [ [None, "0.000"], [0, "0.000"], [1, "1.000"], [1.234, "1.234"], ] for test in tests: self.assertEqual(gf.time_to_ssmmm(test[0]), test[1])
def test_time_to_ssmm(self): tests = [ (None, "0.000"), (0, "0.000"), (1, "1.000"), (1.234, "1.234"), ] for test in tests: self.assertEqual(gf.time_to_ssmmm(test[0]), test[1])
def format(self, syncmap): smil_data = [] smil_ids = [] for fragment in syncmap.fragments: text = fragment.text_fragment smil_data.append({ "id": text.identifier, "begin": gf.time_to_ssmmm(fragment.begin), "end": gf.time_to_ssmmm(fragment.end) }) smil_ids.append(text.identifier) return gf.safe_unicode( json.dumps(obj={ "smil_ids": smil_ids, "smil_data": smil_data }, indent=1, sort_keys=True))
def _write_rbse(self, output_file): """ Write to RBSE file """ output_dict = {} smil_data = [] smil_ids = [] for fragment in self.fragments: text = fragment.text_fragment output_fragment = {} output_fragment["id"] = text.identifier output_fragment["begin"] = gf.time_to_ssmmm(fragment.begin) output_fragment["end"] = gf.time_to_ssmmm(fragment.end) smil_ids.append(text.identifier) smil_data.append(output_fragment) output_dict = { "smil_ids": smil_ids, "smil_data": smil_data } output_file.write(json.dumps(output_dict, indent=1, sort_keys=True))
def format(self, syncmap): smil_data = [] smil_ids = [] for fragment in syncmap.fragments: text = fragment.text_fragment smil_data.append({ "id": text.identifier, "begin": gf.time_to_ssmmm(fragment.begin), "end": gf.time_to_ssmmm(fragment.end) }) smil_ids.append(text.identifier) return gf.safe_unicode( json.dumps( obj={ "smil_ids": smil_ids, "smil_data": smil_data }, indent=1, sort_keys=True ) )
def _output_json(self, output_file): """ Output to JSON """ output_file.write("{\n") output_file.write(" \"smil_ids\": [\n") string = ",\n".join([" \"%s\"" % ( f.text_fragment.identifier ) for f in self.fragments]) output_file.write(string) output_file.write("\n") output_file.write(" ],\n") output_file.write(" \"smil_data\": [\n") string = ",\n".join([" { \"id\": \"%s\", \"begin\": %s, \"end\": %s }" % ( f.text_fragment.identifier, gf.time_to_ssmmm(f.begin), gf.time_to_ssmmm(f.end), ) for f in self.fragments]) output_file.write(string) output_file.write("\n") output_file.write(" ]\n") output_file.write("}\n")
def _output_ttml(self, output_file, parameters=None): """ Output to TTML """ output_file.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n") output_file.write("<tt xmlns=\"http://www.w3.org/ns/ttml\">\n") # TODO add metadata from parameters here? # output_file.write(" <head/>\n") output_file.write(" <body>\n") output_file.write(" <div>\n") for fragment in self.fragments: text = fragment.text_fragment output_file.write(" <p xml:id=\"%s\" begin=\"%s\" end=\"%s\">\n" % ( text.identifier, gf.time_to_ssmmm(fragment.begin), gf.time_to_ssmmm(fragment.end) )) output_file.write(" %s\n" % text.text) output_file.write(" </p>\n") output_file.write(" </div>\n") output_file.write(" </body>\n") output_file.write("</tt>")