BackTranslation 0.3.1__tar.gz → 0.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/BackTranslation/__init__.py +1 -1
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/BackTranslation/languages.py +109 -109
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/BackTranslation/translated.py +24 -22
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/BackTranslation/translation.py +136 -116
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/BackTranslation/translation_Baidu.py +117 -113
- backtranslation-0.4.0/BackTranslation.egg-info/PKG-INFO +152 -0
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/BackTranslation.egg-info/SOURCES.txt +6 -1
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/BackTranslation.egg-info/top_level.txt +1 -0
- backtranslation-0.4.0/LICENSE +21 -0
- backtranslation-0.4.0/PKG-INFO +152 -0
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/README.md +120 -99
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/setup.cfg +4 -4
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/setup.py +29 -24
- backtranslation-0.4.0/tests/__init__.py +0 -0
- backtranslation-0.4.0/tests/test_translated.py +39 -0
- backtranslation-0.4.0/tests/test_translation.py +148 -0
- backtranslation-0.4.0/tests/test_translation_baidu.py +115 -0
- BackTranslation-0.3.1/BackTranslation.egg-info/PKG-INFO +0 -116
- BackTranslation-0.3.1/PKG-INFO +0 -116
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/BackTranslation.egg-info/dependency_links.txt +0 -0
- {BackTranslation-0.3.1 → backtranslation-0.4.0}/BackTranslation.egg-info/requires.txt +0 -0
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
from BackTranslation.translation import BackTranslation
|
|
1
|
+
from BackTranslation.translation import BackTranslation
|
|
2
2
|
from BackTranslation.translation_Baidu import BackTranslation_Baidu
|
|
@@ -1,109 +1,109 @@
|
|
|
1
|
-
LANG_CODES = {
|
|
2
|
-
'afrikaans': 'af',
|
|
3
|
-
'albanian': 'sq',
|
|
4
|
-
'amharic': 'am',
|
|
5
|
-
'arabic': 'ar',
|
|
6
|
-
'armenian': 'hy',
|
|
7
|
-
'azerbaijani': 'az',
|
|
8
|
-
'basque': 'eu',
|
|
9
|
-
'belarusian': 'be',
|
|
10
|
-
'bengali': 'bn',
|
|
11
|
-
'bosnian': 'bs',
|
|
12
|
-
'bulgarian': 'bg',
|
|
13
|
-
'catalan': 'ca',
|
|
14
|
-
'cebuano': 'ceb',
|
|
15
|
-
'chichewa': 'ny',
|
|
16
|
-
'chinese (simplified)': 'zh-cn',
|
|
17
|
-
'chinese (traditional)': 'zh-tw',
|
|
18
|
-
'corsican': 'co',
|
|
19
|
-
'croatian': 'hr',
|
|
20
|
-
'czech': 'cs',
|
|
21
|
-
'danish': 'da',
|
|
22
|
-
'dutch': 'nl',
|
|
23
|
-
'english': 'en',
|
|
24
|
-
'esperanto': 'eo',
|
|
25
|
-
'estonian': 'et',
|
|
26
|
-
'filipino': 'tl',
|
|
27
|
-
'finnish': 'fi',
|
|
28
|
-
'french': 'fr',
|
|
29
|
-
'frisian': 'fy',
|
|
30
|
-
'galician': 'gl',
|
|
31
|
-
'georgian': 'ka',
|
|
32
|
-
'german': 'de',
|
|
33
|
-
'greek': 'el',
|
|
34
|
-
'gujarati': 'gu',
|
|
35
|
-
'haitian creole': 'ht',
|
|
36
|
-
'hausa': 'ha',
|
|
37
|
-
'hawaiian': 'haw',
|
|
38
|
-
'hebrew': 'he',
|
|
39
|
-
'hindi': 'hi',
|
|
40
|
-
'hmong': 'hmn',
|
|
41
|
-
'hungarian': 'hu',
|
|
42
|
-
'icelandic': 'is',
|
|
43
|
-
'igbo': 'ig',
|
|
44
|
-
'indonesian': 'id',
|
|
45
|
-
'irish': 'ga',
|
|
46
|
-
'italian': 'it',
|
|
47
|
-
'japanese': 'ja',
|
|
48
|
-
'javanese': 'jw',
|
|
49
|
-
'kannada': 'kn',
|
|
50
|
-
'kazakh': 'kk',
|
|
51
|
-
'khmer': 'km',
|
|
52
|
-
'korean': 'ko',
|
|
53
|
-
'kurdish (kurmanji)': 'ku',
|
|
54
|
-
'kyrgyz': 'ky',
|
|
55
|
-
'lao': 'lo',
|
|
56
|
-
'latin': 'la',
|
|
57
|
-
'latvian': 'lv',
|
|
58
|
-
'lithuanian': 'lt',
|
|
59
|
-
'luxembourgish': 'lb',
|
|
60
|
-
'macedonian': 'mk',
|
|
61
|
-
'malagasy': 'mg',
|
|
62
|
-
'malay': 'ms',
|
|
63
|
-
'malayalam': 'ml',
|
|
64
|
-
'maltese': 'mt',
|
|
65
|
-
'maori': 'mi',
|
|
66
|
-
'marathi': 'mr',
|
|
67
|
-
'mongolian': 'mn',
|
|
68
|
-
'myanmar (burmese)': 'my',
|
|
69
|
-
'nepali': 'ne',
|
|
70
|
-
'norwegian': 'no',
|
|
71
|
-
'odia': 'or',
|
|
72
|
-
'pashto': 'ps',
|
|
73
|
-
'persian': 'fa',
|
|
74
|
-
'polish': 'pl',
|
|
75
|
-
'portuguese': 'pt',
|
|
76
|
-
'punjabi': 'pa',
|
|
77
|
-
'romanian': 'ro',
|
|
78
|
-
'russian': 'ru',
|
|
79
|
-
'samoan': 'sm',
|
|
80
|
-
'scots gaelic': 'gd',
|
|
81
|
-
'serbian': 'sr',
|
|
82
|
-
'sesotho': 'st',
|
|
83
|
-
'shona': 'sn',
|
|
84
|
-
'sindhi': 'sd',
|
|
85
|
-
'sinhala': 'si',
|
|
86
|
-
'slovak': 'sk',
|
|
87
|
-
'slovenian': 'sl',
|
|
88
|
-
'somali': 'so',
|
|
89
|
-
'spanish': 'es',
|
|
90
|
-
'sundanese': 'su',
|
|
91
|
-
'swahili': 'sw',
|
|
92
|
-
'swedish': 'sv',
|
|
93
|
-
'tajik': 'tg',
|
|
94
|
-
'tamil': 'ta',
|
|
95
|
-
'telugu': 'te',
|
|
96
|
-
'thai': 'th',
|
|
97
|
-
'turkish': 'tr',
|
|
98
|
-
'ukrainian': 'uk',
|
|
99
|
-
'urdu': 'ur',
|
|
100
|
-
'uyghur': 'ug',
|
|
101
|
-
'uzbek': 'uz',
|
|
102
|
-
'vietnamese': 'vi',
|
|
103
|
-
'welsh': 'cy',
|
|
104
|
-
'xhosa': 'xh',
|
|
105
|
-
'yiddish': 'yi',
|
|
106
|
-
'yoruba': 'yo',
|
|
107
|
-
'zulu': 'zu'}
|
|
108
|
-
|
|
109
|
-
LANGUAGES = {value:key for key, value in LANG_CODES.items()}
|
|
1
|
+
LANG_CODES = {
|
|
2
|
+
'afrikaans': 'af',
|
|
3
|
+
'albanian': 'sq',
|
|
4
|
+
'amharic': 'am',
|
|
5
|
+
'arabic': 'ar',
|
|
6
|
+
'armenian': 'hy',
|
|
7
|
+
'azerbaijani': 'az',
|
|
8
|
+
'basque': 'eu',
|
|
9
|
+
'belarusian': 'be',
|
|
10
|
+
'bengali': 'bn',
|
|
11
|
+
'bosnian': 'bs',
|
|
12
|
+
'bulgarian': 'bg',
|
|
13
|
+
'catalan': 'ca',
|
|
14
|
+
'cebuano': 'ceb',
|
|
15
|
+
'chichewa': 'ny',
|
|
16
|
+
'chinese (simplified)': 'zh-cn',
|
|
17
|
+
'chinese (traditional)': 'zh-tw',
|
|
18
|
+
'corsican': 'co',
|
|
19
|
+
'croatian': 'hr',
|
|
20
|
+
'czech': 'cs',
|
|
21
|
+
'danish': 'da',
|
|
22
|
+
'dutch': 'nl',
|
|
23
|
+
'english': 'en',
|
|
24
|
+
'esperanto': 'eo',
|
|
25
|
+
'estonian': 'et',
|
|
26
|
+
'filipino': 'tl',
|
|
27
|
+
'finnish': 'fi',
|
|
28
|
+
'french': 'fr',
|
|
29
|
+
'frisian': 'fy',
|
|
30
|
+
'galician': 'gl',
|
|
31
|
+
'georgian': 'ka',
|
|
32
|
+
'german': 'de',
|
|
33
|
+
'greek': 'el',
|
|
34
|
+
'gujarati': 'gu',
|
|
35
|
+
'haitian creole': 'ht',
|
|
36
|
+
'hausa': 'ha',
|
|
37
|
+
'hawaiian': 'haw',
|
|
38
|
+
'hebrew': 'he',
|
|
39
|
+
'hindi': 'hi',
|
|
40
|
+
'hmong': 'hmn',
|
|
41
|
+
'hungarian': 'hu',
|
|
42
|
+
'icelandic': 'is',
|
|
43
|
+
'igbo': 'ig',
|
|
44
|
+
'indonesian': 'id',
|
|
45
|
+
'irish': 'ga',
|
|
46
|
+
'italian': 'it',
|
|
47
|
+
'japanese': 'ja',
|
|
48
|
+
'javanese': 'jw',
|
|
49
|
+
'kannada': 'kn',
|
|
50
|
+
'kazakh': 'kk',
|
|
51
|
+
'khmer': 'km',
|
|
52
|
+
'korean': 'ko',
|
|
53
|
+
'kurdish (kurmanji)': 'ku',
|
|
54
|
+
'kyrgyz': 'ky',
|
|
55
|
+
'lao': 'lo',
|
|
56
|
+
'latin': 'la',
|
|
57
|
+
'latvian': 'lv',
|
|
58
|
+
'lithuanian': 'lt',
|
|
59
|
+
'luxembourgish': 'lb',
|
|
60
|
+
'macedonian': 'mk',
|
|
61
|
+
'malagasy': 'mg',
|
|
62
|
+
'malay': 'ms',
|
|
63
|
+
'malayalam': 'ml',
|
|
64
|
+
'maltese': 'mt',
|
|
65
|
+
'maori': 'mi',
|
|
66
|
+
'marathi': 'mr',
|
|
67
|
+
'mongolian': 'mn',
|
|
68
|
+
'myanmar (burmese)': 'my',
|
|
69
|
+
'nepali': 'ne',
|
|
70
|
+
'norwegian': 'no',
|
|
71
|
+
'odia': 'or',
|
|
72
|
+
'pashto': 'ps',
|
|
73
|
+
'persian': 'fa',
|
|
74
|
+
'polish': 'pl',
|
|
75
|
+
'portuguese': 'pt',
|
|
76
|
+
'punjabi': 'pa',
|
|
77
|
+
'romanian': 'ro',
|
|
78
|
+
'russian': 'ru',
|
|
79
|
+
'samoan': 'sm',
|
|
80
|
+
'scots gaelic': 'gd',
|
|
81
|
+
'serbian': 'sr',
|
|
82
|
+
'sesotho': 'st',
|
|
83
|
+
'shona': 'sn',
|
|
84
|
+
'sindhi': 'sd',
|
|
85
|
+
'sinhala': 'si',
|
|
86
|
+
'slovak': 'sk',
|
|
87
|
+
'slovenian': 'sl',
|
|
88
|
+
'somali': 'so',
|
|
89
|
+
'spanish': 'es',
|
|
90
|
+
'sundanese': 'su',
|
|
91
|
+
'swahili': 'sw',
|
|
92
|
+
'swedish': 'sv',
|
|
93
|
+
'tajik': 'tg',
|
|
94
|
+
'tamil': 'ta',
|
|
95
|
+
'telugu': 'te',
|
|
96
|
+
'thai': 'th',
|
|
97
|
+
'turkish': 'tr',
|
|
98
|
+
'ukrainian': 'uk',
|
|
99
|
+
'urdu': 'ur',
|
|
100
|
+
'uyghur': 'ug',
|
|
101
|
+
'uzbek': 'uz',
|
|
102
|
+
'vietnamese': 'vi',
|
|
103
|
+
'welsh': 'cy',
|
|
104
|
+
'xhosa': 'xh',
|
|
105
|
+
'yiddish': 'yi',
|
|
106
|
+
'yoruba': 'yo',
|
|
107
|
+
'zulu': 'zu'}
|
|
108
|
+
|
|
109
|
+
LANGUAGES = {value:key for key, value in LANG_CODES.items()}
|
|
@@ -1,22 +1,24 @@
|
|
|
1
|
-
class Translated(object):
|
|
2
|
-
"""
|
|
3
|
-
Save the translated result
|
|
4
|
-
"""
|
|
5
|
-
def __init__(self, src_lang, tmp_lang, text, trans_text, back_text):
|
|
6
|
-
self.source_text = text
|
|
7
|
-
self.src = src_lang
|
|
8
|
-
self.tmp = tmp_lang
|
|
9
|
-
self.tran_text = trans_text
|
|
10
|
-
self.result_text = back_text
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def __str__(self):
|
|
14
|
-
return self.__unicode__()
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def __unicode__(self):
|
|
19
|
-
if len(self.result_text) > 30
|
|
20
|
-
show_text = ' '.join(self.result_text.split(' ')[:30])
|
|
21
|
-
show_text += '...'
|
|
22
|
-
|
|
1
|
+
class Translated(object):
|
|
2
|
+
"""
|
|
3
|
+
Save the translated result
|
|
4
|
+
"""
|
|
5
|
+
def __init__(self, src_lang, tmp_lang, text, trans_text, back_text):
|
|
6
|
+
self.source_text = text
|
|
7
|
+
self.src = src_lang
|
|
8
|
+
self.tmp = tmp_lang
|
|
9
|
+
self.tran_text = trans_text
|
|
10
|
+
self.result_text = back_text
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def __str__(self):
|
|
14
|
+
return self.__unicode__()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def __unicode__(self):
|
|
19
|
+
if len(self.result_text.split(' ')) > 30:
|
|
20
|
+
show_text = ' '.join(self.result_text.split(' ')[:30])
|
|
21
|
+
show_text += '...'
|
|
22
|
+
else:
|
|
23
|
+
show_text = self.result_text
|
|
24
|
+
return u'Translated(src={src}, tmp={tmp}, result_text={text})'.format(src=self.src, tmp=self.tmp, text=show_text)
|
|
@@ -1,116 +1,136 @@
|
|
|
1
|
-
from googletrans import Translator
|
|
2
|
-
import nltk
|
|
3
|
-
from nltk.tokenize import sent_tokenize
|
|
4
|
-
import time
|
|
5
|
-
from BackTranslation.languages import LANGUAGES, LANG_CODES
|
|
6
|
-
from BackTranslation.translated import Translated
|
|
7
|
-
import typing
|
|
8
|
-
import httpcore
|
|
9
|
-
|
|
10
|
-
try:
|
|
11
|
-
nltk.data.find('tokenizers/punkt')
|
|
12
|
-
except LookupError:
|
|
13
|
-
nltk.download('punkt')
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
class BackTranslation(object):
|
|
17
|
-
"""
|
|
18
|
-
Translate the words to other language and translate back
|
|
19
|
-
in order to generate more samples for NLP.
|
|
20
|
-
|
|
21
|
-
This package built based on googletrans package.
|
|
22
|
-
Thus, you have to create only one instance to translate your words.
|
|
23
|
-
Otherwise, an error will occur from googletrans.
|
|
24
|
-
|
|
25
|
-
"""
|
|
26
|
-
|
|
27
|
-
def __init__(self, url=['translate.google.com'], proxies: typing.Dict[str, httpcore.SyncHTTPTransport] = None):
|
|
28
|
-
self.translator = Translator(service_urls=url, proxies=proxies)
|
|
29
|
-
self.Languages = LANGUAGES
|
|
30
|
-
self.langCodes = LANG_CODES
|
|
31
|
-
self.MAX_LENGTH = 5000
|
|
32
|
-
|
|
33
|
-
def translate(self, text, src=None, tmp=None, sleeping=0):
|
|
34
|
-
|
|
35
|
-
if not src:
|
|
36
|
-
src = self.translator.detect(text).lang
|
|
37
|
-
|
|
38
|
-
if src.lower() not in self.Languages:
|
|
39
|
-
raise ValueError("'{}': INVALID source language.".format(src))
|
|
40
|
-
|
|
41
|
-
# if tmp is null, set a default language for tmp.
|
|
42
|
-
if not tmp:
|
|
43
|
-
if src == 'en':
|
|
44
|
-
tmp = 'zh-cn'
|
|
45
|
-
else:
|
|
46
|
-
tmp = 'en'
|
|
47
|
-
|
|
48
|
-
if tmp not in self.Languages:
|
|
49
|
-
raise ValueError("'{}': INVALID transited language.".format(tmp))
|
|
50
|
-
|
|
51
|
-
if src == tmp:
|
|
52
|
-
raise ValueError("Transited language ({tmp}) should different from
|
|
53
|
-
tmp=
|
|
54
|
-
|
|
55
|
-
# check the length of text
|
|
56
|
-
if len(text) > self.MAX_LENGTH:
|
|
57
|
-
original_sentences = self._split_segement(sent_tokenize(text))
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
:
|
|
99
|
-
:
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
1
|
+
from googletrans import Translator
|
|
2
|
+
import nltk
|
|
3
|
+
from nltk.tokenize import sent_tokenize
|
|
4
|
+
import time
|
|
5
|
+
from BackTranslation.languages import LANGUAGES, LANG_CODES
|
|
6
|
+
from BackTranslation.translated import Translated
|
|
7
|
+
import typing
|
|
8
|
+
import httpcore
|
|
9
|
+
|
|
10
|
+
try:
|
|
11
|
+
nltk.data.find('tokenizers/punkt')
|
|
12
|
+
except LookupError:
|
|
13
|
+
nltk.download('punkt')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class BackTranslation(object):
|
|
17
|
+
"""
|
|
18
|
+
Translate the words to other language and translate back
|
|
19
|
+
in order to generate more samples for NLP.
|
|
20
|
+
|
|
21
|
+
This package built based on googletrans package.
|
|
22
|
+
Thus, you have to create only one instance to translate your words.
|
|
23
|
+
Otherwise, an error will occur from googletrans.
|
|
24
|
+
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __init__(self, url=['translate.google.com'], proxies: typing.Dict[str, httpcore.SyncHTTPTransport] = None):
|
|
28
|
+
self.translator = Translator(service_urls=url, proxies=proxies)
|
|
29
|
+
self.Languages = LANGUAGES
|
|
30
|
+
self.langCodes = LANG_CODES
|
|
31
|
+
self.MAX_LENGTH = 5000
|
|
32
|
+
|
|
33
|
+
def translate(self, text, src=None, tmp=None, sleeping=0):
|
|
34
|
+
|
|
35
|
+
if not src:
|
|
36
|
+
src = self.translator.detect(text).lang
|
|
37
|
+
|
|
38
|
+
if src.lower() not in self.Languages:
|
|
39
|
+
raise ValueError("'{}': INVALID source language.".format(src))
|
|
40
|
+
|
|
41
|
+
# if tmp is null, set a default language for tmp.
|
|
42
|
+
if not tmp:
|
|
43
|
+
if src == 'en':
|
|
44
|
+
tmp = 'zh-cn'
|
|
45
|
+
else:
|
|
46
|
+
tmp = 'en'
|
|
47
|
+
|
|
48
|
+
if tmp not in self.Languages:
|
|
49
|
+
raise ValueError("'{}': INVALID transited language.".format(tmp))
|
|
50
|
+
|
|
51
|
+
if src == tmp:
|
|
52
|
+
raise ValueError("Transited language ({tmp}) should different from source language ({src}).".format(
|
|
53
|
+
tmp=tmp, src=src))
|
|
54
|
+
|
|
55
|
+
# check the length of text
|
|
56
|
+
if len(text) > self.MAX_LENGTH:
|
|
57
|
+
original_sentences = self._split_segement(sent_tokenize(text))
|
|
58
|
+
|
|
59
|
+
try:
|
|
60
|
+
t_text = self.translator.translate(original_sentences, src=src, dest=tmp)
|
|
61
|
+
except httpcore.ConnectTimeout:
|
|
62
|
+
raise httpcore.ConnectTimeout("Connection timed out. If you are blocked by Google, try using the 'proxies' parameter or a different 'url'.")
|
|
63
|
+
except Exception as e:
|
|
64
|
+
raise Exception("Translation failed (try increasing the 'sleeping' parameter): {}".format(e))
|
|
65
|
+
tran_text = ' '.join([t.text for t in t_text])
|
|
66
|
+
time.sleep(sleeping)
|
|
67
|
+
try:
|
|
68
|
+
r_text = self.translator.translate([t.text for t in t_text], src=tmp, dest=src)
|
|
69
|
+
except httpcore.ConnectTimeout:
|
|
70
|
+
raise httpcore.ConnectTimeout("Connection timed out. If you are blocked by Google, try using the 'proxies' parameter or a different 'url'.")
|
|
71
|
+
except Exception as e:
|
|
72
|
+
raise Exception("Back-translation failed (try increasing the 'sleeping' parameter): {}".format(e))
|
|
73
|
+
back_text = ' '.join([r.text for r in r_text])
|
|
74
|
+
back_text.rstrip()
|
|
75
|
+
else:
|
|
76
|
+
try:
|
|
77
|
+
mid_text = self.translator.translate(text, src=src, dest=tmp)
|
|
78
|
+
except httpcore.ConnectTimeout:
|
|
79
|
+
raise httpcore.ConnectTimeout("Connection timed out. If you are blocked by Google, try using the 'proxies' parameter or a different 'url'.")
|
|
80
|
+
except Exception as e:
|
|
81
|
+
raise Exception("Translation failed (try increasing the 'sleeping' parameter): {}".format(e))
|
|
82
|
+
tran_text = mid_text.text
|
|
83
|
+
time.sleep(sleeping) # Sleep between translation
|
|
84
|
+
try:
|
|
85
|
+
result_text = self.translator.translate(tran_text, src=tmp, dest=src)
|
|
86
|
+
except httpcore.ConnectTimeout:
|
|
87
|
+
raise httpcore.ConnectTimeout("Connection timed out. If you are blocked by Google, try using the 'proxies' parameter or a different 'url'.")
|
|
88
|
+
except Exception as e:
|
|
89
|
+
raise Exception("Back-translation failed (try increasing the 'sleeping' parameter): {}".format(e))
|
|
90
|
+
back_text = result_text.text
|
|
91
|
+
result = Translated(src_lang=src, tmp_lang=tmp, text=text, trans_text=tran_text, back_text=back_text)
|
|
92
|
+
return result
|
|
93
|
+
|
|
94
|
+
def _split_segement(self, sentences):
|
|
95
|
+
"""
|
|
96
|
+
Split the long sentences into multiple sentences whose lengths are less than MAX_LENGTH.
|
|
97
|
+
|
|
98
|
+
:param sentences: the list of tokenized sentences from source text
|
|
99
|
+
:return: the list of sentences with proper length
|
|
100
|
+
:rtype: list
|
|
101
|
+
"""
|
|
102
|
+
sentences_list = []
|
|
103
|
+
block = ""
|
|
104
|
+
for sentence in sentences:
|
|
105
|
+
if len((block.rstrip() + ' ' + sentence).encode('utf-8')) > self.MAX_LENGTH:
|
|
106
|
+
sentences_list.append(block.rstrip())
|
|
107
|
+
block = sentence
|
|
108
|
+
else:
|
|
109
|
+
block = block + sentence + ' '
|
|
110
|
+
sentences_list.append(block.rstrip())
|
|
111
|
+
return sentences_list
|
|
112
|
+
|
|
113
|
+
def searchLanguage(self, language):
|
|
114
|
+
"""
|
|
115
|
+
Search a matched language code by language name.
|
|
116
|
+
|
|
117
|
+
:param language: the full name of language in english
|
|
118
|
+
:return: return matched language name and their code
|
|
119
|
+
:rtype: dict
|
|
120
|
+
|
|
121
|
+
Usage:
|
|
122
|
+
>>> trans = BackTranslation()
|
|
123
|
+
>>> trans.searchLanguage('frenc')
|
|
124
|
+
# {'french': 'fr'}
|
|
125
|
+
>>> trans.searchLanguage('Chinese')
|
|
126
|
+
# {'chinese (simplified)': 'zh-cn', 'chinese (traditional)': 'zh-tw'}
|
|
127
|
+
"""
|
|
128
|
+
language = language.lower()
|
|
129
|
+
if language in self.langCodes:
|
|
130
|
+
return {language: self.langCodes[language]}
|
|
131
|
+
else:
|
|
132
|
+
language_list = [key for key in self.langCodes.keys() if language in key]
|
|
133
|
+
if language_list != []:
|
|
134
|
+
return {lan: self.langCodes[lan] for lan in language_list}
|
|
135
|
+
else:
|
|
136
|
+
raise ValueError("{}: No existing language.".format(language))
|