jquery - 用于柔性匹配的双向 Quicksilver 算法

标签 jquery algorithm reverse quicksilver

我正在使用 flexselect jQuery plugin简化大 <select> 上的选择列表,它使用了 Quicksilver algorithm灵活匹配(模糊匹配?)选项。

例如:输入3gscam快速匹配iPhone 3GS Camera , 但输入 cam3gs没有。

是否有 Quicksilver 算法的修改版本可以在两个方向上工作,并优先考虑前向匹配?

现有的 jQuery 插件会很有帮助,这样我就不必自己动手了。

最佳答案

我们可以看到方向是由 for 循环设置的,从 length 到 0 如果我们找不到任何选项,我们可以从 0 到 length 以另一种方式运行相同的代码

我希望我做对了

     String.prototype.score = function (abbreviation, offset) {
        offset = offset || 0 // TODO: I think this is unused... remove

        if (abbreviation.length == 0) return 0.9
        if (abbreviation.length > this.length) return 0.0

        for (var i = abbreviation.length; i > 0; i--) {
            var sub_abbreviation = abbreviation.substring(0, i)
            var index = this.indexOf(sub_abbreviation)


            if (index < 0) continue;
            if (index + abbreviation.length > this.length + offset) continue;

            var next_string = this.substring(index + sub_abbreviation.length)
            var next_abbreviation = null

            if (i >= abbreviation.length)
                next_abbreviation = ''
            else
                next_abbreviation = abbreviation.substring(i)

            var remaining_score = next_string.score(next_abbreviation, offset + index)

            if (remaining_score > 0) {
                var score = this.length - next_string.length;

                if (index != 0) {
                    var j = 0;

                    var c = this.charCodeAt(index - 1)
                    if (c == 32 || c == 9) {
                        for (var j = (index - 2); j >= 0; j--) {
                            c = this.charCodeAt(j)
                            score -= ((c == 32 || c == 9) ? 1 : 0.15)
                        }
                    } else {
                        score -= index
                    }
                }

                score += remaining_score * next_string.length
                score /= this.length;
                return score
            }
        }

        for (var i = 0; i < abbreviation.length;  i++) { //Cangee 
            var sub_abbreviation = abbreviation.substring(i, abbreviation.length-1) //Change
            var index = this.indexOf(sub_abbreviation)


            if (index < 0) continue;
            if (index + abbreviation.length > this.length + offset) continue;

            var next_string = this.substring(index + sub_abbreviation.length)
            var next_abbreviation = null

            if (i >= abbreviation.length)
                next_abbreviation = ''
            else
                next_abbreviation = abbreviation.substring(i)

            var remaining_score = next_string.score(next_abbreviation, offset + index)

            if (remaining_score > 0) {
                var score = this.length - next_string.length;

                if (index != 0) {
                    var j = 0;

                    var c = this.charCodeAt(index - 1)
                    if (c == 32 || c == 9) {
                        for (var j = (index - 2); j >= 0; j--) {
                            c = this.charCodeAt(j)
                            score -= ((c == 32 || c == 9) ? 1 : 0.15)
                        }
                    } else {
                        score -= index
                    }
                }

                score += remaining_score * next_string.length
                score /= this.length;
                return score
            }
        }

        return 0.0
    }

关于jquery - 用于柔性匹配的双向 Quicksilver 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4575140/

相关文章:

javascript - 添加具有唯一 id 的动态输入字段以进行计数

Javascript:在 'select' 上运行函数 'option'/How to stop setInterval(...);?

algorithm - 神经网络和IDS

python - 如何使用for循环向后打印字符串

python - 如何从 while 循环中反转整数值?

jquery - 在 NodeJS 上使用 jQuery 的函数 css()

c# - IE 缓存从服务器返回的旧数据

algorithm - 优化数据过滤和计算方式

algorithm - 什么是超越贪婪算法的有效方法

c++ - 使用递归反转字符串在 C 中有效,但在 C++ 中无效