Bird
Raised Fist0
NLPml~20 mins

Tokenization in spaCy in NLP - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
spaCy Tokenization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of spaCy Tokenization Code
What is the output of the following code snippet that uses spaCy to tokenize a sentence?
NLP
import spacy
nlp = spacy.blank('en')
doc = nlp('Hello world! How are you?')
tokens = [token.text for token in doc]
print(tokens)
A['Hello', 'world', '!', 'How', 'are', 'you', '?']
B['Hello world!', 'How are you?']
C['Hello', 'world!', 'How', 'are', 'you?']
D['Hello', 'world', 'How', 'are', 'you']
Attempts:
2 left
💡 Hint
Think about how spaCy splits punctuation from words by default.
🧠 Conceptual
intermediate
1:30remaining
spaCy Tokenizer Behavior on Contractions
Which statement correctly describes how spaCy tokenizes contractions like "don't" by default?
AIt removes the apostrophe and returns 'dont' as one token.
BIt keeps "don't" as a single token.
CIt splits "don't" into three tokens: 'do', 'n', and 't'.
DIt splits "don't" into two tokens: 'do' and 'n't'.
Attempts:
2 left
💡 Hint
Think about how spaCy handles common English contractions.
Hyperparameter
advanced
2:00remaining
Changing spaCy Tokenizer Behavior
Which spaCy component or method would you customize to change how tokens are split, for example to keep 'New York' as one token?
AModify the tokenizer exceptions or add special cases to the tokenizer.
BChange the pipeline's tagger component settings.
CAdjust the parser's dependency rules.
DModify the lemmatizer's dictionary.
Attempts:
2 left
💡 Hint
Token splitting is controlled before tagging or parsing.
Metrics
advanced
2:00remaining
Evaluating Tokenization Accuracy
You have a gold standard tokenization and a spaCy tokenizer output. Which metric best measures how well spaCy tokenized the text compared to the gold standard?
APerplexity of the tokenizer output.
BToken-level F1 score comparing spaCy tokens to gold tokens.
CSentence-level BLEU score.
DAccuracy of part-of-speech tags.
Attempts:
2 left
💡 Hint
Think about comparing sets of tokens for overlap.
🔧 Debug
expert
2:30remaining
Identifying Tokenization Bug in spaCy Customization
You added a special case to spaCy's tokenizer to keep 'San Francisco' as one token, but after running, it still splits into two tokens. What is the most likely cause?
NLP
import spacy
from spacy.symbols import ORTH

nlp = spacy.blank('en')
special_case = [{ORTH: 'San Francisco'}]
nlp.tokenizer.add_special_case('San Francisco', special_case)
doc = nlp('I visited San Francisco last year.')
tokens = [token.text for token in doc]
print(tokens)
AThe ORTH symbol is incorrect; it should be LEMMA.
BThe tokenizer needs to be rebuilt after adding special cases.
CThe special case should be a list of dicts with separate tokens, not a single dict with the full phrase.
DThe blank model 'en' does not support special cases.
Attempts:
2 left
💡 Hint
Check the format of the special case argument.

Practice

(1/5)
1. What does tokenization do in spaCy?
easy
A. It splits text into smaller pieces called tokens.
B. It trains a machine learning model.
C. It translates text into another language.
D. It visualizes text data.

Solution

  1. Step 1: Understand tokenization concept

    Tokenization means breaking text into smaller parts called tokens, like words or punctuation.
  2. Step 2: Relate to spaCy functionality

    spaCy uses tokenization to prepare text for analysis by splitting it into tokens.
  3. Final Answer:

    It splits text into smaller pieces called tokens. -> Option A
  4. Quick Check:

    Tokenization = splitting text [OK]
Hint: Tokenization means breaking text into tokens [OK]
Common Mistakes:
  • Confusing tokenization with training models
  • Thinking tokenization translates text
  • Assuming tokenization visualizes data
2. Which of the following is the correct way to load the English model in spaCy for tokenization?
easy
A. import spacy; nlp = spacy.tokenize('en')
B. import spacy; nlp = spacy.load('en_core_web_sm')
C. import spacy; nlp = spacy.model('english')
D. import spacy; nlp = spacy.load_model('english')

Solution

  1. Step 1: Recall spaCy model loading syntax

    spaCy loads models using spacy.load with the model name as a string.
  2. Step 2: Identify correct model name and function

    The English small model is 'en_core_web_sm' and loaded by spacy.load('en_core_web_sm').
  3. Final Answer:

    import spacy; nlp = spacy.load('en_core_web_sm') -> Option B
  4. Quick Check:

    Use spacy.load('model_name') to load models [OK]
Hint: Use spacy.load('model_name') to load models [OK]
Common Mistakes:
  • Using spacy.tokenize instead of spacy.load
  • Wrong model names like 'english' instead of 'en_core_web_sm'
  • Using non-existent functions like load_model
3. What will be the output tokens list from this code snippet?
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp('Hello, world!')
tokens = [token.text for token in doc]
print(tokens)
medium
A. ['Hello', ',', 'world', '!']
B. ['Hello,', 'world!']
C. ['Hello world']
D. ['Hello', 'world!']

Solution

  1. Step 1: Understand spaCy tokenization behavior

    spaCy splits punctuation from words, so commas and exclamation marks become separate tokens.
  2. Step 2: Analyze the given text 'Hello, world!'

    Tokens will be 'Hello', ',', 'world', and '!' separately.
  3. Final Answer:

    ['Hello', ',', 'world', '!'] -> Option A
  4. Quick Check:

    spaCy separates punctuation as tokens [OK]
Hint: Remember spaCy splits punctuation into separate tokens [OK]
Common Mistakes:
  • Keeping punctuation attached to words
  • Combining words into one token
  • Ignoring punctuation tokens
4. Identify the error in this spaCy tokenization code:
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp('Test sentence.')
for token in doc:
print(token.text)
medium
A. The token.text attribute does not exist.
B. Wrong model name used in spacy.load.
C. Missing indentation for print inside the for loop.
D. The variable 'doc' is not defined.

Solution

  1. Step 1: Check Python syntax for loops

    Python requires the code inside a for loop to be indented properly.
  2. Step 2: Inspect the given code

    The print statement is not indented under the for loop, causing an IndentationError.
  3. Final Answer:

    Missing indentation for print inside the for loop. -> Option C
  4. Quick Check:

    Indent loop body code in Python [OK]
Hint: Indent code inside loops to avoid errors [OK]
Common Mistakes:
  • Ignoring Python indentation rules
  • Assuming model name is wrong
  • Thinking token.text is invalid
5. You want to tokenize a sentence but keep contractions like "don't" as one token using spaCy. Which approach is best?
hard
A. Use the default spaCy tokenizer without changes.
B. Split contractions manually after tokenization.
C. Replace contractions with full words before tokenization.
D. Modify the tokenizer exceptions to keep contractions as single tokens.

Solution

  1. Step 1: Understand spaCy's default tokenizer behavior

    By default, spaCy splits contractions like "don't" into two tokens: 'do' and "n't".
  2. Step 2: Identify how to keep contractions as one token

    Modifying tokenizer exceptions allows spaCy to treat contractions as single tokens.
  3. Final Answer:

    Modify the tokenizer exceptions to keep contractions as single tokens. -> Option D
  4. Quick Check:

    Customize tokenizer exceptions to control token splits [OK]
Hint: Change tokenizer exceptions to keep contractions whole [OK]
Common Mistakes:
  • Using default tokenizer expecting contractions as one token
  • Splitting contractions manually after tokenization
  • Replacing contractions before tokenization unnecessarily