#408Build a vocabularyEasy

Build a vocabulary

Background

Before text can become numbers, you fix a vocabulary: every distinct token gets a unique integer index. A simple, deterministic scheme is to sort the unique tokens and number them from 0.

Problem statement

Implement build_vocab(corpus) returning a dict mapping each unique token to an index, assigned in sorted order starting at 0.

Input

  • corpus — list of string tokens (may contain repeats).

Output

Returns a dict {token: index} covering each unique token once.

Examples

Example 1

Input:  corpus = ["cat", "dog", "cat"]
Output: {"cat": 0, "dog": 1}

Example 2 — sorted order

Input:  corpus = ["b", "a", "c"]
Output: {"a": 0, "b": 1, "c": 2}

Constraints

  • Deduplicate, sort the unique tokens, and assign indices 0, 1, 2, ….
  • Return a dict.

Notes

  • Real vocabularies add special tokens (<pad>, <unk>, <bos>, <eos>) and often cap the size by frequency, but the core idea is this token→index map.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Example — dedup and index in sorted order
  • Reference — sorted assignment
  • Sample — repeated single token