-
Notifications
You must be signed in to change notification settings - Fork 628
Open
Labels
FEPfeeluown enhancement proposalfeeluown enhancement proposal
Description
简介与背景
如题,目前fuo未提供于ui或rpc接口( https://github.com/cosven/feeluownx 需要)搜索播放列表中特定歌曲的功能
鉴于直接进行关键词搜索较容易匹配失败,可能需要尝试使用模糊匹配算法
方案概述
- 基于Dice-Sørensen 模糊匹配搜索
- 使用标准库 diffllib (对乱序支持不好)
经过一些尝试,推荐做法是以匹配结果的最多 k 个高可能结果按顺序返回(+ 数量可设置)
另外可以把字符串处理前统一为小写简体,后者依赖opencc
may block cosven/feeluownx#4
另附:
from difflib import SequenceMatcher
from sys import argv
from typing import List
from collections.abc import Hashable
def std_distance(s1: str, s2: str):
s = SequenceMatcher(isjunk=lambda c: c.isspace(), a=s1, b=s2)
return s.ratio()
def distance(x: List[Hashable], y: List[Hashable]) -> float:
wx = [tuple(x[i : i + 2]) for i in range(len(x) - 1)]
wy = [tuple(y[i : i + 2]) for i in range(len(y) - 1)]
nx = len(wx)
ny = len(wy)
hash_set = set(wx)
length = 0
for w in wy:
if w in hash_set:
length += 2
if nx + ny == 0:
return 0.0
return length / (nx + ny)
if __name__ == "__main__":
print(f'{"Std difflib":16}', std_distance(argv[1], argv[2]))
print(f'{"Dice-Sorensen":16}', distance(argv[1], argv[2]))
Metadata
Metadata
Assignees
Labels
FEPfeeluown enhancement proposalfeeluown enhancement proposal