We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

BM25 Search

In the last chapter we implemented TF-IDF, and in this chapter we've made the key improvements needed for BM25 search:

  • More stable IDF calculation
  • Term frequency saturation
  • Document length normalization

Now we're ready to put it all together to implement BM25.

BM25 Search Algorithm

The BM25 formula multiplies TF by IDF, like TF-IDF, but it uses the improved versions we've built in the last few lessons:

BM25 = bm25_tf * bm25_idf

This gives us a score for one document and one term. To calculate the full BM25 score for a query, we sum the scores for each query term:

  1. Tokenize the query.
  2. Calculate the BM25 score for each query token in a given document, and sum them.
  3. Do this for every document.
  4. Sort documents by score (highest first).
  5. Return the top N documents.

Assignment

  1. bm25search_parser = subparsers.add_parser(
        "bm25search", help="Search movies using full BM25 scoring"
    )
    bm25search_parser.add_argument("query", type=str, help="Search query")
    
  2. 1. (15) The Adventures of Mowgli - Score: 7.79
    2. (11342) Gakuen Alice - Score: 7.42
    3. (30) Day of the Animals - Score: 7.21
    4. (5542) Candy - Score: 7.07
    5. (3395) Life of Pi - Score: 7.05
    

    The number in parentheses is the document ID.

Run and submit the CLI tests.