Skip to content

Commit

Permalink
fix issue #138
Browse files Browse the repository at this point in the history
fix the inaccurate translation results caused by the wrong result confidence.
  • Loading branch information
wizyoung committed Dec 18, 2019
1 parent d87736a commit 2d532fe
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/googletrans/__init__.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Free Google Translate API for Python. Translates totally free of charge."""
__all__ = 'Translator',
__version__ = '2.3.0'
__version__ = '2.4.1'


from googletrans.client import Translator
Expand Down
Empty file modified src/googletrans/adapters.py
100755 → 100644
Empty file.
Empty file modified src/googletrans/client.py
100755 → 100644
Empty file.
Empty file modified src/googletrans/compat.py
100755 → 100644
Empty file.
Empty file modified src/googletrans/constants.py
100755 → 100644
Empty file.
26 changes: 20 additions & 6 deletions src/googletrans/gtoken.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,19 @@ def _xr(self, a, b):
return a

def acquire(self, text):
a = []
# Convert text to ints
for i in text:
val = ord(i)
if val < 0x10000:
a += [val]
else:
# Python doesn't natively use Unicode surrogates, so account for those
a += [
math.floor((val - 0x10000)/0x400 + 0xD800),
math.floor((val - 0x10000)%0x400 + 0xDC00)
]

b = self.tkk if self.tkk != '0' else ''
d = b.split('.')
b = int(d[0]) if len(d) > 1 else 0
Expand All @@ -149,8 +162,8 @@ def acquire(self, text):
e = []
g = 0
size = len(text)
for i, char in enumerate(text):
l = ord(char)
while g < size:
l = a[g]
# just append if l is less than 128(ascii: DEL)
if l < 128:
e.append(l)
Expand All @@ -161,15 +174,16 @@ def acquire(self, text):
else:
# append calculated value if l matches special condition
if (l & 64512) == 55296 and g + 1 < size and \
ord(text[g + 1]) & 64512 == 56320:
a[g + 1] & 64512 == 56320:
g += 1
l = 65536 + ((l & 1023) << 10) + ord(text[g]) & 1023
l = 65536 + ((l & 1023) << 10) + (a[g] & 1023) # This bracket is important
e.append(l >> 18 | 240)
e.append(l >> 12 & 63 | 128)
else:
e.append(l >> 12 | 224)
e.append(l >> 6 & 63 | 128)
e.append(l & 63 | 128)
e.append(l & 63 | 128)
g += 1
a = b
for i, value in enumerate(e):
a += value
Expand All @@ -185,4 +199,4 @@ def acquire(self, text):
def do(self, text):
self._update()
tk = self.acquire(text)
return tk
return tk
Empty file modified src/googletrans/models.py
100755 → 100644
Empty file.
Empty file modified src/googletrans/urls.py
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion src/googletrans/utils.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def build_params(query, src, dest, token):
params = {
'client': 't',
'client': 'webapp',
'sl': src,
'tl': dest,
'hl': dest,
Expand Down
2 changes: 1 addition & 1 deletion src/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.1
2.1.2

0 comments on commit 2d532fe

Please sign in to comment.