diff --git a/CHANGELOG.md b/CHANGELOG.md index a863233f..9685f618 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)  ↑  diff --git a/README.md b/README.md index ce984362..d2707364 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/autosub/cmdline_utils.py b/autosub/cmdline_utils.py index e2d5df3e..59c917e6 100644 --- a/autosub/cmdline_utils.py +++ b/autosub/cmdline_utils.py @@ -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 @@ -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.")) diff --git a/autosub/core.py b/autosub/core.py index 3770c941..9b3294ad 100644 --- a/autosub/core.py +++ b/autosub/core.py @@ -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, @@ -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(), ' ', @@ -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) @@ -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() diff --git a/docs/CHANGELOG.zh-Hans.md b/docs/CHANGELOG.zh-Hans.md index d8362921..12adbb81 100644 --- a/docs/CHANGELOG.zh-Hans.md +++ b/docs/CHANGELOG.zh-Hans.md @@ -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)  ↑  diff --git a/docs/README.zh-Hans.md b/docs/README.zh-Hans.md index ca871a0a..26f4433a 100644 --- a/docs/README.zh-Hans.md +++ b/docs/README.zh-Hans.md @@ -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 @@ -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 @@ -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 @@ -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)来了解详情。  ↑