Nidhal Baccouri commited on
Commit
5574589
·
unverified ·
2 Parent(s): 33c503f b8fcb92

Merge pull request #117 from nidhaloff/fix/file_translation

Browse files
deep_translator/__init__.py CHANGED
@@ -1,6 +1,5 @@
1
  """Top-level package for Deep Translator"""
2
 
3
- # TODO: Discussion: Do these need to be in __init__.py? Are they intended to be exportable?
4
  from .google_trans import GoogleTranslator
5
  from .pons import PonsTranslator
6
  from .linguee import LingueeTranslator
@@ -13,7 +12,6 @@ from .microsoft import MicrosoftTranslator
13
  from .papago import PapagoTranslator
14
  from .libre import LibreTranslator
15
 
16
- # TODO: Discussion: These should be declared in setup.cfg, setting them here is redundant
17
  __author__ = """Nidhal Baccouri"""
18
  __email__ = '[email protected]'
19
  __version__ = '1.5.0'
@@ -27,7 +25,8 @@ __all__ = [
27
  "MicrosoftTranslator",
28
  "QCRI",
29
  "DeepL",
30
- "LibreTranslator"
 
31
  "single_detection",
32
  "batch_detection"
33
- ]
 
1
  """Top-level package for Deep Translator"""
2
 
 
3
  from .google_trans import GoogleTranslator
4
  from .pons import PonsTranslator
5
  from .linguee import LingueeTranslator
 
12
  from .papago import PapagoTranslator
13
  from .libre import LibreTranslator
14
 
 
15
  __author__ = """Nidhal Baccouri"""
16
  __email__ = '[email protected]'
17
  __version__ = '1.5.0'
 
25
  "MicrosoftTranslator",
26
  "QCRI",
27
  "DeepL",
28
+ "LibreTranslator",
29
+ "PapagoTranslator",
30
  "single_detection",
31
  "batch_detection"
32
+ ]
deep_translator/google_trans.py CHANGED
@@ -152,7 +152,7 @@ class GoogleTranslator(BaseTranslator):
152
  @return: str
153
  """
154
  try:
155
- with open(path) as f:
156
  text = f.read().strip()
157
  return self.translate(text)
158
  except Exception as e:
 
152
  @return: str
153
  """
154
  try:
155
+ with open(path, 'r', encoding='utf-8') as f:
156
  text = f.read().strip()
157
  return self.translate(text)
158
  except Exception as e:
deep_translator/libre.py CHANGED
@@ -45,7 +45,7 @@ class LibreTranslator(BaseTranslator):
45
  @return: list or dict
46
  """
47
  return [*LibreTranslator._languages.keys()] if not as_dict else LibreTranslator._languages
48
-
49
  def _map_language_to_code(self, language, **kwargs):
50
  """
51
  map language to its corresponding code (abbreviation) if the language was passed by its full name by the user
@@ -57,7 +57,7 @@ class LibreTranslator(BaseTranslator):
57
  elif language in self._languages.values():
58
  return language
59
  raise LanguageNotSupportedException(language)
60
-
61
  def _is_language_supported(self, language, **kwargs):
62
  """
63
  check if the language is supported by the translator
@@ -68,7 +68,7 @@ class LibreTranslator(BaseTranslator):
68
  return True
69
  else:
70
  raise LanguageNotSupportedException(language)
71
-
72
  def translate(self, text, **kwargs):
73
  """
74
  function that uses microsoft translate to translate a text
@@ -87,7 +87,7 @@ class LibreTranslator(BaseTranslator):
87
  "format": 'text'
88
  }
89
  # Add API Key if required
90
- if self.api_key:
91
  params["api_key"] = self.api_key
92
  # Do the request and check the connection.
93
  try:
@@ -95,7 +95,7 @@ class LibreTranslator(BaseTranslator):
95
  except ConnectionError:
96
  raise ServerException(503)
97
  # If the answer is not success, raise server exception.
98
-
99
  if response.status_code == 403:
100
  raise AuthorizationException(self.api_key)
101
  elif response.status_code != 200:
@@ -116,7 +116,7 @@ class LibreTranslator(BaseTranslator):
116
  @return: str
117
  """
118
  try:
119
- with open(path) as f:
120
  text = f.read().strip()
121
  return self.translate(text)
122
  except Exception as e:
 
45
  @return: list or dict
46
  """
47
  return [*LibreTranslator._languages.keys()] if not as_dict else LibreTranslator._languages
48
+
49
  def _map_language_to_code(self, language, **kwargs):
