Processamento de Linguagem Natural com spaCy
Azadeh Mobasher
Principal Data Scientist
| POS | Descrição | Exemplo |
|---|---|---|
| VERB | Verbo | run, eat, ate, take |
| NOUN | Substantivo | man, airplane, tree, flower |
| ADJ | Adjetivo | big, old, incompatible, conflicting |
| ADV | Advérbio | very, down, there, tomorrow |
| CONJ | Conjunção | and, or, but |
spaCy registra as tags de POS no recurso pos_ do pipeline nlpspacy.explain() explica uma tag de POSverb_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')]
| Tipo de entidade | Descrição |
|---|---|
| PERSON | Pessoa nomeada ou família |
| ORG | Empresas, instituições etc. |
| GPE | Entidade geopolítica: países, cidades etc. |
| LOC | Locais que não são GPE: cadeias de montanhas etc. |
| DATE | Datas ou períodos absolutos ou relativos |
| TIME | Tempo menor que um dia |
spaCy extraem entidades pelo componente NER do pipelinedoc.entsspaCy também marca cada entidade com seu rótulo (.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 tem um visualizador moderno: displaCydisplaCy destaca as entidades e seus rótulosimport 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")
Processamento de Linguagem Natural com spaCy