sort by words matched then by frequency
git-svn-id: http://svn.openlayers.org/trunk/openlayers@7098 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -98,9 +98,14 @@
|
||||
for(exIndex in dict) {
|
||||
var count = dict[exIndex];
|
||||
if(scores[exIndex]) {
|
||||
scores[exIndex] += count;
|
||||
if(scores[exIndex][word]) {
|
||||
scores[exIndex][word] += count;
|
||||
} else {
|
||||
scores[exIndex][word] = count;
|
||||
}
|
||||
} else {
|
||||
scores[exIndex] = count;
|
||||
scores[exIndex] = {};
|
||||
scores[exIndex][word] = count;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -111,9 +116,26 @@
|
||||
ex.score = scores[j];
|
||||
examples.push(ex);
|
||||
}
|
||||
// sort examples by descending score
|
||||
// sort examples by first by number of words matched, then
|
||||
// by word frequency
|
||||
examples.sort(function(a, b) {
|
||||
return (b.score - a.score);
|
||||
var cmp;
|
||||
var aWords = 0, bWords = 0;
|
||||
var aScore = 0, bScore = 0;
|
||||
for(var i in a.score) {
|
||||
aScore += a.score[i];
|
||||
aWords += 1;
|
||||
}
|
||||
for(var j in b.score) {
|
||||
bScore += b.score[j];
|
||||
bWords += 1;
|
||||
}
|
||||
if(aWords == bWords) {
|
||||
cmp = bScore - aScore;
|
||||
} else {
|
||||
cmp = bWords - aWords;
|
||||
}
|
||||
return cmp;
|
||||
});
|
||||
}
|
||||
listExamples(examples);
|
||||
|
||||
Reference in New Issue
Block a user