Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the NLTK library for part-of-speech tagging.
NLP
import nltk text = "I love learning AI" tokens = nltk.word_tokenize(text) pos_tags = nltk.[1](tokens) print(pos_tags)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong function name like tag_pos or posTag.
Forgetting to tokenize the text before tagging.
✗ Incorrect
The correct function to get part-of-speech tags in NLTK is pos_tag().
2fill in blank
mediumComplete the code to tokenize the sentence before tagging.
NLP
sentence = "She enjoys reading books" tokens = [1](sentence) print(tokens)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using sentence.split which does not handle punctuation well.
Using a non-existent function like sentence.tokenize.
✗ Incorrect
The nltk.word_tokenize() function splits the sentence into words (tokens).
3fill in blank
hardFix the error in the code to correctly tag the tokens.
NLP
tokens = ['They', 'are', 'playing', 'football'] pos_tags = nltk.pos_tag([1]) print(pos_tags)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling tokens as a function with parentheses.
Using incorrect syntax like tokens[].
✗ Incorrect
The pos_tag function expects a list of tokens, so pass the variable tokens directly.
4fill in blank
hardFill both blanks to create a dictionary of words and their POS tags.
NLP
tokens = ['He', 'runs', 'fast'] pos_tags = nltk.pos_tag(tokens) pos_dict = [1](pos_tags) print(pos_dict) # Access the POS tag of 'runs' print(pos_dict[[2]])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using list() instead of dict() to convert.
Accessing dictionary with wrong key like 'fast'.
✗ Incorrect
Use dict() to convert list of tuples to dictionary, then access the word 'runs' to get its tag.
5fill in blank
hardFill the blanks to filter and print only nouns from the tagged tokens.
NLP
tokens = nltk.word_tokenize("Dogs bark loudly at night") pos_tags = nltk.pos_tag(tokens) nouns = [word for word, tag in pos_tags if tag [1] [2]] print(nouns) # Expected output: ['Dogs', 'night']
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' to compare tag with a list (causes error).
Using wrong tag names or not filtering nouns.
✗ Incorrect
Use 'in' to check if tag is in the list ['NN', 'NNS'] which are noun tags. 'NN' is a noun tag example.