gnome/extensions/web_search_dialog@awamper.gmail.com/utils.js
/**
 * Scores a string against another string.
 *  'Hello World'.score('he');     //=> 0.5931818181818181
 *  'Hello World'.score('Hello');  //=> 0.7318181818181818
 */
Scores a string against another string.
Scores a string against another string .
function string_score(string, abbreviation, fuzziness) {
  if (string == abbreviation) {return 1;}
  if(abbreviation == "" || string == "") {return 0;}
  var total_character_score = 0,
      abbreviation_length = abbreviation.length,
      string_length = string.length,
      start_of_string_bonus,
      abbreviation_score,
      fuzzies=1,
      final_score;
  for (var i = 0,
         character_score,
         index_in_string,
         c,
         index_c_lowercase,
         index_c_uppercase,
         min_index;
     i < abbreviation_length;
     ++i) {
    c = abbreviation.charAt(i);
    index_c_lowercase = string.indexOf(c.toLowerCase());
    index_c_uppercase = string.indexOf(c.toUpperCase());
    min_index = Math.min(index_c_lowercase, index_c_uppercase);
    index_in_string = (min_index > -1) ? min_index : Math.max(index_c_lowercase, index_c_uppercase);
    if (index_in_string === -1) { 
      if (fuzziness) {
        fuzzies += 1-fuzziness;
        continue;
      } else {
        return 0;
      }
    } else {
      character_score = 0.1;
    }
    if (string[index_in_string] === c) { 
      character_score += 0.1; 
    }
    if (index_in_string === 0) {
      character_score += 0.6;
      if (i === 0) {
        start_of_string_bonus = 1 
      }
    }
    else {
  if (string.charAt(index_in_string - 1) === ' ') {
    character_score += 0.8; 
  }
    }
    string = string.substring(index_in_string + 1, string_length);
    total_character_score += character_score;
  } 
  abbreviation_score = total_character_score / abbreviation_length;
  final_score = ((abbreviation_score * (abbreviation_length / string_length)) + abbreviation_score) / 2;
  final_score = final_score / fuzzies;
  if (start_of_string_bonus && (final_score + 0.15 < 1)) {
    final_score += 0.15;
  }
  return final_score;
}
function string_score ( string , abbreviation , fuzziness ) { if ( string == abbreviation ) { return 1 ; } if ( abbreviation == "" || string == "" ) { return 0 ; } var total_character_score = 0 , abbreviation_length = abbreviation . length , string_length = string . length , start_of_string_bonus , abbreviation_score , fuzzies = 1 , final_score ; for ( var i = 0 , character_score , index_in_string , c , index_c_lowercase , index_c_uppercase , min_index ; i < abbreviation_length ; ++ i ) { c = abbreviation . charAt ( i ) ; index_c_lowercase = string . indexOf ( c . toLowerCase ( ) ) ; index_c_uppercase = string . indexOf ( c . toUpperCase ( ) ) ; min_index = Math . min ( index_c_lowercase , index_c_uppercase ) ; index_in_string = ( min_index > - 1 ) ? min_index : Math . max ( index_c_lowercase , index_c_uppercase ) ; if ( index_in_string === - 1 ) { if ( fuzziness ) { fuzzies += 1 - fuzziness ; continue ; } else { return 0 ; } } else { character_score = 0.1 ; } if ( string [ index_in_string ] === c ) { character_score += 0.1 ; } if ( index_in_string === 0 ) { character_score += 0.6 ; if ( i === 0 ) { start_of_string_bonus = 1 } } else { if ( string . charAt ( index_in_string - 1 ) === ' ' ) { character_score += 0.8 ; } } string = string . substring ( index_in_string + 1 , string_length ) ; total_character_score += character_score ; } abbreviation_score = total_character_score / abbreviation_length ; final_score = ( ( abbreviation_score * ( abbreviation_length / string_length ) ) + abbreviation_score ) / 2 ; final_score = final_score / fuzzies ; if ( start_of_string_bonus && ( final_score + 0.15 < 1 ) ) { final_score += 0.15 ; } return final_score ; }
Scores a string against another string.
Scores a string against another string .
// If the string is equal to the abbreviation, perfect match. //if it's not a perfect match and is empty return 0 // Walk through abbreviation and add up scores. /* = 0*/ /* = 0*/ /* = ''*/ /* = 0*/ /* = 0*/ /* = 0*/ // Find the first case-insensitive match of a character. // Set base score for matching 'c'. // Same case bonus. // Consecutive letter & start-of-string Bonus // Increase the score when matching first character of the remainder of the string // If match is the first character of the string // & the first character of abbreviation, add a // start-of-string match bonus. //true; // Acronym Bonus // Weighing Logic: Typing the first character of an acronym is as if you // preceded it with two perfect character matches. // * Math.min(index_in_string, 5); // Cap bonus at 0.4 * 5 // Left trim the already matched part of the string // (forces sequential matching). // end of for loop // Uncomment to weigh smaller words higher. // return total_character_score / string_length; //percentage_of_matched_string = abbreviation_length / string_length; //word_score = abbreviation_score * percentage_of_matched_string; // Reduce penalty for longer strings. //final_score = (word_score + abbreviation_score) / 2;