-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeptrans
executable file
·52 lines (42 loc) · 1.68 KB
/
deptrans
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
#!/usr/bin/python3
import sys, subprocess, re, urllib3, json, argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--pair',
help='Language pair, separate by bar (eg. `es|ca`)')
parser.add_argument('-s', '--server',
help="Translation server URL",
default="http://localhost:2737/translate")
parser.add_argument('-m', '--mode',
help='add: replace lemma; append: replace form',
default='append')
args = parser.parse_args()
out = ""
http = urllib3.PoolManager()
for line in sys.stdin:
if line[0] == "#":
out += line
continue
if line == "\n":
out += "\n"
continue
line = line.rstrip("\n").split("\t")
num = line[0]
word = line[1]
word = re.sub(r'"', r'\\"', word)
if word[0].isalnum():
r = http.request('POST', args.server, fields={'langpair': args.pair, 'q': word})
s = json.loads(r.data.decode('utf-8'))['responseData']['translatedText']
else:
s = word
s = re.sub(r'@', '', s)
if args.mode == "append":
out += "{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format(num,
s.lstrip("*#").rstrip("#|").lower(), *line[2:])
elif args.mode == "add":
out += "{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\n".format(num,
word, s.lstrip("*#").rstrip("#|").lower(), *line[3:])
else:
sys.stdout.write("Error: invalid mode!\n")
break
sys.stdout.write(out)