0
0
NLPml~5 mins

Extractive QA concept in NLP

Choose your learning style9 modes available
Introduction

Extractive Question Answering (QA) helps find exact answers from a given text. It picks the right part of the text as the answer.

You want to find a specific fact from a document, like a date or name.
You have a paragraph and want to answer questions about it quickly.
You want a system to help users get quick answers from manuals or FAQs.
You need to build a chatbot that answers questions using a fixed text source.
You want to highlight the exact sentence or phrase that answers a question.
Syntax
NLP
model = ExtractiveQAModel()
answer = model.answer(question, context)

The question is what you want to know.

The context is the text where the answer is found.

Examples
The model finds "Paris, France" as the answer.
NLP
question = "Where is the Eiffel Tower located?"
context = "The Eiffel Tower is in Paris, France."
answer = model.answer(question, context)
The model extracts "Jane Austen" as the answer.
NLP
question = "Who wrote 'Pride and Prejudice'?"
context = "Jane Austen wrote 'Pride and Prejudice' in 1813."
answer = model.answer(question, context)
Sample Model

This code uses a ready-made model to find the answer in the context. It prints the answer and how confident the model is.

NLP
from transformers import pipeline

# Load a pre-trained extractive QA pipeline
qa_pipeline = pipeline('question-answering')

context = "The Statue of Liberty is located in New York Harbor. It was a gift from France."
question = "Where is the Statue of Liberty located?"

result = qa_pipeline(question=question, context=context)

print(f"Answer: {result['answer']}")
print(f"Score: {result['score']:.2f}")
OutputSuccess
Important Notes

Extractive QA only picks answers from the given text; it does not generate new information.

Good context quality helps the model find better answers.

Models may give a confidence score showing how sure they are about the answer.

Summary

Extractive QA finds exact answers inside a text.

It works by selecting a part of the context that answers the question.

It is useful for quick fact-finding from documents or chats.