Skip to content

Commit

Permalink
fix: 修复\n错误,优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
timelic committed Feb 17, 2023
1 parent c6ac7b1 commit d6f77ce
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
3 changes: 1 addition & 2 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ def helper(search: str) -> Union[NameDesc, None, NoReturn]:
item = result[i]
name: str = item["cn_name"]
desc: str = item["text"]["desc"]
p_desc: str = item["text"]["pdesc"]
if p_desc != "":
if p_desc := item["text"]["pdesc"] != "":
desc = f"{desc}\n【灵摆效果】\n{p_desc}"
return {"name": name, "desc": desc}

Expand Down
2 changes: 1 addition & 1 deletion index.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def main(
set_status_msg("翻译成功!")

except IOError as e:
# TODO 这儿对应无权限
# 这儿对应无权限
print(e)
log(str(e))
set_status(Status.error_io)
Expand Down
5 changes: 3 additions & 2 deletions interface.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ctypes import alignment
import webbrowser
from os import path
from typing import Any
Expand All @@ -7,8 +8,7 @@
from index import main as install_trans
from utils import get_logger, throttle

RELEASE_URL = "https://www.bilibili.com/read/cv21869124" # TODO

RELEASE_URL = "https://www.bilibili.com/read/cv21869124"

def log(e: Any) -> None:
get_logger().error(e, stack_info=True)
Expand Down Expand Up @@ -50,6 +50,7 @@ def main(page: ft.Page):
]
)
)

"""
dialog
"""
Expand Down
42 changes: 32 additions & 10 deletions translate.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from tqdm import tqdm
from hint import CardRawData, CardRawDataItem
from api import api, api_local
from functools import reduce
from typing import Callable

from tqdm import tqdm
from tqdm.contrib import tenumerate

from api import api, api_local
from hint import CardRawData, CardRawDataItem


def unify_pendulum_desc(desc: str) -> str:
"""
Expand Down Expand Up @@ -45,13 +48,30 @@ def unify_separator(desc: str) -> str:
2. 召唤限制\n
3. ① ②这些的前面不会有\r\n
"""
for key_word in ["②", "③", "④", "⑤", "⑥", "⑦"]:
orders_without_first = ["②", "③", "④", "⑤", "⑥", "⑦"]
ch_separator = "【灵摆效果】"
for order in ["②", "③", "④", "⑤", "⑥", "⑦"]:
for prefix in ["\r\n", "\n", "\r"]:
desc = desc.replace(f"{prefix}{key_word}", key_word)
desc = desc.replace(f"{prefix}{order}", order)
# 处理①
for prefix in ["\r\n", "\n", "\r"]:
desc = desc.replace(f"。{prefix}①", "。①")
return desc.replace("\r\n", "\n").strip()
desc = desc.replace("\r\n", "\n").strip()
# 在●后面紧跟的一个②或者③,它们的前面有\n
parts: list[str] = [f"{'●' if i else ''}{x}" for i, x in enumerate(desc.split("●"))]
for part in parts:
if not part.startswith('●'):
continue
index = len(part) - 1
for order in orders_without_first:
if (i := part.find(order)) != -1:
index = min(index, i)
if (i := part.find("【灵摆效果】")) != -1:
index = min(index, i)
if index != len(part) - 1 and part[index] != ch_separator[0]:
parts[parts.index(part)] = f"{part[:index]}\n{part[index:]}"
# merge parts
return reduce(lambda x, y: f"{x}{y}", parts)


def unity(desc: str) -> str:
Expand All @@ -65,12 +85,14 @@ def unity(desc: str) -> str:
unify_separator(
unify_pendulum_desc(
"""
这张卡不能特殊召唤。这张卡通常召唤的场合,必须把3只解放作召唤\r\n①:这张卡的召唤不会被无效化。\r\n②:在这张卡的召唤成功时,这张卡以外的魔法・陷阱・怪兽的效果不能发动\r\n③:这张卡召唤成功时,把基本分支付到变成100基本分才能发动。这张卡的攻击力・守备力上升支付的数值。\r\n④:支付1000基本分,以场上1只怪兽为对象才能发动。那只怪兽破坏
①:1回合1次,可以从以下效果选择1个发动\r\n●以自己场上1只「X-首领加农」为对象,把这张卡当作装备卡使用给那只怪兽装备。装备怪兽被战斗・效果破坏的场合,作为代替把这张卡破坏。\r\n●装备的这张卡特殊召唤\r\n【灵摆效果】\r\n①:装备怪兽的攻击力・守备力上升400
"""
)
)
]
)
print([unify_separator("①:1回合1次,可以从以下效果选择1个发动。\r\n●以自己场上1只「X-首领加农」为对象,把这张卡当作装备卡使用给那只怪兽装备。装备怪兽被战斗・效果破坏的场合,作为代替把这张卡破坏。\r\n●装备的这张卡特殊召唤。\r\n【灵摆效果】\r\n②:装备怪兽的攻击力・守备力上升400。")])
print([unify_separator("①:1回合1次,可以从以下效果选择1个发动。\r\n●以自己场上1只「X-首领加农」为对象,把这张卡当作装备卡使用给那只怪兽装备。装备怪兽被战斗・效果破坏的场合,作为代替把这张卡破坏。\r\n●装备的这张卡特殊召唤。\r\n②:装备怪兽的攻击力・守备力上升400。●装备的这张卡特殊召唤。\r\n\r\n③:装备怪兽的攻击力・守备力上升400。")])


def card_translate(
Expand All @@ -95,16 +117,16 @@ def search_archived_data(name_jp: str) -> CardRawDataItem | None:
for i, item in iterable:
name_jp = item["name"]["ja-jp"]
progress_update_cb(f"{i}/{len(card_raw_data)}")
if result_archived:= search_archived_data(name_jp):
if result_archived := search_archived_data(name_jp):
# 在已归档数据中找到了对应的日文名
item["name"]["custom"] = result_archived["name"]["custom"]
item["desc"]["custom"] = unity(result_archived["desc"]["custom"])
continue
if result_api_local:= api_local(name_jp):
if result_api_local := api_local(name_jp):
# 在本地API中找到了对应的日文名
item["name"]["custom"] = result_api_local["name"]
item["desc"]["custom"] = unity(result_api_local["desc"])
if result_api:= api(name_jp, network_error_cb):
if result_api := api(name_jp, network_error_cb):
# 在API中找到了对应的日文名
item["name"]["custom"] = result_api["name"]
item["desc"]["custom"] = unity(result_api["desc"]) # 修正灵摆...修正\r\n
Expand Down

0 comments on commit d6f77ce

Please sign in to comment.