Skip to content

Commit

Permalink
Use *all* rated documents for ideal DCG computation. (#1242)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-fisher authored Feb 25, 2025
1 parent 2da6a30 commit 0e92b09
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
8 changes: 8 additions & 0 deletions db/migrate/20250225162317_sync_communal_scorer_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class SyncCommunalScorerCode < ActiveRecord::Migration[8.0]
def change
Scorer.communal.each do |scorer|
file_name = scorer.name.downcase + '.js'
scorer.update( code: File.readlines("./db/scorers/#{file_name}",'\n').join('\n'))
end
end
end
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion db/scorers/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ eachDocWithRating(function(doc) {
if (doc.rating > 0) {
totalRel++;
}
}, bestDocs.length);
});

// AP is the sum of the precision points divided by the total
// number of relevant documents
Expand Down
41 changes: 20 additions & 21 deletions db/scorers/[email protected]
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
var k = 10 // @Rank
var missing_rating = 0; // pessimistic assumption

var ideal = topRatings(k) // could return less than k if less than k docs have ratings
var scores = Array(k);
for (var i = 0; i < k; i++) {
if (!ideal[i]) {
ideal[i] = missing_rating;
const topK = 10 // @Rank
const missing_rating = 0;
const scores = Array(topK);
const idealScores = []
eachDocWithRating(function(doc) {
idealScores.push(doc.rating)
})
idealScores.sort(function(a,b) { return b - a; });
for (var i = 0; i < topK; i++) {
if (!idealScores[i]) {
idealScores[i] = missing_rating;
}
if (hasDocRating(i)) {
scores[i] = (docRating(i));
} else {
scores[i] = missing_rating;
}
}

function DCG(vals, k) {
var dcg = 0;
for (var i = 0; i < k; i++) {
var d = Math.log2(i + 2);
var n = Math.pow(2, vals[i]) - 1;
dcg += d ? (n / d) : 0;
let dcg = 0;
for (var j = 0; j < k; j++) {
const den = Math.log2(j + 2);
const num = Math.pow(2, vals[j]) - 1;
dcg += den ? (num / den) : 0;
}
return dcg;
}

function nDCG(vals, ideal, k) {
var n = DCG(vals, k);
var d = DCG(ideal, k);
return d ? (n / d) : 0;
const num = DCG(vals, k);
const den = DCG(ideal, ideal.length);
return den ? (num / den) : 0;
}

setScore(nDCG(scores, ideal, k));

setScore(nDCG(scores, idealScores, topK));

0 comments on commit 0e92b09

Please sign in to comment.