打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

用户:RedDragon/Test:修订间差异

来自Rizline中文维基
RedDragon
RedDragon留言 | 贡献 (优化匹配逻辑)
第47行: 第47行:
     }
     }


     //计算匹配度,输入的每个字符只能在目标字符串中匹配一次,必须按顺序,每匹配一个字符加1分
     /*计算权重
    输入字符只在目标字符串中匹配一次
    允许间隔,顺序匹配
    每匹配一个字符加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 === target) return 100
         if (input == target) return { score: 100, matched: input }


         let score = 0
        // 输入作为目标的子序列
         let pos = 0
         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 < input.length; 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 = pos; j < target.length; j++) {
             for (let j = pos2; j < input.length; j++) {
                 if (input[i] === target[j]) {
                 if (target[i] == input[j]) {
                    score += 1
                    score2 += 1
                     pos = j + 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 maxScore = getMatchScore(searchText, song.title ? song.title.toLowerCase() : '')
             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) {
                         maxScore = Math.max(maxScore, getMatchScore(searchText, alias.toLowerCase()))
                         let result = getMatchScore(searchText, alias.toLowerCase())
                        if (result.score > best.score) {
                            best = result
                            bestAlias = alias
                        }
                     })
                     })
                 }
                 }
第93行: 第137行:
             return {
             return {
                 song: song,
                 song: song,
                 score: maxScore
                 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 + ": " + result.score)
             //    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的版本

搜索结果:

别名列表: