-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlignmentNews.py
More file actions
75 lines (65 loc) · 2.51 KB
/
AlignmentNews.py
File metadata and controls
75 lines (65 loc) · 2.51 KB
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
import pdb
import time
import urllib
import Utility
import configparser
from deep_translator import GoogleTranslator
def translate(src_lang, tgt_lang, list_text):
list_transed_text = list()
googleTranslator = GoogleTranslator(source=tgt_lang, target=src_lang)
time_except = 360
for text in list_text:
while(True):
try:
rawtext = text
text = text.replace("?", "").replace(".", "?").replace(".", "")
list_ = googleTranslator.translate(text)
if (tgt_lang != "zh-CN"):
list_transed_text.append({src_lang: list_, tgt_lang: rawtext})
else:
list_transed_text.append({src_lang: list_, "zh": rawtext})
print(list_)
break
except Exception as e:
print(e)
if time_except == 0:
break
time_except = time_except - 1
time.sleep(2)
pass
if time_except == 0:
break
return list_transed_text
import requests
config = configparser.RawConfigParser()
config.read('Api.properties')
def sentencesAlignment(src_source, tgt_source, tgt_lang):
global config
payload = Utility.objectToJson({"type": config.get(tgt_lang, 'api.type'),
"source": src_source,
"target": tgt_source})
if tgt_lang == "km":
payload = Utility.objectToJson({"type": config.get(tgt_lang, 'api.type'),
"doc_source": src_source,
"doc_target": tgt_source})
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", config.get(tgt_lang,'api.url'), headers=headers, data=payload.encode("utf-8"))
print(response.text)
data = Utility.stringJsonToOject(response.text)
for x in data['data']:
print(x)
def sentencesSegmentation(src_source, tgt_lang):
global config
payload = Utility.objectToJson({"type":tgt_lang,
"source": src_source})
headers = { 'Content-type': 'application/json; charset=utf-8'}
try:
response = requests.request("POST", config.get('segmentation','api.url'), headers=headers, data=payload.encode("utf-8"), timeout=3)
data = Utility.stringJsonToOject(response.text)
response.close()
except:
return src_source
time.sleep(0.001)
return data['data']