Skip to content

Commit

Permalink
Fix (close #48) list_to_googletrans index error bug
Browse files Browse the repository at this point in the history
  • Loading branch information
BingLingGroup committed Jul 31, 2019
1 parent 49ff034 commit 3da6285
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 31 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Click up arrow to go back to TOC.

#### Changed(0.5.1-alpha)

- Fix method list_to_googletrans index error bug.
- Fix method list_to_googletrans index error bug. [issue #48](https://github.com/BingLingGroup/autosub/issues/48)
- Fix unix subprocess.check_output compatibility. [issue #47](https://github.com/BingLingGroup/autosub/issues/47)

<escape><a href = "#TOC">&nbsp;&nbsp;</a></escape>
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Due to the autosub PyPI project is maintained by the original autosub repo's own

Include dependencies installation commands.

Install from `alpha` branch.(autosub-0.5.1a)
Install from `alpha` branch.(latest autosub release)

```bash
apt install ffmpeg python python-pip git -y
Expand Down Expand Up @@ -163,7 +163,7 @@ Choco installation command is for cmd.(not Powershell)
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
```

Install from `alpha` branch.(autosub-0.5.1a)
Install from `alpha` branch.(latest autosub release)

```batch
choco install git python2 curl ffmpeg -y
Expand Down
8 changes: 6 additions & 2 deletions autosub/cmdline_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,15 @@ def subs_trans( # pylint: disable=too-many-branches, too-many-statements, too-m
service_urls=args.service_urls
)

if not translated_text or len(translated_text) != len(text_list):
raise exceptions.AutosubException(
_("Error: Translation failed."))

try:
args.output_files.remove("bilingual")
bilingual_sub = pysubs2.SSAFile()
bilingual_sub.styles = src_sub.styles
bilingual_sub.events = src_sub.events.copy()
bilingual_sub.events = src_sub.events[:]
if args.styles and \
len(styles_list) == 2 and \
(args.format == 'ass' or
Expand Down Expand Up @@ -1015,7 +1019,7 @@ def audio_or_video_prcs( # pylint: disable=too-many-branches, too-many-statemen
service_urls=args.service_urls
)

if len(translated_text) != len(regions):
if not translated_text or len(translated_text) != len(regions):
raise exceptions.AutosubException(
_("Error: Translation failed."))

Expand Down
60 changes: 41 additions & 19 deletions autosub/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def list_to_gtv2( # pylint: disable=too-many-locals,too-many-arguments
return translated_text


def list_to_googletrans( # pylint: disable=too-many-locals, too-many-arguments
def list_to_googletrans( # pylint: disable=too-many-locals, too-many-arguments, too-many-branches, too-many-statements
text_list,
src_language=constants.DEFAULT_SRC_LANGUAGE,
dst_language=constants.DEFAULT_DST_LANGUAGE,
Expand All @@ -259,20 +259,36 @@ def list_to_googletrans( # pylint: disable=too-many-locals, too-many-arguments
i = 0
partial_index = []
valid_index = []
is_last = False
for text in text_list:
if text:
if not is_last:
is_last = text
valid_index.append(i)
# valid_index for valid text position start
size = size + len(text)
if size > size_per_trans:
# use size_per_trans to split the list
partial_index.append(i)
size = 0
valid_index.append(i)
# valid_index for valid text position
else:
if is_last:
is_last = text
valid_index.append(i)
# valid_index for valid text position end
i = i + 1
if size:
partial_index.append(i)
# python sequence
# every group's end index
else:
return None

len_valid_index = len(valid_index)

if len_valid_index % 2:
valid_index.append(i)
# valid_index for valid text position end

widgets = [_("Translation: "),
progressbar.Percentage(), ' ',
Expand All @@ -286,7 +302,6 @@ def list_to_googletrans( # pylint: disable=too-many-locals, too-many-arguments
# total position
j = 0
# valid_index position
last_index = 0
translator = googletrans.Translator(
user_agent=user_agent,
service_urls=service_urls)
Expand All @@ -298,23 +313,30 @@ def list_to_googletrans( # pylint: disable=too-many-locals, too-many-arguments
src=src_language)
result_text = translation.text.replace('’', '\'')
result_list = result_text.split('\n')

while i < index:
if j < len(valid_index) and i == valid_index[j]:
# if text is the valid one, append it
translated_text.append(
result_list[valid_index[j] - last_index])
# minus last group's length
j = j + 1
else:
# else append an empty one
k = 0
len_result_list = len(result_list)
while i < index and j < len_valid_index and k < len_result_list:
if not result_list[k]:
# if the result is invalid,
# continue
k = k + 1
continue
if i < valid_index[j]:
# if text is invalid,
# append the empty string
# and then continue
translated_text.append("")

i = i + 1
i = i + 1
pbar.update(i)
continue
if i < valid_index[j + 1]:
# if text is valid, append it
translated_text.append(result_list[k])
k = k + 1
i = i + 1
else:
j = j + 2
pbar.update(i)

last_index = index

if len(partial_index) > 1:
time.sleep(sleep_seconds)
pbar.finish()
Expand Down
2 changes: 1 addition & 1 deletion docs/CHANGELOG.zh-Hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#### 改动(0.5.1-alpha)

- 修复方法list_to_googletrans的列表越界bug。
- 修复方法list_to_googletrans的列表越界bug。[issue #48](https://github.com/BingLingGroup/autosub/issues/48)
- 修复unix subprocess.check_output的兼容性问题。[issue #47](https://github.com/BingLingGroup/autosub/issues/47)

<escape><a href = "#TOC">&nbsp;&nbsp;</a></escape>
Expand Down
12 changes: 6 additions & 6 deletions docs/README.zh-Hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ pip install .

第一行包含依赖的安装。

`alpha`分支安装。(autosub-0.5.0a)
`alpha`分支安装。(最新alpha版)

```bash
apt install ffmpeg python python-pip git -y
pip install git+https://github.com/BingLingGroup/autosub.git@alpha ffmpeg-normalize
```

`origin`分支安装。(autosub-0.4.0a)
`origin`分支安装。autosub-0.4.0a

```bash
apt install ffmpeg python python-pip git -y
pip install git+https://github.com/BingLingGroup/autosub.git@origin
```

从PyPI安装。(autosub-0.3.12)
从PyPI安装。autosub-0.3.12

```bash
apt install ffmpeg python python-pip -y
Expand Down Expand Up @@ -163,7 +163,7 @@ pip install autosub
@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
```

`alpha`分支安装。(autosub-0.5.0a)
`alpha`分支安装。(最新alpha版)

```batch
choco install git python2 curl ffmpeg -y
Expand All @@ -172,7 +172,7 @@ python get-pip.py
pip install git+https://github.com/BingLingGroup/autosub.git@alpha ffmpeg-normalize
```

`origin`分支安装。(autosub-0.4.0a)
`origin`分支安装。autosub-0.4.0a

```batch
choco install git python2 curl ffmpeg -y
Expand All @@ -181,7 +181,7 @@ python get-pip.py
pip install git+https://github.com/BingLingGroup/autosub.git@origin
```

PyPI的版本(autosub-0.3.12)不推荐在windows上使用,因为它无法成功运行。查看[origin分支的更新日志](CHANGELOG.zh-Hans.md#040-alpha---2019-02-17)来了解详情。
PyPI的版本autosub-0.3.12不推荐在windows上使用,因为它无法成功运行。查看[origin分支的更新日志](CHANGELOG.zh-Hans.md#040-alpha---2019-02-17)来了解详情。

<escape><a href = "#目录">&nbsp;&nbsp;</a></escape>

Expand Down

0 comments on commit 3da6285

Please sign in to comment.