mirror of
https://gitea.cookies.4d.ink/Cookies/CookiesChartConverter.git
synced 2025-10-26 03:02:39 +00:00
66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
from difflib import SequenceMatcher
|
||
import json
|
||
sim_num = 0.5
|
||
def deduplicate_by_songid(matches):
|
||
unique = {}
|
||
for item in matches:
|
||
sid = item['SongID']
|
||
if sid not in unique:
|
||
unique[sid] = item
|
||
else:
|
||
# 比较分数,保留分数更高的
|
||
if item['Score'] > unique[sid]['Score']:
|
||
unique[sid] = item
|
||
return list(unique.values())
|
||
|
||
def similarity(a, b):
|
||
return SequenceMatcher(None, a, b).ratio()
|
||
|
||
def find_song_id(name_or_id):
|
||
songs = json.loads(open('alias.json','r',encoding='utf-8').read())['content']
|
||
name_lower = str(name_or_id).strip().lower()
|
||
|
||
|
||
|
||
|
||
|
||
# 模糊匹配(>= sim_num),返回列表
|
||
fuzzy_matches = []
|
||
for song in songs:
|
||
sim_name = similarity(name_lower, song["Name"].lower())
|
||
if sim_name >= sim_num:
|
||
fuzzy_matches.append({
|
||
"Name": song["Name"],
|
||
"MatchedWith": song["Name"],
|
||
"SongID": song["SongID"],
|
||
"Score": sim_name
|
||
})
|
||
for alias in song["Alias"]:
|
||
sim_alias = similarity(name_lower, alias.lower())
|
||
if sim_alias >= sim_num:
|
||
fuzzy_matches.append({
|
||
"Name": song["Name"],
|
||
"MatchedWith": alias,
|
||
"SongID": song["SongID"],
|
||
"Score": sim_alias
|
||
})
|
||
sim_alias = similarity(str(song["SongID"]), name_lower)
|
||
if sim_alias >= 1:
|
||
fuzzy_matches.append({
|
||
"Name": song["Name"],
|
||
"MatchedWith": str(song["SongID"]),
|
||
"SongID": song["SongID"],
|
||
"Score": sim_alias
|
||
})
|
||
|
||
# 按相似度排序,返回列表或者空字典
|
||
fuzzy_matches.sort(key=lambda x: x["Score"], reverse=True)
|
||
result = deduplicate_by_songid(fuzzy_matches)
|
||
return {'length': len(fuzzy_matches), 'result': result}
|
||
|
||
# 测试调用
|
||
if __name__ == '__main__':
|
||
query = input("请输入搜索内容: ")
|
||
result = find_song_id(query)
|
||
print(result)
|