Cosine Similarity
easyWhen a support chatbot answers from your product docs, it first picks which paragraphs to read: the system converts the question and every candidate paragraph into vectors (a vector is just the math name for one of those number lists) and looks for the paragraph whose list points the same direction. Cosine similarity is that direction check, and it is the single number that orders the results in every meaning-based search. Implement cosine_similarity(a, b): a and b are word-importance dicts like {"search": 2, "engine": 1}, meaning the word "search" carries weight 2 in that document and "engine" carries 1. Return the cosine similarity between them, rounded to 4 decimal places. Identical vectors return 1.0, vectors that share no words return 0.0, and a vector with no entries at all has no direction, so return 0.0 for it.
Run your code to see output