0
0
NLPml~10 mins

NER with NLTK in NLP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to tokenize the sentence before named entity recognition.

NLP
import nltk
sentence = "Apple is looking at buying U.K. startup for $1 billion"
tokens = nltk.word_tokenize([1])
print(tokens)
Drag options to blanks, or click blank then click option'
Atext
Bnltk
Ctokens
Dsentence
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the tokenizer function itself instead of the sentence string.
Passing the token list instead of the sentence string.
2fill in blank
medium

Complete the code to tag parts of speech for the tokens.

NLP
pos_tags = nltk.pos_tag([1])
print(pos_tags)
Drag options to blanks, or click blank then click option'
Atokens
Bsentence
Cpos_tags
Dnltk
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the original sentence string instead of tokens.
Passing the POS tags variable itself.
3fill in blank
hard

Fix the error in the code to perform named entity recognition on POS-tagged tokens.

NLP
named_entities = nltk.ne_chunk([1])
print(named_entities)
Drag options to blanks, or click blank then click option'
Asentence
Bpos_tags
Ctokens
Dnamed_entities
Attempts:
3 left
💡 Hint
Common Mistakes
Passing raw tokens instead of POS-tagged tokens.
Passing the original sentence string.
4fill in blank
hard

Fill both blanks to extract named entity labels and their word tokens from the tree.

NLP
for subtree in named_entities:
    if hasattr(subtree, '[1]') and subtree.label() == '[2]':
        print('Entity:', ' '.join([token for token, pos in subtree.leaves()]))
Drag options to blanks, or click blank then click option'
Alabel
Blabel()
CPERSON
Dleaves
Attempts:
3 left
💡 Hint
Common Mistakes
Using label() inside hasattr which expects a string attribute name.
Confusing method call with attribute name.
5fill in blank
hard

Fill all three blanks to create a dictionary of named entities and their types.

NLP
entities = { ' '.join([token for token, pos in subtree.leaves()]): subtree.[1]() for subtree in named_entities if hasattr(subtree, '[2]') }
print(entities)

# Filter only entities of type [3]
Drag options to blanks, or click blank then click option'
Alabel
CPERSON
Dleaves
Attempts:
3 left
💡 Hint
Common Mistakes
Using leaves instead of label for entity type.
Passing method call as string in hasattr.
Using wrong entity type string.