Skip to content

Commit

Permalink
Fix (close #49) google_trans single request length limit for full-wid…
Browse files Browse the repository at this point in the history
…e chars
  • Loading branch information
BingLingGroup committed Aug 2, 2019
1 parent 3da6285 commit f0b0ec3
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion autosub/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pysubs2
import auditok
import googletrans
import wcwidth

# Any changes to the path and your own modules
from autosub import speech_trans_api
Expand Down Expand Up @@ -266,7 +267,18 @@ def list_to_googletrans( # pylint: disable=too-many-locals, too-many-arguments,
is_last = text
valid_index.append(i)
# valid_index for valid text position start
size = size + len(text)
wcswidth_text = wcwidth.wcswidth(text)
if wcswidth_text * 10 / len(text) >= 10:
# If text contains full-wide char,
# count its length about 4 times than the ordinary text.
# Avoid weird problem when text has full-wide char.
# In this case google will count a full-wide char
# at least 2 times larger than a half-wide char.
# It will certainly exceed the limit of the size_per_trans.
# Causing a googletrans internal jsondecode error.
size = size + wcswidth_text * 2
else:
size = size + len(text)
if size > size_per_trans:
# use size_per_trans to split the list
partial_index.append(i)
Expand Down

0 comments on commit f0b0ec3

Please sign in to comment.