mirror of
https://github.com/xuthus83/LittlePaimon.git
synced 2025-04-12 23:29:37 +08:00
✨ 优化Wiki
别名匹配
This commit is contained in:
parent
ebaba5cbdc
commit
3d9e2af29d
@ -251,6 +251,9 @@ async def _(bot: Bot, event: MessageEvent, state: T_State, type: str = Arg('type
|
|||||||
MessageSegment.image(API[type].format(proxy=config.github_proxy, name=final_name)))
|
MessageSegment.image(API[type].format(proxy=config.github_proxy, name=final_name)))
|
||||||
except ActionFailed:
|
except ActionFailed:
|
||||||
await total_wiki.finish(f'{final_name}的{type}发送失败,可能是网络问题或者不存在该资源')
|
await total_wiki.finish(f'{final_name}的{type}发送失败,可能是网络问题或者不存在该资源')
|
||||||
|
else:
|
||||||
|
if type == '材料':
|
||||||
|
matches = get_match_alias(name, ['角色', '武器', '原魔'])
|
||||||
else:
|
else:
|
||||||
matches = get_match_alias(name, ['角色', '武器', '原魔', '圣遗物'])
|
matches = get_match_alias(name, ['角色', '武器', '原魔', '圣遗物'])
|
||||||
if m := await get_match_card(name):
|
if m := await get_match_card(name):
|
||||||
|
@ -65,8 +65,7 @@ ALIAS_TYPE = Literal['角色', '武器', '原魔', '圣遗物']
|
|||||||
|
|
||||||
|
|
||||||
def get_match_alias(name: str, types: Union[List[ALIAS_TYPE], ALIAS_TYPE] = None,
|
def get_match_alias(name: str, types: Union[List[ALIAS_TYPE], ALIAS_TYPE] = None,
|
||||||
one_to_list: bool = False) -> Union[
|
one_to_list: bool = False) -> Union[Dict[str, List[str]], List[str]]:
|
||||||
Dict[str, List[str]], List[str]]:
|
|
||||||
"""
|
"""
|
||||||
根据字符串消息,获取与之相似或匹配的角色、武器、原魔名
|
根据字符串消息,获取与之相似或匹配的角色、武器、原魔名
|
||||||
:param name: 名称
|
:param name: 名称
|
||||||
@ -79,10 +78,12 @@ def get_match_alias(name: str, types: Union[List[ALIAS_TYPE], ALIAS_TYPE] = None
|
|||||||
elif isinstance(types, str):
|
elif isinstance(types, str):
|
||||||
types = [types]
|
types = [types]
|
||||||
matches = {}
|
matches = {}
|
||||||
|
include_flag = False
|
||||||
for type in types:
|
for type in types:
|
||||||
alias_list = alias_file[type]
|
alias_list = alias_file[type]
|
||||||
matches[type] = []
|
matches[type] = []
|
||||||
if type == '角色':
|
if type == '角色':
|
||||||
|
# 如果是主角,则返回旅行者+元素类型
|
||||||
if name.startswith(('风', '岩', '雷', '草', '水', '火', '冰')) and name.endswith(
|
if name.startswith(('风', '岩', '雷', '草', '水', '火', '冰')) and name.endswith(
|
||||||
('主', '主角', '旅行者')):
|
('主', '主角', '旅行者')):
|
||||||
matches[type].append(f'旅行者{name[0]}')
|
matches[type].append(f'旅行者{name[0]}')
|
||||||
@ -90,28 +91,38 @@ def get_match_alias(name: str, types: Union[List[ALIAS_TYPE], ALIAS_TYPE] = None
|
|||||||
matches[type].append(name)
|
matches[type].append(name)
|
||||||
else:
|
else:
|
||||||
for alias in alias_list.values():
|
for alias in alias_list.values():
|
||||||
|
# 如果该角色别名列表里有和待匹配的name,则只保留该角色
|
||||||
if name in alias:
|
if name in alias:
|
||||||
matches[type] = [alias[0]]
|
matches[type] = [alias[0]]
|
||||||
|
include_flag = True
|
||||||
break
|
break
|
||||||
|
# 否则,则模糊匹配,有匹配到的别名,具把角色添加到匹配列表
|
||||||
if get_close_matches(name, alias, cutoff=0.6):
|
if get_close_matches(name, alias, cutoff=0.6):
|
||||||
matches[type].append(alias[0])
|
matches[type].append(alias[0])
|
||||||
elif type in {'武器', '圣遗物'}:
|
elif type in {'武器', '圣遗物'}:
|
||||||
|
# 逻辑和角色一样
|
||||||
for raw_name, alias in alias_list.items():
|
for raw_name, alias in alias_list.items():
|
||||||
if name in alias:
|
if name in alias:
|
||||||
matches[type] = [raw_name]
|
matches[type] = [raw_name]
|
||||||
|
include_flag = True
|
||||||
break
|
break
|
||||||
else:
|
|
||||||
if get_close_matches(name, alias, cutoff=0.6):
|
if get_close_matches(name, alias, cutoff=0.6):
|
||||||
matches[type].append(raw_name)
|
matches[type].append(raw_name)
|
||||||
elif type == '原魔':
|
elif type == '原魔':
|
||||||
|
# 如果已经有精准匹配到的角色、武器或圣遗物,则不再继续匹配原魔
|
||||||
|
if not include_flag:
|
||||||
|
# 尽可能的匹配所有类似的原魔名
|
||||||
for raw_name, alias in alias_list.items():
|
for raw_name, alias in alias_list.items():
|
||||||
if get_close_matches(name, alias, cutoff=0.5):
|
if get_close_matches(name, alias, cutoff=0.5):
|
||||||
matches[type].append(raw_name)
|
matches[type].append(raw_name)
|
||||||
if not matches[type]:
|
if not matches[type]:
|
||||||
del matches[type]
|
del matches[type]
|
||||||
|
|
||||||
if one_to_list and len(matches) == 1:
|
if one_to_list and len(matches) == 1:
|
||||||
|
# 如果one_to_list为true且匹配到的种类只有一种,则以列表形式直接返回该种类匹配结果
|
||||||
return list(matches.values())[0]
|
return list(matches.values())[0]
|
||||||
else:
|
else:
|
||||||
|
# 否则,以字典形式返回各种类的匹配结果
|
||||||
return matches
|
return matches
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user