用户:RedDragon/Test:修订间差异
来自Rizline中文维基
更多操作
小 |
小 |
||
| 第47行: | 第47行: | ||
} | } | ||
// | /*计算权重 | ||
输入字符只在目标字符串中匹配一次 | |||
允许间隔,顺序匹配 | |||
每匹配一个字符加1分 | |||
连续匹配额外加分*/ | |||
function getMatchScore(input, target) { | function getMatchScore(input, target) { | ||
if (!input || !target) return 0 | if (!input || !target) return { score: 0, matched: "" } | ||
input = input.toLowerCase() | input = input.toLowerCase() | ||
target = target.toLowerCase() | target = target.toLowerCase() | ||
if (input | if (input == target) return { score: 100, matched: input } | ||
let | // 输入作为目标的子序列 | ||
let | let score1 = 0, matched1 = "", lastPos1 = -1, bonus1 = 0, pos1 = 0 | ||
for (let i = 0; i < input.length; i++) { | |||
let found = false | |||
for (let j = pos1; j < target.length; j++) { | |||
if (input[i] == target[j]) { | |||
score1 += 1 | |||
matched1 += input[i] | |||
if (lastPos1 >= 0) { | |||
if (j == lastPos1 + 1) { | |||
bonus1 += 1 | |||
} | |||
} | |||
lastPos1 = j | |||
pos1 = j + 1 | |||
found = true | |||
break | |||
} | |||
} | |||
if (!found) break | |||
} | |||
score1 += bonus1 | |||
for (let i = 0; i < | // 目标作为输入的子序列 | ||
let score2 = 0, matched2 = "", lastPos2 = -1, bonus2 = 0, pos2 = 0 | |||
for (let i = 0; i < target.length; i++) { | |||
let found = false | let found = false | ||
for (let j = | for (let j = pos2; j < input.length; j++) { | ||
if ( | if (target[i] == input[j]) { | ||
score2 += 1 | |||
matched2 += target[i] | |||
if (lastPos2 >= 0) { | |||
if (j == lastPos2 + 1) { | |||
bonus2 += 1 | |||
} | |||
} | |||
lastPos2 = j | |||
pos2 = j + 1 | |||
found = true | found = true | ||
break | break | ||
| 第69行: | 第102行: | ||
if (!found) break | if (!found) break | ||
} | } | ||
return score | score2 += bonus2 | ||
if (score1 >= score2) { | |||
return { score: score1, matched: matched1 } | |||
} else { | |||
return { score: score2, matched: matched2 } | |||
} | |||
} | } | ||
| 第83行: | 第122行: | ||
const results = songlist.map(function (song) { | const results = songlist.map(function (song) { | ||
let | let best = getMatchScore(searchText, song.title ? song.title.toLowerCase() : '') | ||
let bestAlias = "" | |||
if (song.aliases) { | if (song.aliases) { | ||
if (song.aliases.length > 0) { | if (song.aliases.length > 0) { | ||
song.aliases.forEach(function (alias) { | song.aliases.forEach(function (alias) { | ||
let result = getMatchScore(searchText, alias.toLowerCase()) | |||
if (result.score > best.score) { | |||
best = result | |||
bestAlias = alias | |||
} | |||
}) | }) | ||
} | } | ||
| 第93行: | 第137行: | ||
return { | return { | ||
song: song, | song: song, | ||
score: | score: best.score, | ||
matched: best.matched, | |||
matchedAlias: bestAlias | |||
} | } | ||
}).filter(function (result) { | }).filter(function (result) { | ||
// if (result.score >= resc) { | // if (result.score >= resc) { | ||
// console.log(result.song.title | // console.log( | ||
// `${result.song.title}: [${result.matched}]${(result.matchedAlias ? ` (${result.matchedAlias})` : "")} ${result.score} ` | |||
// ) | |||
// } | // } | ||
return result.score >= resc | return result.score >= resc | ||
2025年9月14日 (日) 02:14的版本