Attributs lexicaux
Dans cet exemple, vous allez utiliser les objets Doc et Token de spaCy, ainsi que des attributs lexicaux, pour trouver des pourcentages dans un texte. Vous rechercherez deux tokens consécutifs : un nombre puis un signe de pourcentage. L’objet nlp en anglais a déjà été créé.
Cet exercice fait partie du cours
NLP avancé avec spaCy
Instructions
- Utilisez l’attribut de token
like_numpour vérifier si un token dans ledocressemble à un nombre. - Récupérez le token qui suit le token actuel dans le document. L’indice du token suivant dans le
docesttoken.i + 1. - Vérifiez si l’attribut
textdu token suivant est un signe de pourcentage "%".
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# 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)