idf.js 653 B

123456789101112131415161718192021
  1. /**
  2. * A function to calculate the inverse document frequency for
  3. * a posting. This is shared between the builder and the index
  4. *
  5. * @private
  6. * @param {object} posting - The posting for a given term
  7. * @param {number} documentCount - The total number of documents.
  8. */
  9. lunr.idf = function (posting, documentCount) {
  10. var documentsWithTerm = 0
  11. for (var fieldName in posting) {
  12. if (fieldName == '_index') continue // Ignore the term index, its not a field
  13. documentsWithTerm += Object.keys(posting[fieldName]).length
  14. }
  15. var x = (documentCount - documentsWithTerm + 0.5) / (documentsWithTerm + 0.5)
  16. return Math.log(1 + Math.abs(x))
  17. }