Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Named entity recognition (NER)

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

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

  1. Information extraction: Extract key people and events from news
  2. Search engine optimization: Enhance semantic understanding of search results
  3. Customer support: Automatically identify key entities in user queries
  4. Medical field: Identify drug names and disease terms in medical records

Industry value


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

  1. Conditional Random Field (CRF)
  2. Bidirectional LSTM
  3. 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

:

   

print

(

ent.

text

,

ent.

label_

)


Evaluation metrics for NER

Key performance metrics

  1. Precision: The proportion of correctly identified entities among all identified entities
  2. Recall: The proportion of correctly identified entities among all actual entities
  3. F1 score: The harmonic mean of precision and recall

Evaluation example

Suppose the test set contains 100 entities:


Challenges and solutions of NER

Common challenges

  1. Entity boundary recognition: e.g., whether "New York Times" should be recognized as a whole or separately
  2. Entity ambiguity: e.g., "Apple" may refer to the fruit or the company
  3. Domain adaptation: Entity recognition in the medical domain requires specialized dictionaries

Solution


Practical Exercise

Exercise 1: Use existing tools

  1. Install the spaCy library:pip install spacy
  2. Download the language model:python -m spacy download en_core_web_sm
  3. 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"

print

(

rule_based_ner

(

sample

)

)