LittlePaimon/utils/alias_handler.py

49 lines
1.6 KiB
Python

import difflib
from typing import Union
from .file_handler import load_json
import os
# def get_id_by_alias(name):
#
# for c in character_alias.items():
# if name in c[1]:
# return c
# return None
def get_short_name(name):
short_name = load_json(path=os.path.join(os.path.dirname(__file__),'short_name.json'))
return name if name not in short_name.keys() else short_name[name]
def get_match_alias(msg: str, type: str = 'roles', single_to_dict: bool = False) -> Union[str, list, dict]:
alias_file = load_json(path=os.path.join(os.path.dirname(__file__), 'alias.json'))
alias_list = alias_file[type]
if msg in ['风主', '岩主', '雷主']:
return msg
elif type == 'roles':
possible = {}
for role_id, alias in alias_list.items():
match_list = difflib.get_close_matches(msg, alias, cutoff=0.6, n=3)
if msg in match_list:
if single_to_dict:
return {alias[0]: role_id}
else:
return alias[0]
elif match_list:
possible[alias[0]] = role_id
return possible
elif type == 'weapons':
possible = []
for name, alias in alias_list.items():
match_list = difflib.get_close_matches(msg, alias, cutoff=0.4, n=3)
if msg in match_list:
return name
elif match_list:
possible.append(name)
return possible
elif type == 'monsters':
match_list = difflib.get_close_matches(msg, alias_list, cutoff=0.4, n=5)
return match_list[0] if len(match_list) == 1 else match_list