feat: 使用 opencc 将歌曲名转化为简体 (#192)
This commit is contained in:
parent
a428f377d4
commit
3110c2221b
@ -16,6 +16,7 @@ dependencies = [
|
|||||||
"aiofiles>=24.1.0",
|
"aiofiles>=24.1.0",
|
||||||
"ga4mp>=2.0.4",
|
"ga4mp>=2.0.4",
|
||||||
"apscheduler>=3.10.4",
|
"apscheduler>=3.10.4",
|
||||||
|
"opencc-python-reimplemented==0.1.7",
|
||||||
]
|
]
|
||||||
requires-python = ">=3.10,<3.12"
|
requires-python = ">=3.10,<3.12"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
@ -31,6 +31,10 @@ from mutagen.wave import WAVE
|
|||||||
from requests.utils import cookiejar_from_dict
|
from requests.utils import cookiejar_from_dict
|
||||||
|
|
||||||
from xiaomusic.const import SUPPORT_MUSIC_TYPE
|
from xiaomusic.const import SUPPORT_MUSIC_TYPE
|
||||||
|
from opencc import OpenCC
|
||||||
|
|
||||||
|
|
||||||
|
cc = OpenCC('t2s') # convert from Traditional Chinese to Simplified Chinese
|
||||||
|
|
||||||
|
|
||||||
### HELP FUNCTION ###
|
### HELP FUNCTION ###
|
||||||
@ -90,6 +94,10 @@ def fuzzyfinder(user_input, collection, extra_search_index=None):
|
|||||||
extra_search_index=extra_search_index)
|
extra_search_index=extra_search_index)
|
||||||
|
|
||||||
|
|
||||||
|
def traditional_to_simple(to_convert: str):
|
||||||
|
return cc.convert(to_convert)
|
||||||
|
|
||||||
|
|
||||||
# 关键词检测
|
# 关键词检测
|
||||||
def keyword_detection(user_input, str_list, n):
|
def keyword_detection(user_input, str_list, n):
|
||||||
# 过滤包含关键字的字符串
|
# 过滤包含关键字的字符串
|
||||||
@ -119,15 +127,17 @@ def real_search(prompt, candidates, cutoff, n):
|
|||||||
|
|
||||||
|
|
||||||
def find_best_match(user_input, collection, cutoff=0.6, n=1, extra_search_index=None):
|
def find_best_match(user_input, collection, cutoff=0.6, n=1, extra_search_index=None):
|
||||||
lower_collection = {item.lower(): item for item in collection}
|
lower_collection = {traditional_to_simple(item.lower()): item for item in collection}
|
||||||
user_input = user_input.lower()
|
user_input = traditional_to_simple(user_input.lower())
|
||||||
matches = real_search(user_input, lower_collection.keys(), cutoff, n)
|
matches = real_search(user_input, lower_collection.keys(), cutoff, n)
|
||||||
cur_matched_collection = [lower_collection[match] for match in matches]
|
cur_matched_collection = [lower_collection[match] for match in matches]
|
||||||
if len(matches) >= n or extra_search_index is None:
|
if len(matches) >= n or extra_search_index is None:
|
||||||
return cur_matched_collection[:n]
|
return cur_matched_collection[:n]
|
||||||
|
|
||||||
# 如果数量不满足,继续搜索
|
# 如果数量不满足,继续搜索
|
||||||
lower_extra_search_index = {k.lower():v for k, v in extra_search_index.items()}
|
lower_extra_search_index = {
|
||||||
|
traditional_to_simple(k.lower()): v for k, v in extra_search_index.items()
|
||||||
|
}
|
||||||
matches = real_search(user_input, lower_extra_search_index.keys(), cutoff, n)
|
matches = real_search(user_input, lower_extra_search_index.keys(), cutoff, n)
|
||||||
cur_matched_collection += [lower_extra_search_index[match] for match in matches]
|
cur_matched_collection += [lower_extra_search_index[match] for match in matches]
|
||||||
return cur_matched_collection[:n]
|
return cur_matched_collection[:n]
|
||||||
|
Loading…
Reference in New Issue
Block a user