50
  """
51
  map language to its corresponding code (abbreviation) if the language was passed by its full name by the user
 
57
  elif language in self._languages.values():
58
  return language
59
  raise LanguageNotSupportedException(language)
60
+
61
  def _is_language_supported(self, language, **kwargs):
62
  """
63
  check if the language is supported by the translator
 
68
  return True
69
  else:
70
  raise LanguageNotSupportedException(language)
71
+
72
  def translate(self, text, **kwargs):
73
  """
74
  function that uses microsoft translate to translate a text
 
87
  "format": 'text'
88
  }
89
  # Add API Key if required
90
+ if self.api_key:
91
  params["api_key"] = self.api_key
92
  # Do the request and check the connection.
93
  try:
 
95
  except ConnectionError:
96
  raise ServerException(503)
97
  # If the answer is not success, raise server exception.
98
+
99
  if response.status_code == 403:
100
  raise AuthorizationException(self.api_key)
101
  elif response.status_code != 200:
 
116
  @return: str
117
  """
118
  try:
119
+ with open(path, 'r', encoding='utf-8') as f:
120
  text = f.read().strip()
121
  return self.translate(text)
122
  except Exception as e:
deep_translator/microsoft.py CHANGED
@@ -131,7 +131,7 @@ class MicrosoftTranslator:
131
  @return: translated text
132
  """
133
  try:
134
- with open(path) as f:
135
  text = f.read().strip()
136
  return self.translate(text)
137
  except Exception as e:
 
131
  @return: translated text
132
  """
133
  try:
134
+ with open(path, 'r', encoding='utf-8') as f:
135
  text = f.read().strip()
136
  return self.translate(text)
137
  except Exception as e:
deep_translator/mymemory.py CHANGED
@@ -151,7 +151,7 @@ class MyMemoryTranslator(BaseTranslator):
151
  @return: str
152
  """
153
  try:
154
- with open(path) as f:
155
  text = f.read().strip()
156
 
157
  return self.translate(text=text)
 
151
  @return: str
152
  """
153
  try:
154
+ with open(path, 'r', encoding='utf-8') as f:
155
  text = f.read().strip()
156
 
157
  return self.translate(text=text)
deep_translator/papago.py CHANGED
@@ -105,7 +105,7 @@ class PapagoTranslator(object):
105
  @return: str
106
  """
107
  try:
108
- with open(path) as f:
109
  text = f.read().strip()
110
  return self.translate(text)
111
  except Exception as e:
 
105
  @return: str
106
  """
107
  try:
108
+ with open(path, 'r', encoding='utf-8') as f:
109
  text = f.read().strip()
110
  return self.translate(text)
111
  except Exception as e:
deep_translator/parent.py CHANGED
@@ -3,6 +3,8 @@
3
  from .exceptions import NotValidPayload, NotValidLength, InvalidSourceOrTargetLanguage
4
  from abc import ABC, abstractmethod
5
  import string
 
 
6
  class BaseTranslator(ABC):
7
  """
8
  Abstract class that serve as a parent translator for other different translators
 
3
  from .exceptions import NotValidPayload, NotValidLength, InvalidSourceOrTargetLanguage
4
  from abc import ABC, abstractmethod
5
  import string
6
+
7
+
8
  class BaseTranslator(ABC):
9
  """
10
  Abstract class that serve as a parent translator for other different translators
deep_translator/qcri.py CHANGED
@@ -3,6 +3,7 @@ import requests
3
  from .constants import BASE_URLS, QCRI_LANGUAGE_TO_CODE
4
  from .exceptions import (ServerException, TranslationNotFound)
5
 
 
6
  class QCRI(object):
7
  """
8
  class that wraps functions, which use the QRCI translator under the hood to translate word(s)
 
3
  from .constants import BASE_URLS, QCRI_LANGUAGE_TO_CODE
4
  from .exceptions import (ServerException, TranslationNotFound)
5
 
6
+
7
  class QCRI(object):
8
  """
9
  class that wraps functions, which use the QRCI translator under the hood to translate word(s)
deep_translator/yandex.py CHANGED
@@ -119,7 +119,7 @@ class YandexTranslator(object):
119
  @return: translated text
120
  """
121
  try:
122
- with open(path) as f:
123
  text = f.read()
124
 
125
  return self.translate(text)
 
119
  @return: translated text
120
  """
121
  try:
122
+ with open(path, 'r', encoding='utf-8') as f:
123
  text = f.read()
124
 
125
  return self.translate(text)