2011-01-01 6 views
4

Ich verwende die flexselect jQuery plugin, um die Auswahl in großen <select> Listen zu vereinfachen, die die Quicksilver algorithm zu flex-match (Fuzzy-Match?) -Optionen verwendet.Zweirichtungs-Quicksilver-Algorithmus für Flex-Anpassung

Zum Beispiel: Eine Eingabe von 3gscam paßt schnell iPhone 3GS Camera, aber eine Eingabe von cam3gs nicht.

Gibt es eine modifizierte Version des Quicksilver-Algorithmus, die in beide Richtungen funktioniert, mit einer Vorliebe für Vorwärts-Übereinstimmungen?

Ein vorhandenes jQuery-Plugin wäre hilfreich, also muss ich nicht mein eigenes rollen.

Antwort

0

wir die Richtung sehen kann durch die for-Schleife von Länge zu 0 gesetzt, wenn wir jede Option nicht finden, wir den gleichen Code in die andere Richtung von 0 bis Länge

es ich hoffe, ich habe richtig

laufen kann
 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 
    } 
Verwandte Themen