word_tokenize splits punctuation and contractions separately [OK]
Hint: word_tokenize splits punctuation and contractions separately [OK]
Common Mistakes:
Assuming punctuation stays attached to words
Ignoring splitting of contractions
Expecting a single string token
4. Identify the error in this tokenization code snippet:
import nltk
text = "Data Science is fun!"
tokens = nltk.word_tokenize(text)
print(tokens)
medium
A. word_tokenize is not a function in nltk
B. Missing import of word_tokenize from nltk.tokenize
C. text variable is not defined
D. print statement syntax error
Solution
Step 1: Check how word_tokenize is accessed
word_tokenize is inside nltk.tokenize module, not directly under nltk.
Step 2: Identify import mistake
Importing only nltk does not allow calling nltk.word_tokenize; must import from nltk.tokenize.
Final Answer:
Missing import of word_tokenize from nltk.tokenize -> Option B
Quick Check:
word_tokenize needs correct import [OK]
Hint: Import word_tokenize from nltk.tokenize, not just nltk [OK]
Common Mistakes:
Calling nltk.word_tokenize without proper import
Assuming all functions are directly under nltk
Ignoring module structure
5. You have a list of sentences and want to tokenize each sentence into words, then count total tokens. Which code correctly does this using nltk's word_tokenize?
hard
A. from nltk.tokenize import word_tokenize
sentences = ["I love data.", "Tokenization is key."]
tokens = word_tokenize(sentences)
print(len(tokens))
B. import nltk
sentences = ["I love data.", "Tokenization is key."]
tokens = nltk.word_tokenize(sentences)
print(len(tokens))
C. from nltk.tokenize import word_tokenize
sentences = "I love data. Tokenization is key."
tokens = word_tokenize(sentences)
print(tokens)
D. from nltk.tokenize import word_tokenize
sentences = ["I love data.", "Tokenization is key."]
tokens = [word_tokenize(s) for s in sentences]
total = sum(len(t) for t in tokens)
print(total)
Solution
Step 1: Understand input and function usage
word_tokenize expects a string, so to tokenize multiple sentences, tokenize each string separately.
Step 2: Analyze each option's approach
from nltk.tokenize import word_tokenize
sentences = ["I love data.", "Tokenization is key."]
tokens = [word_tokenize(s) for s in sentences]
total = sum(len(t) for t in tokens)
print(total) tokenizes each sentence in a list comprehension, then sums lengths correctly. Some options wrongly pass list to word_tokenize. from nltk.tokenize import word_tokenize
sentences = "I love data. Tokenization is key."
tokens = word_tokenize(sentences)
print(tokens) tokenizes a single string, not list.
Final Answer:
The correct code tokenizes each sentence and counts total tokens -> Option D
Quick Check:
Tokenize each sentence separately, then count tokens [OK]
Hint: Tokenize each sentence string separately, then count tokens [OK]
Common Mistakes:
Passing list directly to word_tokenize
Not summing token counts correctly
Tokenizing entire text as one string when list given
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.