Quepid NDCG Scorer v3

OpenSource Connections Quepid Normalized Discounted Cumulative Gain (NDCG) scorer

by Bertrand Rigaldies

HTML

<!DOCTYPE html>
<html>

  <head>
    <title>Quepid NDCG Calculator</title>
    <script>
      $(document).ready(function() {
        document.getElementById("ratings").innerHTML = mockedQuepidApi.getDocs();

        const idealRatings = []
        const count = mockedQuepidApi.getBestDocs().length;
        for (let i = 0; i < count; i++) {
          idealRatings.push(mockedQuepidApi.getBestDocs()[i].rating)
        }
        document.getElementById("idealRatings").innerHTML = idealRatings;
        document.getElementById("rankPosition").innerHTML = rankPosition;
        document.getElementById("ratingScaleMax").innerHTML = ratingScaleMax;
      });

    </script>
  </head>

  <body>

    <h2>NDCG Calculator</h2>

    <span>This NDCG calculator is implemented according to the spcifications provided in this Wikipedia article: <a href="https://en.wikipedia.org/wiki/Discounted_cumulative_gain" target="_blank">Discounted Cumulative Gain</a></span>

    <h3>Input:</h3>

    <h4>Actual Ratings:</h4>
    <p id="ratings"></p>

    <h4>Ideal Ratings:</h4>
    <p id="idealRatings"></p>

    <h4>Ranking Position 'p':</h4>
    <p id="rankPosition"></p>

    <h4>Rating scale max:</h4>
    <p id="ratingScaleMax"></p>

    <button onclick="myFunction()">Compute NDCG</button>

    <p id="ndcg_score"></p>

    <script>
      function myFunction() {
        document.getElementById("ndcg_score").innerHTML = calculate_ndcg();
      }

    </script>

  </body>

</html>

CSS

body {
  background: LightSteelBlue;
}

JavaScript

// Use load type "No Wrap in head" for the JS code below
// to be accessible from the HTML above.

// From the example in the wikipedia article
// https://en.wikipedia.org/wiki/Discounted_cumulative_gain

// Un-comment to catch sloppy script.
// Comment before deploying to Quepid.
// 'use strict';

// --------------------------------------------------------------------
// QUEPID API and scorer's runtime-context objects MOCKS (BEGIN).
// --------------------------------------------------------------------

// Rank position:
const rankPosition = 10

// Rating scale max
const ratingScaleMax = 3

// Wrap the mocked Quepid objects and API in a namespace
let mockedQuepidApi = {};

(function(context) {

  // List of docs/ratings on page #1 (e.g., 6 with the Wikipedia example)
  const _docs = [3, 2, 3, 0, 1, 2];

  // List of best docs: Descending order of ratings
  const _bestDocs = [{
    rating: 3
  }, {
    rating: 3
  }, {
    rating: 3
  }, {
    rating: 2
  }, {
    rating: 2
  }, {
    rating: 2
  }, {
    rating: 1
  }];

  context.getDocs = function() {
    return _docs;
  }
  
  context.getBestDocs = function() {
    return _bestDocs;
  }

  // Document's rating accessor
  context.docRating = function(position) {
    // from the
    return _docs[position];
  }

  // Has a ranking position been rated?
  context.hasDocRating = function(position) {
    return position >= 0 && position < _docs.length;
  }

})(mockedQuepidApi);

// Callable from the HTML snippet above.
function calculate_ndcg() {
  return NDCG_scorer.ndcg(mockedQuepidApi, rankPosition, ratingScaleMax);
}

function setScore(ndcg) {
  console.log("NDCG Score returned to Quepid (Scaled to [1-100]: " + ndcg * 100)
}

// --------------------------------------------------------------------
// QUEPID API and scorer's runtime-context objects MOCKS (END).
// --------------------------------------------------------------------

// ***************************************************
// DELETE THE MOCKING...