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

Weighted Combination

Normalized scores ready? Let's combine them. We'll use the following formula to create a new weighted hybrid score based on the BM25 (keyword) and semantic scores:

def hybrid_score(bm25_score: float, semantic_score: float, alpha: float = 0.5) -> float:
    return alpha * bm25_score + (1 - alpha) * semantic_score

alpha (or "α") is just a constant that we can use to dynamically control the weighting between the two scores:

α = 1.0: [████████████████████] 100% Keyword
α = 0.7: [██████████████------] 70% Keyword, 30% Semantic
α = 0.5: [██████████----------] 50/50 Split
α = 0.2: [████----------------] 20% Keyword, 80% Semantic
α = 0.0: [--------------------] 100% Semantic

As the developer, we want to choose an alpha that seems to work well for the query type. For example:

Query Type Example Chosen Alpha Reason
Exact match "The Revenant" 0.8 Title search needs keywords
Conceptual "family movies" 0.2 Meaning matters more
Mixed "2015 comedies" 0.5 Both year AND concept

This is why it's so important to tune your search system's constants based on the types of data and queries you're working with in your application! It's not a one-size-fits-all solution, but building configurability into your system allows you to adjust it as needed.

Assignment

Build hybrid search with a configurable alpha.

  1. 1. Paddington
      Hybrid Score: 1.000
      BM25: 1.000, Semantic: 1.000
      Deep in the rainforests of Peru, a young bear lives peacefully with his Aunt Lucy and Uncle Pastuzo,...
    2. The Indian in the Cupboard
      Hybrid Score: 0.943
      BM25: 0.966, Semantic: 0.850
      On his ninth birthday, Omri receives an old cupboard from his brother Gillon (Vincent Kartheiser) an...
    

Run and submit the CLI tests.