Traitement du langage naturel avec spaCy
Azadeh Mobasher
Principal Data Scientist
| POS | Description | Exemple |
|---|---|---|
| VERB | Verbe | run, eat, ate, take |
| NOUN | Nom | man, airplane, tree, flower |
| ADJ | Adjectif | big, old, incompatible, conflicting |
| ADV | Adverbe | very, down, there, tomorrow |
| CONJ | Conjonction | and, or, but |
spaCy stocke les étiquettes POS dans la caractéristique pos_ du pipeline nlpspacy.explain() explique une étiquette POS donnéeverb_sent = "I watch TV."print([(token.text, token.pos_, spacy.explain(token.pos_)) for token in nlp(verb_sent)])
[('I', 'PRON', 'pronoun'),
('watch', 'VERB', 'verb'),
('TV', 'NOUN', 'noun'),
('.', 'PUNCT', 'punctuation')]
noun_sent = "I left without my watch."print([(token.text, token.pos_, spacy.explain(token.pos_)) for token in nlp(noun_sent)])
[('I', 'PRON', 'pronoun'),
('left', 'VERB', 'verb'),
('without', 'ADP', 'adposition'),
('my', 'PRON', 'pronoun'),
('watch', 'NOUN', 'noun'),
('.', 'PUNCT', 'punctuation')]
| Type d'entité | Description |
|---|---|
| PERSON | Personne nommée ou famille |
| ORG | Entreprises, institutions, etc. |
| GPE | Entité géopolitique : pays, villes, etc. |
| LOC | Lieux non GPE : chaînes de montagnes, etc. |
| DATE | Dates ou périodes absolues ou relatives |
| TIME | Temps inférieur à une journée |
spaCy extraient les entités nommées via le composant NER du pipelinedoc.entsspaCy ajoute aussi à chaque entité son étiquette (.label_)
import spacy nlp = spacy.load("en_core_web_sm") text = "Albert Einstein was genius." doc = nlp(text)print([(ent.text, ent.start_char, ent.end_char, ent.label_) for ent in doc.ents])
>>> [('Albert Einstein', 0, 15, 'PERSON')]
Doc
import spacy nlp = spacy.load("en_core_web_sm") text = "Albert Einstein was genius." doc = nlp(text)print([(token.text, token.ent_type_) for token in doc])
>>> [('Albert', 'PERSON'), ('Einstein', 'PERSON'),
('was', ''), ('genius', ''), ('.', '')]
spaCy est doté d'un visualiseur moderne : displaCydisplaCy met en évidence les entités nommées et leurs étiquettesimport spacy from spacy import displacy text = "Albert Einstein was genius." nlp = spacy.load("en_core_web_sm") doc = nlp(text)displacy.serve(doc, style="ent")
Traitement du langage naturel avec spaCy