4

flexselect jQuery プラグインを使用して、大きな<select>リストでの選択を簡素化しています。これは、 Quicksilver アルゴリズムを使用してフレックス マッチ (ファジー マッチ?) オプションを使用します。

例:の入力は3gscamにすばやく一致iPhone 3GS Cameraしますが、 の入力は一致cam3gsしません。

前方一致を優先して、両方向で機能する Quicksilver アルゴリズムの修正版はありますか?

既存の jQuery プラグインがあれば役立つので、自分で作成する必要はありません。

4

1 に答える 1

0

方向がforループによって長さから0に設定されていることがわかります。オプションが見つからない場合は、同じコードを0から長さまで逆に実行できます。

私はそれを正しくやったと思います

     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
    }
于 2012-08-08T13:12:47.303 に答える