我使用flexselect jQuery plugin来简化大型<select>
列表的选择,这些列表采用Quicksilver algorithm灵活匹配(模糊匹配?)选项。
例如: 3gscam
的输入与iPhone 3GS Camera
快速匹配,但cam3gs
的输入不匹配。
Quicksilver算法的修改版本是否适用于两个方向,并且优先选择前向匹配?
现有的jQuery插件会有所帮助,所以我不必自己动手。
答案 0 :(得分: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
}