NLP
Named Entity Recognition
Extract entities with spaCy and transformers.
By EZ4Code Team
nerspacytransformers
Code
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Apple was founded by Steve Jobs in California in 1976 for $1.")
for ent in doc.ents:
print(ent.text, ent.label_, ent.start_char, ent.end_char)
# Visualize entities (in Jupyter)
# from spacy import displacy
# displacy.render(doc, style="ent")
# HuggingFace pipeline for NER
from transformers import pipeline
ner_pipe = pipeline("ner", aggregation_strategy="simple",
model="dslim/bert-base-NER")
results = ner_pipe("Tim Cook is the CEO of Apple in Cupertino.")
for r in results:
print(r["entity_group"], r["word"], round(r["score"], 3))
# Add a custom EntityRuler for domain terms
ruler = nlp.add_pipe("entity_ruler")
ruler.add_patterns([{"label": "PRODUCT", "pattern": "iPhone"}])Explanation
NER identifies spans of text as entities such as persons, organizations, dates, and locations. spaCy ships a built-in NER component accessible through doc.ents, while a HuggingFace token-classification pipeline offers transformer accuracy. The EntityRuler lets you add domain-specific patterns on top of the statistical model.
More NLP Snippets
Tokenization
Tokenize text with NLTK, spaCy, and regex.
Stopwords
Remove common words with NLTK and spaCy.
Stemming and Lemmatization
Reduce words to roots with Porter, Snowball, and lemmatizers.
TF-IDF
Vectorize text with TF-IDF and n-grams.
Word2Vec
Train and use word embeddings with gensim.
Sentiment Analysis
Score text polarity with VADER and transformers.