| 1 |
// Copyright 2006 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Basic class for matching words in an array. |
| 17 |
* |
| 18 |
*/ |
| 19 |
|
| 20 |
|
| 21 |
goog.provide('goog.ui.ac.ArrayMatcher'); |
| 22 |
|
| 23 |
goog.require('goog.string'); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Basic class for matching words in an array |
| 29 |
* @constructor |
| 30 |
* @param {Array<?>} rows Dictionary of items to match. Can be objects if they |
| 31 |
* have a toString method that returns the value to match against. |
| 32 |
* @param {boolean=} opt_noSimilar if true, do not do similarity matches for the |
| 33 |
* input token against the dictionary. |
| 34 |
*/ |
| 35 |
goog.ui.ac.ArrayMatcher = function(rows, opt_noSimilar) { |
| 36 |
this.rows_ = rows || []; |
| 37 |
this.useSimilar_ = !opt_noSimilar; |
| 38 |
}; |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* Replaces the rows that this object searches over. |
| 43 |
* @param {Array<?>} rows Dictionary of items to match. |
| 44 |
*/ |
| 45 |
goog.ui.ac.ArrayMatcher.prototype.setRows = function(rows) { |
| 46 |
this.rows_ = rows || []; |
| 47 |
}; |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* Function used to pass matches to the autocomplete |
| 52 |
* @param {string} token Token to match. |
| 53 |
* @param {number} maxMatches Max number of matches to return. |
| 54 |
* @param {Function} matchHandler callback to execute after matching. |
| 55 |
* @param {string=} opt_fullString The full string from the input box. |
| 56 |
*/ |
| 57 |
goog.ui.ac.ArrayMatcher.prototype.requestMatchingRows = function( |
| 58 |
token, maxMatches, matchHandler, opt_fullString) { |
| 59 |
|
| 60 |
var matches = this.useSimilar_ ? |
| 61 |
goog.ui.ac.ArrayMatcher.getMatchesForRows(token, maxMatches, this.rows_) : |
| 62 |
this.getPrefixMatches(token, maxMatches); |
| 63 |
|
| 64 |
matchHandler(token, matches); |
| 65 |
}; |
| 66 |
|
| 67 |
|
| 68 |
/** |
| 69 |
* Matches the token against the specified rows, first looking for prefix |
| 70 |
* matches and if that fails, then looking for similar matches. |
| 71 |
* |
| 72 |
* @param {string} token Token to match. |
| 73 |
* @param {number} maxMatches Max number of matches to return. |
| 74 |
* @param {!Array<?>} rows Rows to search for matches. Can be objects if they |
| 75 |
* have a toString method that returns the value to match against. |
| 76 |
* @return {!Array<?>} Rows that match. |
| 77 |
*/ |
| 78 |
goog.ui.ac.ArrayMatcher.getMatchesForRows = function(token, maxMatches, rows) { |
| 79 |
var matches = |
| 80 |
goog.ui.ac.ArrayMatcher.getPrefixMatchesForRows(token, maxMatches, rows); |
| 81 |
|
| 82 |
if (matches.length == 0) { |
| 83 |
matches = goog.ui.ac.ArrayMatcher.getSimilarMatchesForRows( |
| 84 |
token, maxMatches, rows); |
| 85 |
} |
| 86 |
return matches; |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
/** |
| 91 |
* Matches the token against the start of words in the row. |
| 92 |
* @param {string} token Token to match. |
| 93 |
* @param {number} maxMatches Max number of matches to return. |
| 94 |
* @return {!Array<?>} Rows that match. |
| 95 |
*/ |
| 96 |
goog.ui.ac.ArrayMatcher.prototype.getPrefixMatches = function( |
| 97 |
token, maxMatches) { |
| 98 |
return goog.ui.ac.ArrayMatcher.getPrefixMatchesForRows( |
| 99 |
token, maxMatches, this.rows_); |
| 100 |
}; |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Matches the token against the start of words in the row. |
| 105 |
* @param {string} token Token to match. |
| 106 |
* @param {number} maxMatches Max number of matches to return. |
| 107 |
* @param {!Array<?>} rows Rows to search for matches. Can be objects if they |
| 108 |
* have |
| 109 |
* a toString method that returns the value to match against. |
| 110 |
* @return {!Array<?>} Rows that match. |
| 111 |
*/ |
| 112 |
goog.ui.ac.ArrayMatcher.getPrefixMatchesForRows = function( |
| 113 |
token, maxMatches, rows) { |
| 114 |
var matches = []; |
| 115 |
|
| 116 |
if (token != '') { |
| 117 |
var escapedToken = goog.string.regExpEscape(token); |
| 118 |
var matcher = new RegExp('(^|\\W+)' + escapedToken, 'i'); |
| 119 |
|
| 120 |
for (var i = 0; i < rows.length && matches.length < maxMatches; i++) { |
| 121 |
var row = rows[i]; |
| 122 |
if (String(row).match(matcher)) { |
| 123 |
matches.push(row); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
return matches; |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Matches the token against similar rows, by calculating "distance" between the |
| 133 |
* terms. |
| 134 |
* @param {string} token Token to match. |
| 135 |
* @param {number} maxMatches Max number of matches to return. |
| 136 |
* @return {!Array<?>} The best maxMatches rows. |
| 137 |
*/ |
| 138 |
goog.ui.ac.ArrayMatcher.prototype.getSimilarRows = function(token, maxMatches) { |
| 139 |
return goog.ui.ac.ArrayMatcher.getSimilarMatchesForRows( |
| 140 |
token, maxMatches, this.rows_); |
| 141 |
}; |
| 142 |
|
| 143 |
|
| 144 |
/** |
| 145 |
* Matches the token against similar rows, by calculating "distance" between the |
| 146 |
* terms. |
| 147 |
* @param {string} token Token to match. |
| 148 |
* @param {number} maxMatches Max number of matches to return. |
| 149 |
* @param {!Array<?>} rows Rows to search for matches. Can be objects |
| 150 |
* if they have a toString method that returns the value to |
| 151 |
* match against. |
| 152 |
* @return {!Array<?>} The best maxMatches rows. |
| 153 |
*/ |
| 154 |
goog.ui.ac.ArrayMatcher.getSimilarMatchesForRows = function( |
| 155 |
token, maxMatches, rows) { |
| 156 |
var results = []; |
| 157 |
|
| 158 |
for (var index = 0; index < rows.length; index++) { |
| 159 |
var row = rows[index]; |
| 160 |
var str = token.toLowerCase(); |
| 161 |
var txt = String(row).toLowerCase(); |
| 162 |
var score = 0; |
| 163 |
|
| 164 |
if (txt.indexOf(str) != -1) { |
| 165 |
score = parseInt((txt.indexOf(str) / 4).toString(), 10); |
| 166 |
|
| 167 |
} else { |
| 168 |
var arr = str.split(''); |
| 169 |
|
| 170 |
var lastPos = -1; |
| 171 |
var penalty = 10; |
| 172 |
|
| 173 |
for (var i = 0, c; c = arr[i]; i++) { |
| 174 |
var pos = txt.indexOf(c); |
| 175 |
|
| 176 |
if (pos > lastPos) { |
| 177 |
var diff = pos - lastPos - 1; |
| 178 |
|
| 179 |
if (diff > penalty - 5) { |
| 180 |
diff = penalty - 5; |
| 181 |
} |
| 182 |
|
| 183 |
score += diff; |
| 184 |
|
| 185 |
lastPos = pos; |
| 186 |
} else { |
| 187 |
score += penalty; |
| 188 |
penalty += 5; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if (score < str.length * 6) { |
| 194 |
results.push({str: row, score: score, index: index}); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
results.sort(function(a, b) { |
| 199 |
var diff = a.score - b.score; |
| 200 |
if (diff != 0) { |
| 201 |
return diff; |
| 202 |
} |
| 203 |
return a.index - b.index; |
| 204 |
}); |
| 205 |
|
| 206 |
var matches = []; |
| 207 |
for (var i = 0; i < maxMatches && i < results.length; i++) { |
| 208 |
matches.push(results[i].str); |
| 209 |
} |
| 210 |
|
| 211 |
return matches; |
| 212 |
}; |