Named Entity Recognition (NER)
Named Entity Recognition (NER) is essential in various Natural Language Processing (NLP) applications. Traditional NER models are effective…
Named Entity Recognition (NER)
Named Entity Recognition (NER) is essential in various Natural Language Processing (NLP) applications. Traditional NER models are effective but limited to a set of predefined entity types. In contrast, Large Language Models (LLMs) can extract arbitrary entities through natural language instructions, offering greater flexibility. However, their size and cost, particularly for those accessed via APIs like ChatGPT, make them impractical in resource-limited scenarios. In this paper, we introduce a compact NER model trained to identify any type of entity. Leveraging a bidirectional transformer encoder, our model, GLiNER, facilitates parallel entity extraction, an advantage over the slow sequential token generation of LLMs. Through comprehensive testing, GLiNER demonstrate strong performance, outperforming both ChatGPT and fine-tuned LLMs in zero-shot evaluations on various NER benchmarks.
[embed]
Ref: 2311.08526
import spacy
from spacy import displacy
nlp=spacy.load('en_core_web_sm')
text = """
Harilala Rasoanaivo, un homme d'affaires local d'Antananarivo, a enregistré une nouvelle société nommée "Rasoanaivo Enterprises" au Lot II M 92 Antohomadinika. Son numéro est le +261 32 22 345 67, et son adresse électronique est harilala.rasoanaivo@telma.mg. Il a fourni son numéro de sécu 501-02-1234 pour l'enregistrement.
"""
doc = nlp(text) # Process the text to create a Doc object
for ent in doc.ents:
print(ent.text, ent.start_char, ent.end_char, ent.label_)
displacy.render(doc, style='ent', jupyter=True)
Output:

english_text = """
John Doe, a software engineer at Google, lives at 1600 Amphitheatre Parkway, Mountain View, CA 94043. His phone number is +1-650-253-0000, and his email is john.doe@google.com. His Social Security Number is ***-**-1234.
"""
english_entities = model.predict_entities(english_text, labels)
print("GLiNER entities for English text:")
for entity in english_entities:
print(f"{entity["text"]} => {entity["label"]}")

# import nltk
from nltk import ne_chunk
from nltk.tokenize import word_tokenize # Added this import
nltk.download('maxent_ne_chunker')
nltk.download('words')
nltk.download('punkt_tab') # Added this line
nltk.download('averaged_perceptron_tagger_eng') # Added this line to download the missing resource
nltk.download('maxent_ne_chunker_tab') # Added this line to download the missing resource
text = """
Harilala Rasoanaivo, un homme d'affaires local d'Antananarivo, a enregistré une nouvelle société nommée "Rasoanaivo Enterprises" au Lot II M 92 Antohomadinika.
Son numéro est le +261 32 22 345 67, et son adresse électronique est harilala.rasoanaivo@telma.mg.
Il a fourni son numéro de sécu 501-02-1234 pour l'enregistrement.
"""
ne_token =word_tokenize(text)
ne_tags = nltk.pos_tag(ne_token)
ne_ner = ne_chunk(ne_tags)
print(ne_ner)

GLiNER Specificity of PII: GLiNER (specifically the gliner_multi_pii-v1 model) demonstrated superior capability in identifying highly specific PII categories like person, company, full address, phone number, email, and Social Security Number. It successfully extracted these from the French text, even though the labels were provided in English.
Multilingual and Custom Labels: GLiNER is designed to be highly flexible for zero-shot or few-shot learning, allowing you to define custom labels (like PII categories) and apply them across different languages without extensive retraining. This makes it very effective for targeted PII extraction in diverse linguistic contexts.
NLTK (with ne_chunk) General NER: NLTK’s ne_chunk (which relies on nltk.pos_tag and then chunking) is good for general named entity recognition, categorizing entities into broader types like PERSON and ORGANIZATION. It correctly identified ‘Harilala Rasoanaivo’ as PERSON and ‘Rasoanaivo Enterprises’ as ORGANIZATION.
Limitations with Specific PII and Non-English Text: NLTK’s standard ne_chunk is primarily trained on English corpora. As seen in the output, it struggled to precisely classify specific PII types in the French text. For instance, phone numbers and social security numbers were treated as general tokens with part-of-speech tags like CD (cardinal digit) or JJ (adjective), rather than recognized as distinct PII categories. Similarly, the address was partially recognized as PERSON (Lot II M 92 Antohomadinika).
Resource Dependencies: NLTK requires downloading multiple data packages (punkt_tab, averaged_perceptron_tagger_eng, maxent_ne_chunker_tab) for its various functionalities, which can sometimes lead to LookupError if not all dependencies are met.
Conclusion For general-purpose Named Entity Recognition, especially in English, NLTK (or spaCy with its English models) can be a good starting point. However, for precise and comprehensive PII extraction, particularly when dealing with specific, custom categories or multilingual content, GLiNER is significantly more effective and robust. Its advanced architecture allows it to generalize better to unseen entity types and languages, making it a powerful tool for sensitive data identification tasks.
Ref: [2311.08526] GLiNER: Generalist Model for Named Entity Recognition using Bidirectional Transformer
# !pip install gliner
from gliner import GLiNER
model = GLiNER.from_pretrained("urchade/gliner_multi_pii-v1")
text = """
Harilala Rasoanaivo, un homme d'affaires local d'Antananarivo, a enregistré une nouvelle société nommée "Rasoanaivo Enterprises" au Lot II M 92 Antohomadinika.
Son numéro est le +261 32 22 345 67, et son adresse électronique est harilala.rasoanaivo@telma.mg. Il a fourni son numéro de sécu 501-02-1234 pour l'enregistrement.
"""
labels = ["work", "booking number", "personally identifiable information", "driver licence", "person", "book", "full address", "company", "actor",
"character", "email", "passport number", "Social Security Number", "phone number"]
entities = model.predict_entities(text, labels)
for entity in entities:
print(entity["text"], "=>", entity["label"])

