Named entity recognition (NER)
Named Entity Recognition (NER) is a fundamental task in Natural Language Processing (NLP); its goal is to identify entities with specific meaning in text and classify them into predefined categories.
Core Concepts
- Named entity: Proper nouns in text that denote specific objects
- Entity categories: Common types include person names, place names, organization names, time, dates, currency, etc.
An Analogy
Think of NER as a "highlighter" tool for text - just as you use different colored markers to highlight different types of important information when reading a document.
Application scenarios of NER
Practical application domains
- Information extraction: Extract key people and events from news
- Search engine optimization: Enhance semantic understanding of search results
- Customer support: Automatically identify key entities in user queries
- Medical field: Identify drug names and disease terms in medical records
Industry value
- Finance: Automatically analyze company and stock information in financial news
- Legal: Quickly locate key clauses and parties in contracts
- E-commerce: Extract product features and brand names from user reviews
Technical implementation of NER
Classification of basic methods
| Method type | Description | Pros and Cons |
|---|---|---|
| Rule matching | Based on predefined rules and dictionaries | High precision but low coverage |
| Statistical learning | Use traditional machine learning models | Requires feature engineering |
| Deep learning | Based on neural network models | High performance but requires large amounts of data |
Common algorithms
- Conditional Random Field (CRF)
- Bidirectional LSTM
- Pretrained models such as BERT
Example
A simple NER example using spaCy
import
spacy
Load the English model
nlp
=
spacy.
load
(
"en_core_web_sm"
)
Process text
text
=
"Apple is looking at buying U.K. startup for $1 billion"
doc
=
nlp
(
text
)
Output the recognition results
for
ent
in
doc.
ents
:
(
ent.
text
,
ent.
label_
)
Evaluation metrics for NER
Key performance metrics
- Precision: The proportion of correctly identified entities among all identified entities
- Recall: The proportion of correctly identified entities among all actual entities
- F1 score: The harmonic mean of precision and recall
Evaluation example
Suppose the test set contains 100 entities:
- The system identifies 90, of which 80 are correct
- Precision = 80/90 ~ 89%
- Recall = 80/100 = 80%
- F1 = 2*(0.89*0.8)/(0.89+0.8) ≈ 84%
Challenges and solutions of NER
Common challenges
- Entity boundary recognition: e.g., whether "New York Times" should be recognized as a whole or separately
- Entity ambiguity: e.g., "Apple" may refer to the fruit or the company
- Domain adaptation: Entity recognition in the medical domain requires specialized dictionaries
Solution
- Context modeling: Use surrounding words to determine entity type
- Domain transfer learning: Pretrain on general data first, then fine-tune in a specialized domain
- Multi-model ensemble: Combine rule-based and statistical methods to improve robustness
Practical Exercise
Exercise 1: Use existing tools
- Install the spaCy library:
pip install spacy - Download the language model:
python -m spacy download en_core_web_sm - Try analyzing text from different domains (news, scientific papers, social media)
Exercise 2: Build simple rules
Example
A simple rule-based NER implementation
import
re
def
rule_based_ner
(
text
)
:
Match dates
dates
=
re
.
findall
(
r
'\d{1,2}[/-]\d{1,2}[/-]\d{2,4}'
,
text
)
Match currency
currencies
=
re
.
findall
(
r
'$\d+.?\d*'
,
text
)
return
{
"date"
: dates
,
"currency"
: currencies
}
sample
=
"The meeting is scheduled for 12/15/2023, with a budget of $5000"
(
rule_based_ner
(
sample
)
)