0Pricing
NLP Academy · Lesson

Finding Answer Spans in Context

Start and end token prediction.

Finding Answer Spans in Context is a free NLP Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Span Mindset

Under the hood, extractive QA never writes text. It picks a span: a contiguous slice of the context defined by where it starts and ends.

Predicting Two Numbers

The model's real job is to predict two positions: the start token of the answer and the end token. The slice between them is the answer.

Tokens, Not Characters

Inside the model the context is split into tokens, not raw letters. Start and end are token indexes that get mapped back to characters.

A Score Per Position

For every token the model emits a start logit and an end logit. These scores say how likely each token begins or ends the answer.

Picking the Best Pair

The chosen span is the start and end pair with the highest combined score, with the rule that end never comes before start.

Slicing With Offsets

The pipeline returns character offsets so you can slice the original context yourself and recover the exact answer text.

answer = context[result["start"]:result["end"]]

Why Offsets Matter

Those offsets let you highlight the answer right inside the passage, which is great for showing users where a fact came from.

Limiting Answer Length

You can cap how long a span may be with max_answer_len. This stops the model from returning an entire sentence as the answer.

qa(question=q, context=c, max_answer_len=20)

Getting Several Candidates

Set top_k to return more than one candidate span. Reviewing a few options helps when the best answer is ambiguous.

qa(question=q, context=c, top_k=3)

Spans Must Be Contiguous

A span is always one continuous stretch of text. Extractive QA cannot stitch together words from different parts of the passage.

Spans Power Highlighting

Because answers are exact spans with offsets, you can highlight them in place, giving users a verifiable source for every reply. 🔦

Quick Check

How does the model decide what the answer is?

Recap

The model predicts start and end positions, picks the best valid pair, and offsets let you slice and highlight the exact answer. 📌

Frequently asked questions

Is the “Finding Answer Spans in Context” lesson free?

Yes — the full text of “Finding Answer Spans in Context” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.

What will I learn in “Finding Answer Spans in Context”?

Start and end token prediction. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start NLP Academy?

No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Finding Answer Spans in Context” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this NLP Academy lesson?

Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Extractive vs Generative QA
  2. Running a QA Pipeline
  3. Finding Answer Spans in Context
  4. Handling No-Answer and Long Docs
← Back to NLP Academy