Let’s compare GLiNER and spaCy for Named Entity Recognition (NER), drawing on their typical characteristics and what we’ve observed in our previous interactions:
spaCy General NER Capabilities: spaCy is a highly optimized and efficient library for various NLP tasks, including NER. It provides pre-trained models for many languages (like en_core_web_sm we used) that perform very well on common entity types (PERSON, ORG, GPE, DATE, etc.).
Rule-based and Statistical Models: spaCy’s models often combine statistical models (like convolutional neural networks or transformers, depending on the model version) with rule-based systems to achieve high accuracy and speed.
Ecosystem and Features: Beyond NER, spaCy offers a rich ecosystem of tools for tokenization, part-of-speech tagging, dependency parsing, sentence segmentation, and more, making it a comprehensive NLP library.
Customization: You can train custom NER models with spaCy by providing annotated data. This requires a significant amount of labeled examples for your specific entity types and domain.
Multilinguality: spaCy supports many languages with pre-trained models, but each language typically requires downloading a specific model.
PII Extraction: For PII extraction, spaCy’s general models will identify entities like PERSON and ORG. To extract more granular PII (e.g., phone numbers, email addresses, social security numbers) that aren’t part of its standard entity types, you would typically need to:
Train a custom NER model: This is effective but data-intensive.
Write custom rules/patterns: Using spaCy’s Matcher or EntityRuler components to define patterns for PII formats (e.g., regex for phone numbers, email). GLiNER
Zero-Shot/Few-Shot NER: GLiNER (General Language INstruction-based EntitY Recognizer) is a more modern approach, typically built on transformer models. Its primary strength lies in its ability to perform zero-shot or few-shot NER. This means you can define custom entity labels (like ‘phone number’ or ‘social security number’) on the fly, and the model can identify them without needing extensive training data for those specific labels.
Instruction-Based Learning: It leverages the understanding of large language models to interpret the meaning of your custom labels and find corresponding entities in text.
Flexibility for Custom Entities: This makes GLiNER incredibly flexible for new or highly specific entity types that might not be covered by standard NER models, or when you don’t have large annotated datasets for custom training.
Multilinguality: Many GLiNER models are designed to be multilingual, allowing you to use the same set of labels across different languages, as demonstrated when it extracted PII from the French text with English labels.
PII Extraction: As we saw, GLiNER excels at PII extraction because you can explicitly tell it to look for labels like ‘phone number’, ‘email’, ‘Social Security Number’, etc., and it will attempt to find them based on its general language understanding.
When to Use Which: Choose spaCy when: You need high-performance, production-ready NER for standard entity types (PERSON, ORG, LOC, etc.). You have ample annotated data for custom entity types and want to train a highly accurate, specialized model. You need a comprehensive NLP pipeline including POS tagging, dependency parsing, etc.
Choose GLiNER when: You need to extract highly specific or novel entity types for which you don’t have training data (zero-shot/few-shot scenario). You are working with PII extraction and need to identify precise categories like email addresses, phone numbers, SSNs, credit card numbers, etc., without extensive custom model training. You need flexibility across multiple languages with the same set of custom labels. You want to quickly prototype and test new entity extraction ideas. In essence, spaCy is a robust, general-purpose NLP toolkit with excellent traditional NER capabilities, while GLiNER shines in its adaptability for custom, specific entity extraction, particularly in zero-shot or few-shot learning scenarios, making it highly valuable for tasks like PII identification.
메타데이터
- post_id
- b2dc51733d3a
- slug
- named-entity-recognition-ner-b2dc51733d3a
- url
- https://medium.com/@ipvikas/named-entity-recognition-ner-b2dc51733d3a
- canonical_url
- https://medium.com/@ipvikas/named-entity-recognition-ner-b2dc51733d3a
- author_url
- https://medium.com/@ipvikas
- status
- ok
- fetched_at
- 2026-06-09 15:37:30