Today you record how many times each term appears in each document. That count - the term frequency - is the raw signal every ranking formula later leans on.
Track and report the term frequency of a term within a document.
Presence tells you a document is relevant; frequency starts to tell you how
relevant. A document that says cat five times is, all else equal, more about
cats than one that says it once. That count is the term frequency (tf), and it
is the first ingredient of every scoring formula in the ranking chapter.
The postings list already dedupes ids, so it cannot hold counts. Keep the counts
beside it, keyed by term and document. A term absent from a document has frequency
zero - make sure your lookup returns 0 rather than raising, because ranking will
ask about many terms a given document does not contain.
# alongside the postings, keep a per-document countfor term in analyze(text):self._tf[(term, doc_id)] = self._tf.get((term, doc_id), 0) + 1def tf(self, term, doc_id):return self._tf.get((term, doc_id), 0)
Manning, Introduction to Information Retrieval - ch. 6.2.