Lowercase & Whitespace Word Tokenizer

Basics
BasicsTokenizationNLPOpenAIAnthropicGoogle

Problem

Implement tokenize_words(text) that lowercases text, strips leading/trailing whitespace, and splits on any run of whitespace (spaces, tabs, newlines) into a list of clean word tokens.

Why It Matters: Pre-tokenization and whitespace normalization are the very first steps before BPE subword encoding, BM25 lexical search, and vocabulary construction.

Function Signature

def tokenize_words(text: str) -> list[str]:
    """Lowercase and split text on whitespace into tokens."""

Sample Input & Output

Example 1: Extra Spaces & Mixed Case

Input:

text = "  Hello   LLM   World!  "

Expected Output:

["hello", "llm", "world!"]

Explanation: Lowercasing normalizes casing and .split() collapses arbitrary runs of whitespace cleanly.

Example 2: Tabs, Newlines & Empty String

Input:

text = "RAG\tuses\n  vectors"  |  text = "   "

Expected Output:

["rag", "uses", "vectors"]  |  []

Explanation: Calling .split() with no argument handles tabs/newlines and returns [] for whitespace-only input.

Constraints & Edge Cases

  • Return a list[str] of lowercase tokens.
  • Empty or whitespace-only input returns [].
  • Use text.lower().split() (NOT .split(' '), which leaves empty string tokens on multiple spaces).
  • Do not strip punctuation unless explicitly requested.

Constraints & Evaluation Criteria

  • Return a `list[str]` of lowercase tokens.
  • Empty or whitespace-only input returns `[]`.

Hints

Python 3
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
'  Hello   LLM   World! '
Expected Output / Criteria
['hello', 'llm', 'world!']