Lexical attributes
In this example, you'll use spaCy's Doc and Token objects, and lexical attributes to find percentages in a text. You'll be looking for two subsequent tokens: a number and a percent sign. The English nlp object has already been created.
Diese Übung ist Teil des Kurses
Advanced NLP with spaCy
Anleitung zur Übung
- Use the
like_numtoken attribute to check whether a token in thedocresembles a number. - Get the token following the current token in the document. The index of the next token in the
docistoken.i + 1. - Check whether the next token's
textattribute is a percent sign "%".
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Process the text
doc = nlp("In 1990, more than 60% of people in East Asia were in extreme poverty. Now less than 4% are.")
# Iterate over the tokens in the doc
for token in doc:
# Check if the token resembles a number
if ____.____:
# Get the next token in the document
next_token = ____[____]
# Check if the next token's text equals '%'
if next_token.____ == '%':
print('Percentage found:', token.text)