Question
Write a function get_relevant which takes two arguments: docs, documents, in the form of a dictionary (keys and values are both strings) and query in
Write a function get_relevant which takes two arguments: docs, documents, in the form of a dictionary (keys and values are both strings) and query in the form of a list of strings, and return the most relevant document(s) in the form of a list in sorted order. Relevance is evaluated as a propotion of words in each document that appear in the query string. Your function should include an appropriate docstring, comments, and good variable names for readability. For example: In the case of docs being the dictionary {'doc1':'A cat sat on the mat', 'doc2':'A cat had a kitten', 'doc3':'I love ice cream'} and query being the list ['cat', 'cats', 'kitten'] , the fuction should return doc2 as it is the most relevant (doc1 has 1 word from a query and has 6 words, so its relevance score is 1/6 (~0.17), relevance of doc2 is 0.4, and relevance of doc3 is 0). If docs or query is empty, the function should return None. In the case of several documents having the same relevance, the function should return all of them if there is a non-zero relevance score, otherwise it should return an empty list. Some example calls to the function are:
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started