-
Notifications
You must be signed in to change notification settings - Fork 2
/
buildtmx.py
82 lines (62 loc) · 1.69 KB
/
buildtmx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Build tmx from tab separated file
Usage:
buildtmx.py LANG1 LANG2
"""
from docopt import docopt
from xml.sax.saxutils import escape
import sys
import datetime
tmx_header = """<?xml version="1.0"?>
<tmx version="1.4">
<header
adminlang="en"
srclang="{}"
o-tmf="PlainText"
creationtool="buildtmx"
creationtoolversion="4.0"
datatype="PlainText"
segtype="sentence"
creationdate="{}"
o-encoding="utf-8">
</header>
<body>
"""
tmx_footer =" </body>\n</tmx>\n"
tu_template=""" <tu>
<tuv xml:lang="{}">
<seg>{}</seg>
</tuv>
<tuv xml:lang="{}">
<seg>{}</seg>
</tuv>
</tu>
"""
def fix_tu(sentence):
output = []
parts = sentence.split("<entity>")
output.append(escape(parts[0]))
for i in parts[1:]:
subparts = i.split("</entity>")
output.append(escape(subparts[0])+"</hi>"+escape(subparts[1]))
return "<hi>".join(output)
def print_tu(lang1, lang2, s1, s2):
global tu_template
return tu_template.format(lang1, fix_tu(s1), lang2, fix_tu(s2))
def main(arguments):
global tmx_header, tmx_footer
input = sys.stdin
output = sys.stdout
lang1 = arguments["LANG1"]
lang2 = arguments["LANG2"]
creationdate = datetime.datetime.now().strftime("%Y%m%dT%H%M%S")
output.write(tmx_header.format("en", creationdate))
for i in input:
sentences = i.strip().split("\t")
output.write(print_tu(lang1, lang2, sentences[0], sentences[1]))
output.write(tmx_footer)
output.close()
def args_and_main():
arguments = docopt(__doc__, version='buildtmx 1.0')
main(arguments)
if __name__ == '__main__':
args_and_main()