mirror of
https://github.com/yihong0618/bilingual_book_maker.git
synced 2025-06-05 19:15:34 +00:00
support --translation_style (#185)
* support translation_style * clean code
This commit is contained in:
parent
e0aef6f8af
commit
1d680a1ccf
@ -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
|
||||
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
|
||||
- `--translation_style` example: `--translation_style "color: #808080; font-style: italic;"`
|
||||
|
||||
### Examples
|
||||
|
||||
|
@ -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
|
||||
""",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--translation_style",
|
||||
dest="translation_style",
|
||||
type=str,
|
||||
help="""ex: --translation_style "color: #808080; font-style: italic;" """,
|
||||
)
|
||||
parser.add_argument(
|
||||
"--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
|
||||
if options.accumulated_num > 1:
|
||||
e.accumulated_num = options.accumulated_num
|
||||
if options.translation_style:
|
||||
e.translation_style = options.translation_style
|
||||
if options.batch_size:
|
||||
e.batch_size = options.batch_size
|
||||
e.make_bilingual_book()
|
||||
|
@ -43,7 +43,10 @@ class EPUBBookLoader(BaseBookLoader):
|
||||
self.translate_tags = "p"
|
||||
self.allow_navigable_strings = False
|
||||
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
|
||||
def _write_items_patch(obj):
|
||||
@ -120,7 +123,7 @@ class EPUBBookLoader(BaseBookLoader):
|
||||
new_p.string = self.translate_model.translate(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
|
||||
|
||||
if index % 20 == 0:
|
||||
@ -166,6 +169,9 @@ class EPUBBookLoader(BaseBookLoader):
|
||||
count = length
|
||||
|
||||
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)
|
||||
all_items = list(self.origin_book.get_items())
|
||||
trans_taglist = self.translate_tags.split(",")
|
||||
@ -269,7 +275,9 @@ class EPUBBookLoader(BaseBookLoader):
|
||||
new_p = self.p_to_save[index]
|
||||
else:
|
||||
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
|
||||
else:
|
||||
break
|
||||
|
@ -3,15 +3,25 @@ from copy import copy
|
||||
|
||||
|
||||
class EPUBBookLoaderHelper:
|
||||
def __init__(self, translate_model, accumulated_num):
|
||||
def __init__(self, translate_model, accumulated_num, translation_style):
|
||||
self.translate_model = translate_model
|
||||
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):
|
||||
self.deal_old(wait_p_list)
|
||||
new_p = copy(p)
|
||||
new_p.string = self.translate_model.translate(p.text)
|
||||
p.insert_after(new_p)
|
||||
self.insert_trans(
|
||||
p, self.translate_model.translate(p.text), self.translation_style
|
||||
)
|
||||
|
||||
def deal_old(self, wait_p_list):
|
||||
if not wait_p_list:
|
||||
@ -22,9 +32,7 @@ class EPUBBookLoaderHelper:
|
||||
for i in range(len(wait_p_list)):
|
||||
if i < len(result_txt_list):
|
||||
p = wait_p_list[i]
|
||||
new_p = copy(p)
|
||||
new_p.string = result_txt_list[i]
|
||||
p.insert_after(new_p)
|
||||
self.insert_trans(p, result_txt_list[i], self.translation_style)
|
||||
|
||||
wait_p_list.clear()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user