Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
hleft committed Mar 11, 2023
1 parent 7c91331 commit 8144fdd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
7 changes: 7 additions & 0 deletions book_maker/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ def main():
metavar="PROMPT_TEMPLATE",
help="used for customizing the prompt. It can be the prompt template string, or a path to the template file. The valid placeholders are `{text}` and `{language}`.",
)
parser.add_argument(
"--accumulated_num",
dest="accumulated_num",
type=int,
default=1,
help="Wait for how many characters have been accumulated before starting the translation",
)

options = parser.parse_args()
PROXY = options.proxy
Expand Down
30 changes: 15 additions & 15 deletions book_maker/loader/epub_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,22 @@ def _make_new_book(self, book):
return new_book

def make_bilingual_book(self):
def deal_new(p, waitPList):
ret = deal_old(waitPList)
def deal_new(p, wait_p_list):
ret = deal_old(wait_p_list)
new_p = copy(p)
new_p.string = self.translate_model.translate(p.text)
p.insert_after(new_p)
return ret

def deal_old(waitPList):
if len(waitPList) == 0:
def deal_old(wait_p_list):
if len(wait_p_list) == 0:
return []

resultTxtList = self.translate_model.translate_list(waitPList)
resultTxtList = self.translate_model.translate_list(wait_p_list)

for i in range(0, len(waitPList)):
for i in range(0, len(wait_p_list)):
if i < len(resultTxtList):
p = waitPList[i]
p = wait_p_list[i]
new_p = copy(p)
new_p.string = resultTxtList[i]
p.insert_after(new_p)
Expand Down Expand Up @@ -130,28 +130,28 @@ def deal_old(waitPList):
sendNum = self.accumulated_num
if sendNum > 1:
count = 0
waitPList = []
wait_p_list = []
for i in range(0, len(p_list)):
p = p_list[i]
if not p.text or self._is_special_text(p.text):
continue
length = len(p.text)
if length > sendNum:
waitPList = deal_new(p, waitPList)
wait_p_list = deal_new(p, wait_p_list)
continue
if i == len(p_list) - 1:
if count + length < sendNum:
waitPList.append(p)
waitPList = deal_old(waitPList)
wait_p_list.append(p)
wait_p_list = deal_old(wait_p_list)
else:
waitPList = deal_new(p, waitPList)
wait_p_list = deal_new(p, wait_p_list)
break
if count + length < sendNum:
count += length
waitPList.append(p)
wait_p_list.append(p)
else:
waitPList = deal_old(waitPList)
waitPList.append(p)
wait_p_list = deal_old(wait_p_list)
wait_p_list.append(p)
count = len(p.text)
else:
is_test_done = self.is_test and index > self.test_num
Expand Down
4 changes: 2 additions & 2 deletions book_maker/translator/chatgptapi_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ def translate(self, text):
def translate_list(self, plist):
sep = "\n\n\n\n\n"
new_str = sep.join([item.text for item in plist])
resultStr = self.translate(new_str)
result_str = self.translate(new_str)

lines = resultStr.split("\n")
lines = result_str.split("\n")
lines = [line.strip() for line in lines if line.strip() != ""]

return lines

0 comments on commit 8144fdd

Please sign in to comment.