hhhwwwuuu / BackTranslation

back translation for NLP
https://pypi.org/project/BackTranslation/
MIT License
25 stars 2 forks source link

Wish #4

Open jhkd-kevin opened 3 years ago

jhkd-kevin commented 3 years ago

Can you provide a complete example such as translation: "Anh ấy đã chữa khỏi cảm cúm bằng aspirin. "

hhhwwwuuu commented 3 years ago
from BackTranslation import BackTranslation

trans = BackTranslation()
# set Korean as middle language. and auto detect the source language
result = trans.translate('Anh ấy đã chữa khỏi cảm cúm bằng aspirin.', tmp='ko')

# source text
print(result.source_text)
# Anh ấy đã chữa khỏi cảm cúm bằng aspirin.

# middle text
print(result.tran_text)
# 그는 아스피린으로 독감을 잡았습니다.

# backtranslated text
print(result.result_text)
# Ông bị cúm với aspirin.

Hope it will help you

jhkd-kevin commented 3 years ago

I can use google translator And I actually follow your step: This is my complete code:

from BackTranslation import BackTranslation trans = BackTranslation(url=[ 'translate.google.com', 'translate.google.co.kr', ]) result = trans.translate('Anh ấy đã chữa khỏi cảm cúm bằng aspirin.', tmp='ko')

print(result.source_text) print(result.tran_text) print(result.result_text)

However, As I run it, it call back

ConnectError: [Errno 0] Error

hhhwwwuuu commented 3 years ago

@jhkd-kevin Is there any more error info?

jhkd-kevin commented 3 years ago

This is full error info, thanks for your help: ConnectError Traceback (most recent call last)

in 4 'translate.google.co.kr', 5 ]) ----> 6 result = trans.translate('Anh ấy đã chữa khỏi cảm cúm bằng aspirin.', tmp='ko') 7 # source text 8 print(result.source_text) C:\Users\anaconda\lib\site-packages\BackTranslation\translation.py in translate(self, text, src, tmp, sleeping) 33 34 if not src: ---> 35 src = self.translator.detect(text).lang 36 37 if src.lower() not in self.Languages: C:\Users\anaconda\lib\site-packages\googletrans\client.py in detect(self, text) 367 368 def detect(self, text: str): --> 369 translated = self.translate(text, src='auto', dest='en') 370 result = Detected(lang=translated.src, confidence=translated.extra_data.get('confidence', None), response=translated._response) 371 return result C:\Users\anaconda\lib\site-packages\googletrans\client.py in translate(self, text, dest, src) 192 193 origin = text --> 194 data, response = self._translate(text, dest, src) 195 196 token_found = False C:\Users\anaconda\lib\site-packages\googletrans\client.py in _translate(self, text, dest, src) 118 'rt': 'c', 119 } --> 120 r = self.client.post(url, params=params, data=data) 121 122 if r.status_code != 200 and self.raise_Exception: C:\Users\anaconda\lib\site-packages\httpx\_client.py in post(self, url, data, files, json, params, headers, cookies, auth, allow_redirects, timeout) 833 auth=auth, 834 allow_redirects=allow_redirects, --> 835 timeout=timeout, 836 ) 837 C:\Users\anaconda\lib\site-packages\httpx\_client.py in request(self, method, url, data, files, json, params, headers, cookies, auth, allow_redirects, timeout) 599 ) 600 return self.send( --> 601 request, auth=auth, allow_redirects=allow_redirects, timeout=timeout, 602 ) 603 C:\Users\anaconda\lib\site-packages\httpx\_client.py in send(self, request, stream, auth, allow_redirects, timeout) 619 620 response = self.send_handling_redirects( --> 621 request, auth=auth, timeout=timeout, allow_redirects=allow_redirects, 622 ) 623 C:\Users\anaconda\lib\site-packages\httpx\_client.py in send_handling_redirects(self, request, auth, timeout, allow_redirects, history) 646 647 response = self.send_handling_auth( --> 648 request, auth=auth, timeout=timeout, history=history 649 ) 650 response.history = list(history) C:\Users\anaconda\lib\site-packages\httpx\_client.py in send_handling_auth(self, request, history, auth, timeout) 682 request = next(auth_flow) 683 while True: --> 684 response = self.send_single_request(request, timeout) 685 if auth.requires_response_body: 686 response.read() C:\Users\anaconda\lib\site-packages\httpx\_client.py in send_single_request(self, request, timeout) 717 headers=request.headers.raw, 718 stream=request.stream, --> 719 timeout=timeout.as_dict(), 720 ) 721 except HTTPError as exc: C:\Users\anaconda\lib\site-packages\httpcore\_sync\http_proxy.py in request(self, method, url, headers, stream, timeout) 109 ) 110 return self._tunnel_request( --> 111 method, url, headers=headers, stream=stream, timeout=timeout 112 ) 113 C:\Users\anaconda\lib\site-packages\httpcore\_sync\http_proxy.py in _tunnel_request(self, method, url, headers, stream, timeout) 190 connect_headers = merge_headers(connect_headers, self.proxy_headers) 191 proxy_response = proxy_connection.request( --> 192 b"CONNECT", connect_url, headers=connect_headers, timeout=timeout 193 ) 194 proxy_status_code = proxy_response[1] C:\Users\anaconda\lib\site-packages\httpcore\_sync\connection.py in request(self, method, url, headers, stream, timeout) 63 "open_socket origin=%r timeout=%r", self.origin, timeout 64 ) ---> 65 self.socket = self._open_socket(timeout) 66 self._create_connection(self.socket) 67 elif self.state in (ConnectionState.READY, ConnectionState.IDLE): C:\Users\anaconda\lib\site-packages\httpcore\_sync\connection.py in _open_socket(self, timeout) 84 try: 85 return self.backend.open_tcp_stream( ---> 86 hostname, port, ssl_context, timeout 87 ) 88 except Exception: C:\Users\anaconda\lib\site-packages\httpcore\_backends\sync.py in open_tcp_stream(self, hostname, port, ssl_context, timeout) 137 sock, server_hostname=hostname.decode("ascii") 138 ) --> 139 return SyncSocketStream(sock=sock) 140 141 def create_lock(self) -> SyncLock: C:\Users\anaconda\lib\contextlib.py in __exit__(self, type, value, traceback) 128 value = type() 129 try: --> 130 self.gen.throw(type, value, traceback) 131 except StopIteration as exc: 132 # Suppress StopIteration *unless* it's the same exception that C:\Users\anaconda\lib\site-packages\httpcore\_exceptions.py in map_exceptions(map) 10 for from_exc, to_exc in map.items(): 11 if isinstance(exc, from_exc): ---> 12 raise to_exc(exc) from None 13 raise 14 ConnectError: [Errno 0] Error
hhhwwwuuu commented 3 years ago

@jhkd-kevin Sorry for the late reply.

I applied your code in my env. It works fine. I am not sure where the error from. But, there are two possible reasons. plz try the following solutions.

  1. dependent library Whether the googletrans library in Python is 4.0.0rc1? If not, plz update to this version.
  2. default URL in BackTranslation you may remove the parameter URL when you create a object. or replace default one to your used.
from BackTranslation import BackTranslation
trans = BackTranslation()
result = trans.translate('Anh ấy đã chữa khỏi cảm cúm bằng aspirin.', tmp='ko')
from BackTranslation import BackTranslation
trans = BackTranslation(url=['url that you used from browser']) # plz replace it 

result = trans.translate('Anh ấy đã chữa khỏi cảm cúm bằng aspirin.', tmp='ko')

please let me know whether this is work. Thanks