diff options
author | Patrick Simianer <p@simianer.de> | 2014-03-16 17:48:48 +0100 |
---|---|---|
committer | Patrick Simianer <p@simianer.de> | 2014-03-16 17:48:48 +0100 |
commit | 62bd9a4bdcea606d6ff2031fa4b207ef20caac31 (patch) | |
tree | 5a97415cff8287398becc602a1ca16c937a43253 /corpus/xml-tok.py | |
parent | 7112976f89f0082f7af48829dd5deee61a3e6d16 (diff) | |
parent | 6c04595f968f7a9c047c5941113752a7c7280b45 (diff) |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'corpus/xml-tok.py')
-rwxr-xr-x | corpus/xml-tok.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/corpus/xml-tok.py b/corpus/xml-tok.py new file mode 100755 index 00000000..4357ced6 --- /dev/null +++ b/corpus/xml-tok.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python + +import os +import re +import subprocess +import sys + +# Tokenize XML files with tokenize-anything.sh +# in: <seg id="963"> The earnings on its 10-year bonds are 28.45%. </seg> +# out: <seg id="963"> The earnings on its 10 - year bonds are 28.45 % . </seg> + +def escape(s): + return s.replace('&', '&').replace('>', '>').replace('<', '<').replace('"', '"').replace('\'', ''') + +def unescape(s): + return s.replace('>', '>').replace('<', '<').replace('"', '"').replace(''', '\'').replace('&', '&') + +def main(): + tok = subprocess.Popen([os.path.join(os.path.dirname(__file__), 'tokenize-anything.sh'), '-u'], stdin=subprocess.PIPE, stdout=subprocess.PIPE) + while True: + line = sys.stdin.readline() + if not line: + break + line = line.strip() + pieces = [] + eol = len(line) + pos = 0 + while pos < eol: + next = line.find('<', pos) + if next == -1: + next = eol + tok.stdin.write('{}\n'.format(unescape(line[pos:next]))) + pieces.append(escape(tok.stdout.readline().strip())) + if next == eol: + break + pos = line.find('>', next + 1) + if pos == -1: + pos = eol + else: + pos += 1 + pieces.append(line[next:pos]) + sys.stdout.write('{}\n'.format(' '.join(pieces).strip())) + tok.stdin.close() + tok.wait() + +if __name__ == '__main__': + main() |