Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions