Bird
Raised Fist0
NLPml~20 mins

spaCy installation and models 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 Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding spaCy Model Sizes

Which spaCy model is the smallest and fastest but provides less detailed linguistic features?

Aen_core_web_md
Ben_core_web_sm
Cen_core_web_lg
Den_core_web_trf
Attempts:
2 left
💡 Hint

Think about the naming convention where 'sm' stands for small.

Predict Output
intermediate
2:00remaining
Output of spaCy Model Loading Code

What will be the output of the following code snippet?

NLP
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp('Apple is looking at buying U.K. startup for $1 billion')
print([(ent.text, ent.label_) for ent in doc.ents])
A[('Apple', 'ORG'), ('U.K.', 'GPE'), ('$1 billion', 'MONEY')]
B[('Apple', 'PERSON'), ('U.K.', 'ORG'), ('$1 billion', 'MONEY')]
C[('Apple', 'ORG'), ('U.K.', 'LOC'), ('$1 billion', 'MONEY')]
D[('Apple', 'ORG'), ('U.K.', 'GPE'), ('$1 billion', 'QUANTITY')]
Attempts:
2 left
💡 Hint

Look at common entity labels: ORG for organizations, GPE for geopolitical entities, MONEY for monetary values.

Model Choice
advanced
1:30remaining
Choosing the Best spaCy Model for Transformer-Based Accuracy

You want to use spaCy for a project that requires the highest accuracy using transformer models. Which model should you choose?

Aen_core_web_sm
Ben_core_web_md
Cen_core_web_lg
Den_core_web_trf
Attempts:
2 left
💡 Hint

Transformer models usually have 'trf' in their name.

Hyperparameter
advanced
1:30remaining
spaCy Pipeline Components Loading

When loading a spaCy model with spacy.load(), which parameter allows you to disable specific pipeline components to speed up processing?

Adisable=['parser', 'ner']
Bremove=['parser', 'ner']
Cskip=['parser', 'ner']
Dexclude=['parser', 'ner']
Attempts:
2 left
💡 Hint

Check spaCy documentation for the parameter name to disable components.

🔧 Debug
expert
2:00remaining
Error When Loading spaCy Model

What error will occur if you try to load a spaCy model that is not installed on your system using spacy.load('en_core_web_md')?

AValueError: Model name must be a string
BImportError: No module named 'en_core_web_md'
COSError: [E050] Can't find model 'en_core_web_md'. It doesn't seem to be a shortcut link, a Python package or a valid path.
DTypeError: spacy.load() missing 1 required positional argument
Attempts:
2 left
💡 Hint

Think about spaCy's error message when a model is missing.

Practice

(1/5)
1. What is the correct command to install spaCy using pip?
easy
A. pip install spacy-model
B. pip install spacy
C. python -m spacy install
D. pip download spacy

Solution

  1. Step 1: Understand pip installation command

    The standard way to install Python packages is using pip install package_name.
  2. Step 2: Identify spaCy package name

    The correct package name for spaCy is exactly spacy, so the command is pip install spacy.
  3. Final Answer:

    pip install spacy -> Option B
  4. Quick Check:

    pip install spacy = B [OK]
Hint: Use 'pip install spacy' to install the main package [OK]
Common Mistakes:
  • Using 'pip install spacy-model' which is incorrect
  • Trying 'python -m spacy install' which is invalid
  • Using 'pip download spacy' which only downloads, not installs
2. Which command correctly downloads the English small model for spaCy?
easy
A. python -m spacy download en_core_web_sm
B. pip install en_core_web_sm
C. spacy install en_core_web_sm
D. python spacy download en

Solution

  1. Step 1: Identify the correct download command format

    spaCy models are downloaded using python -m spacy download <model_name>.
  2. Step 2: Match the correct model name for English small

    The official English small model is named en_core_web_sm, so the full command is python -m spacy download en_core_web_sm.
  3. Final Answer:

    python -m spacy download en_core_web_sm -> Option A
  4. Quick Check:

    python -m spacy download en_core_web_sm = A [OK]
Hint: Use 'python -m spacy download model_name' to get models [OK]
Common Mistakes:
  • Trying to install models with pip instead of spacy download
  • Using incomplete model names like 'en' only
  • Using 'spacy install' which is not a valid command
3. What will be the output of this code snippet after loading the spaCy English model and processing text?
import spacy
nlp = spacy.load('en_core_web_sm')
doc = nlp('Hello world!')
print([(token.text, token.pos_) for token in doc])
medium
A. [('Hello', 'NOUN'), ('world', 'VERB'), ('!', 'PUNCT')]
B. [('Hello', 'VERB'), ('world', 'ADJ'), ('!', 'SYM')]
C. [('Hello', 'INTJ'), ('world', 'NOUN'), ('!', 'PUNCT')]
D. SyntaxError

Solution

  1. Step 1: Load the English model and process text

    The code loads the 'en_core_web_sm' model and processes the sentence 'Hello world!'.
  2. Step 2: Understand token parts of speech

    In spaCy, 'Hello' is tagged as interjection (INTJ), 'world' as noun (NOUN), and '!' as punctuation (PUNCT).
  3. Final Answer:

    [('Hello', 'INTJ'), ('world', 'NOUN'), ('!', 'PUNCT')] -> Option C
  4. Quick Check:

    Token POS tags match [('Hello', 'INTJ'), ('world', 'NOUN'), ('!', 'PUNCT')] [OK]
Hint: Remember 'Hello' is INTJ, 'world' is NOUN, '!' is PUNCT [OK]
Common Mistakes:
  • Confusing POS tags like 'Hello' as VERB
  • Expecting syntax error due to correct code
  • Mixing up punctuation tag as SYM instead of PUNCT
4. Identify the error in this code snippet for loading a spaCy model:
import spacy
nlp = spacy.load('en_core_web_md')
doc = nlp('Test sentence.')
print(doc.text)

Assuming the model was not downloaded yet.
medium
A. ModelNotFoundError because 'en_core_web_md' is not installed
B. SyntaxError due to wrong import
C. AttributeError on 'nlp' object
D. No error, code runs fine

Solution

  1. Step 1: Check if model is downloaded

    The code tries to load 'en_core_web_md' model, which must be downloaded first using spaCy's download command.
  2. Step 2: Understand error when model missing

    If the model is not installed, spaCy raises a ModelNotFoundError when calling spacy.load().
  3. Final Answer:

    ModelNotFoundError because 'en_core_web_md' is not installed -> Option A
  4. Quick Check:

    Missing model causes ModelNotFoundError [OK]
Hint: Always download models before loading to avoid ModelNotFoundError [OK]
Common Mistakes:
  • Assuming code runs without downloading model
  • Thinking import causes SyntaxError
  • Expecting AttributeError on nlp object
5. You want to process text in French using spaCy. Which steps correctly install and load the French model for use in your Python code?
hard
A. Run python -m spacy install fr_core_news_sm then nlp = spacy.load('fr_core_news_sm')
B. Run pip install fr_core_news_sm then nlp = spacy.load('fr')
C. Run python -m spacy download fr then nlp = spacy.load('fr_core_news_sm')
D. Run python -m spacy download fr_core_news_sm then nlp = spacy.load('fr_core_news_sm')

Solution

  1. Step 1: Download the correct French model

    The official French small model is named fr_core_news_sm, downloaded with python -m spacy download fr_core_news_sm.
  2. Step 2: Load the model in Python

    After downloading, load it with spacy.load('fr_core_news_sm') to process French text.
  3. Final Answer:

    Run python -m spacy download fr_core_news_sm then nlp = spacy.load('fr_core_news_sm') -> Option D
  4. Quick Check:

    Download full model name, then load same name [OK]
Hint: Download full model name, then load it exactly [OK]
Common Mistakes:
  • Trying to install model with pip instead of spacy download
  • Using short name 'fr' in download command
  • Using 'spacy install' which is invalid