Complete the code to compute cosine similarity between query and documents.
similarity_scores = cosine_similarity(query_vector, [1])The cosine similarity compares the query vector to each document vector to find how similar they are.
Complete the code to select top k documents based on similarity scores.
top_k_indices = similarity_scores.argsort()[::-1][:[1]]
We select the top k documents by sorting similarity scores in descending order and taking the first k indices.
Fix the error in the Maximal Marginal Relevance (MMR) score calculation.
mmr_score = lambda doc, selected: lambda_param * similarity(doc, query) - (1 - lambda_param) * max(similarity(doc, s) for s in [1])
In MMR, we subtract the maximum similarity of the candidate document to already selected documents to reduce redundancy.
Fill both blanks to implement a hybrid retrieval score combining similarity and MMR.
hybrid_score = lambda doc, selected: alpha * similarity(doc, query) + (1 - alpha) * [1](doc, selected) selected_doc = max(candidates, key=lambda doc: [2](doc, selected))
The hybrid score combines similarity and MMR. We use mmr_score function and select the document with the highest hybrid_score.
Fill all three blanks to create a dictionary of document scores filtered by a threshold.
filtered_scores = {doc: [1] for doc, score in scores.items() if score [2] [3]This dictionary comprehension keeps documents with scores greater than a threshold and maps each document to its score.