-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslator.py
40 lines (30 loc) · 964 Bytes
/
translator.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
import requests
_API_URL = 'http://120.125.83.10:8090'
def google_translate(phrase, target = 'zh-TW'):
service_url = f'{_API_URL}/googleTranslate'
post_data = {
'phrase': phrase,
'target': target
}
response = requests.post(service_url, json = post_data)
translated = None
if response.status_code == 200:
translated = response.text
return translated
def translate(phrase, source, target):
service_url = f'{_API_URL}/pyTranslate'
post_data = {
'phrase': phrase,
'source': source,
'target': target
}
response = requests.post(service_url, json = post_data)
translated = None
if response.status_code == 200:
translated = response.text
return translated
if __name__ == '__main__':
# translated = google_translate('cup')
# translated = translate('cup', 'EN', 'zh-TW')
translated = translate('杯子', 'zh-TW', 'EN')
print(translated)