Skip to content

Commit

Permalink
减少不必要的翻译
Browse files Browse the repository at this point in the history
  • Loading branch information
abse4411 committed Apr 23, 2024
1 parent 8a47658 commit d82f101
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ open_ai:
t {index_or_name} -t openai -n {API_name} -l {lang}
```
> **⭐Suggestion⭐**<br />
> We strongly recommended using the 'llm_translate' command instead of the above translation command. To use `llm_translate` command:
> We strongly recommend that using the 'llm_translate' command instead of the above translation command. To use `llm_translate` command:
> `lt {index or name} -l {lang} -m qwen:0.5b -t Chinese` or
> `lt {index or name} -l {lang} -a`
>
Expand Down
2 changes: 1 addition & 1 deletion command/translation/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
from config import default_config
from store.database.base import flush
from store.group import group_translations_by, ALL
from store.misc import strip_tags
from trans.openai_api import OpenAITranslator
from util import strip_or_none
from util.renpy import strip_tags


class _InnerTranslator(OpenAITranslator):
Expand Down
15 changes: 11 additions & 4 deletions local_server/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
from injection.base.base import UndoOnFailedCallInjector, undo_chain
from injection.default import FontInjection, OnlinePyInjection
from local_server.safe import SafeDict
from store.misc import strip_tags, quote_with_fonttag
from store.misc import quote_with_fonttag
from store.web_index import WebTranslationIndex
from trans import Translator
from util import strip_or_none, exists_file
from util.renpy import strip_tags


class TranslationRunner(threading.Thread):
Expand Down Expand Up @@ -61,15 +62,21 @@ def run(self):
self._close_translator()
print('Translator is stopped by the user.')
return
packs, texts = [], []
packs, texts, passed_packs = [], [], []
try:
while not self._queue.empty():
p = self._queue.get()
t = p['substituted']
packs.append(p)
texts.append(strip_tags(t))
s_text = strip_tags(t)
if strip_or_none(s_text) is not None:
texts.append(s_text)
packs.append(p)
else:
p['new_text'] = t
passed_packs.append(p)
if len(packs) == self._batch_size:
break
self._update_func(passed_packs)
new_texts = self._translator.translate_batch(texts)
for p, t in zip(packs, new_texts):
p['new_text'] = t
Expand Down
10 changes: 0 additions & 10 deletions store/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,6 @@ def block_of(
}


def strip_tags(text: str):
if text:
tags = list_tags(text)
if tags:
for t in tags.keys():
text = text.replace(t, '')
return text
return ''


def quote_with_fonttag(font_dir, font, text):
if font:
return f'{{font={font_dir}{font}}}{text}{{/font}}'
Expand Down
3 changes: 2 additions & 1 deletion store/web_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
from store.database.base import db_context
from store.index import extra_data_of
from store.index_type import register_index
from store.misc import ast_of, block_of, strip_tags, quote_with_fonttag
from store.misc import ast_of, block_of, quote_with_fonttag
from util import exists_file, assert_not_blank, default_read, default_write
from util.renpy import strip_tags


class WebTranslationIndex(TranslationIndex):
Expand Down
2 changes: 1 addition & 1 deletion translation_provider/closeapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from trans.openai_api import OpenAITranslator
from store.misc import strip_tags
from trans import Translator
from translation_provider.base import Provider, register_provider
from util import strip_or_none
from util.renpy import strip_tags


class _InnerTranslator(Translator):
Expand Down
1 change: 0 additions & 1 deletion translator/api/openai_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from command.translation.base import register_cmd_translator
from config.base import ProjzConfig
from trans.openai_api import OpenAITranslator
from store.misc import strip_tags
from translator.base import CachedTranslatorTemplate
from util import strip_or_none, my_input, line_to_args

Expand Down
10 changes: 7 additions & 3 deletions util/renpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ def list_escape_chars(text: str):


def strip_tags(text: str):
if text is not None:
return regex_tag.sub('', text)
return text
if text:
tags = list_tags(text)
if tags:
for t in tags.keys():
text = text.replace(t, '')
return text
return ''


def contain_alpha(text: str):
Expand Down

0 comments on commit d82f101

Please sign in to comment.