v9.1.1
Menu

Keyword Answer Extractor

easyFREEQuestion AnsweringNLP 3: Advanced

Given a context paragraph and a question, find the sentence in the context that has the most keyword overlap with the question. Return that sentence as the answer.

Input format:

  • Line 1: The context (sentences separated by periods)
  • Line 2: The question

Keywords are all words (lowercased) except common stop words: {"the", "a", "an", "is", "are", "was", "were", "in", "on", "at", "to", "for", "of", "and", "or", "it", "this", "that", "what", "which", "who", "where", "when", "how", "do", "does", "did"}.

If there's a tie, return the first matching sentence.

Example

Input
Cats are popular pets. Dogs are loyal animals. Fish live in water.
What animals are loyal
Output
Dogs are loyal animals
Reasoning

Step 1: Extract question keywords (minus stop words) "what animals are loyal" → keywords: {"animals", "loyal"}

Step 2: Count keyword overlap per sentence "Cats are popular pets" → overlap: 0 "Dogs are loyal animals" → overlap: {"loyal", "animals"} = 2 "Fish live in water" → overlap: 0

Step 3: Return best match "Dogs are loyal animals" has highest overlap (2)

Constraints

  • Sentences separated by periods
  • Case-insensitive matching
  • Stop words excluded from keyword matching
  • Output: The best matching sentence (stripped of leading/trailing whitespace)

Learn this in

More from NLP 3: Advanced

solution.py

Test results

Sign in to run your code against the test cases.