-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
32 lines (22 loc) · 947 Bytes
/
translate.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
import json
from tqdm import tqdm
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
def translate_with_qmark(q, target):
q = q if q[-1] == "?" else q + "?"
q_trans = translate_client.translate(q, target_language=target)["translatedText"]
return q_trans[:-1] if q_trans[-1] == "?" else q_trans
def translate_file(target):
with open(f"./cwq/dataset.json", "r") as f:
data = json.load(f)
for idx in tqdm(range(len(data))):
data[idx][f"questionWithBrackets_{target}"] = translate_with_qmark(data[idx][f"questionWithBrackets"], target)
data[idx][f"questionPatternModEntities_{target}"] = translate_with_qmark(data[idx][f"questionPatternModEntities"], target)
with open(f"./cwq/dataset.json", "w") as f:
json.dump(data, f)
def main():
translate_file("kn")
translate_file("he")
translate_file("zh")
if __name__ == "__main__":
main()