support --translation_style (#185)

* support translation_style

* clean code
This commit is contained in:
hleft 2023-03-19 21:20:30 +08:00 committed by GitHub
parent e0aef6f8af
commit 1d680a1ccf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 10 deletions

View File

@ -43,6 +43,7 @@ The bilingual_book_maker is an AI translation tool that uses ChatGPT to assist u
- `--accumulated_num` Wait for how many tokens have been accumulated before starting the translation. gpt3.5 limits the total_token to 4090. For example, if you use --accumulated_num 1600, maybe openai will - `--accumulated_num` Wait for how many tokens have been accumulated before starting the translation. gpt3.5 limits the total_token to 4090. For example, if you use --accumulated_num 1600, maybe openai will
output 2200 tokens and maybe 200 tokens for other messages in the system messages user messages, 1600+2200+200=4000, So you are close to reaching the limit. You have to choose your own output 2200 tokens and maybe 200 tokens for other messages in the system messages user messages, 1600+2200+200=4000, So you are close to reaching the limit. You have to choose your own
value, there is no way to know if the limit is reached before sending value, there is no way to know if the limit is reached before sending
- `--translation_style` example: `--translation_style "color: #808080; font-style: italic;"`
### Examples ### Examples

View File

@ -182,6 +182,12 @@ and maybe 200 tokens for other messages in the system messages user messages, 16
So you are close to reaching the limit. You have to choose your own value, there is no way to know if the limit is reached before sending So you are close to reaching the limit. You have to choose your own value, there is no way to know if the limit is reached before sending
""", """,
) )
parser.add_argument(
"--translation_style",
dest="translation_style",
type=str,
help="""ex: --translation_style "color: #808080; font-style: italic;" """,
)
parser.add_argument( parser.add_argument(
"--batch_size", "--batch_size",
dest="batch_size", dest="batch_size",
@ -273,6 +279,8 @@ So you are close to reaching the limit. You have to choose your own value, there
e.translate_tags = options.translate_tags e.translate_tags = options.translate_tags
if options.accumulated_num > 1: if options.accumulated_num > 1:
e.accumulated_num = options.accumulated_num e.accumulated_num = options.accumulated_num
if options.translation_style:
e.translation_style = options.translation_style
if options.batch_size: if options.batch_size:
e.batch_size = options.batch_size e.batch_size = options.batch_size
e.make_bilingual_book() e.make_bilingual_book()

View File

@ -43,7 +43,10 @@ class EPUBBookLoader(BaseBookLoader):
self.translate_tags = "p" self.translate_tags = "p"
self.allow_navigable_strings = False self.allow_navigable_strings = False
self.accumulated_num = 1 self.accumulated_num = 1
self.helper = EPUBBookLoaderHelper(self.translate_model, self.accumulated_num) self.translation_style = ""
self.helper = EPUBBookLoaderHelper(
self.translate_model, self.accumulated_num, self.translation_style
)
# monkey pathch for # 173 # monkey pathch for # 173
def _write_items_patch(obj): def _write_items_patch(obj):
@ -120,7 +123,7 @@ class EPUBBookLoader(BaseBookLoader):
new_p.string = self.translate_model.translate(p.text) new_p.string = self.translate_model.translate(p.text)
self.p_to_save.append(new_p.text) self.p_to_save.append(new_p.text)
p.insert_after(new_p) self.helper.insert_trans(p, new_p.string, self.translation_style)
index += 1 index += 1
if index % 20 == 0: if index % 20 == 0:
@ -166,6 +169,9 @@ class EPUBBookLoader(BaseBookLoader):
count = length count = length
def make_bilingual_book(self): def make_bilingual_book(self):
self.helper = EPUBBookLoaderHelper(
self.translate_model, self.accumulated_num, self.translation_style
)
new_book = self._make_new_book(self.origin_book) new_book = self._make_new_book(self.origin_book)
all_items = list(self.origin_book.get_items()) all_items = list(self.origin_book.get_items())
trans_taglist = self.translate_tags.split(",") trans_taglist = self.translate_tags.split(",")
@ -269,7 +275,9 @@ class EPUBBookLoader(BaseBookLoader):
new_p = self.p_to_save[index] new_p = self.p_to_save[index]
else: else:
new_p.string = self.p_to_save[index] new_p.string = self.p_to_save[index]
p.insert_after(new_p) self.helper.insert_trans(
p, new_p.string, self.translation_style
)
index += 1 index += 1
else: else:
break break

View File

@ -3,15 +3,25 @@ from copy import copy
class EPUBBookLoaderHelper: class EPUBBookLoaderHelper:
def __init__(self, translate_model, accumulated_num): def __init__(self, translate_model, accumulated_num, translation_style):
self.translate_model = translate_model self.translate_model = translate_model
self.accumulated_num = accumulated_num self.accumulated_num = accumulated_num
self.translation_style = translation_style
def insert_trans(self, p, text, translation_style=""):
if p.string is not None and p.string.strip() == text.strip():
return
new_p = copy(p)
new_p.string = text
if translation_style != "":
new_p["style"] = translation_style
p.insert_after(new_p)
def deal_new(self, p, wait_p_list): def deal_new(self, p, wait_p_list):
self.deal_old(wait_p_list) self.deal_old(wait_p_list)
new_p = copy(p) self.insert_trans(
new_p.string = self.translate_model.translate(p.text) p, self.translate_model.translate(p.text), self.translation_style
p.insert_after(new_p) )
def deal_old(self, wait_p_list): def deal_old(self, wait_p_list):
if not wait_p_list: if not wait_p_list:
@ -22,9 +32,7 @@ class EPUBBookLoaderHelper:
for i in range(len(wait_p_list)): for i in range(len(wait_p_list)):
if i < len(result_txt_list): if i < len(result_txt_list):
p = wait_p_list[i] p = wait_p_list[i]
new_p = copy(p) self.insert_trans(p, result_txt_list[i], self.translation_style)
new_p.string = result_txt_list[i]
p.insert_after(new_p)
wait_p_list.clear() wait_p_list.clear()