Lexikalische Attribute
In diesem Beispiel verwendest du spaCys Objekte Doc und Token sowie lexikalische Attribute, um Prozentsätze in einem Text zu finden. Du suchst nach zwei aufeinanderfolgenden Tokens: einer Zahl und einem Prozentzeichen. Das englische nlp-Objekt wurde bereits erstellt.
Diese Übung ist Teil des Kurses
Fortgeschrittenes NLP mit spaCy
Anleitung zur Übung
- Verwende das Token-Attribut
like_num, um zu prüfen, ob ein Token indoceiner Zahl ähnelt. - Hole das Token, das im Dokument auf das aktuelle Token folgt. Der Index des nächsten Tokens in
docisttoken.i + 1. - Prüfe, ob das
text-Attribut des nächsten Tokens ein Prozentzeichen "%" ist.
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)