Introduction to Question Answering

Batuhan Şenoğlu
3 min readFeb 24, 2023

--

Question Answering (QA) is a subfield of Natural Language Processing (NLP) that involves developing models that can automatically answer questions posed in natural language. QA has become an increasingly important area of research in recent years, with applications in areas such as customer support, chatbots, and virtual assistants.

The goal of QA is to build models that can take a question in natural language and produce a precise answer in natural language. QA systems typically consist of several components, including a question parser, a knowledge base, and an answer generator.

Types of QA

There are several types of QA systems, each with its own strengths and weaknesses. Some of the most common types of QA systems include:

  1. Factoid QA: Factoid QA systems are designed to answer questions that can be answered with a single fact, such as “What is the capital of France?” or “When was the Eiffel Tower built?”
  2. List QA: List QA systems are designed to answer questions that require a list of facts as the answer, such as “What are the countries in the European Union?”
  3. Definition QA: Definition QA systems are designed to answer questions that require a definition, such as “What is machine learning?”
  4. Scenario QA: Scenario QA systems are designed to answer questions that require a narrative or scenario as the answer, such as “What would happen if the United States invaded Canada?”

Math Behind QA

QA systems typically use a combination of machine learning and natural language processing techniques to generate answers. One popular approach to QA is to use a technique called Information Retrieval (IR), which involves retrieving relevant documents from a knowledge base and extracting answers from them.

In IR-based QA, the system first parses the question and identifies the relevant concepts and keywords. It then searches the knowledge base for documents that contain those concepts and keywords, and ranks the documents based on their relevance to the question. Finally, it extracts the answer from the top-ranked document.

The relevance ranking is typically done using a technique called cosine similarity. Cosine similarity measures the similarity between two vectors by computing the cosine of the angle between them. In QA, the vectors represent the question and the documents, and the cosine similarity score measures how similar the question is to each document.

Python Code for QA

Let’s now take a look at some Python code that demonstrates how to build a basic QA system using IR. We will use the TREC 2003 dataset, which consists of questions and their corresponding answers.

import pandas as pd
import numpy as np
import nltk
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

# Load the dataset
data = pd.read_csv('trec.csv')

# Preprocess the data
stop_words = set(stopwords.words('english'))
data['question'] = data['question'].apply(lambda x: ' '.join([word for word in x.split() if word not in stop_words]))

# Define the vectorizer
vectorizer = TfidfVectorizer()

# Vectorize the data
X = vectorizer.fit_transform(data['question'])

# Define the query
query = "What is the capital of France?"

# Vectorize the query
query_vec = vectorizer.transform([query])

# Compute the similarity scores
scores = cosine_similarity(X, query_vec)

# Get the index of the top-scoring question
index = np.argmax(scores)

# Get the answer
answer = data.iloc[index]['answer']

print(f"Answer: {answer}")

--

--