本文目录导读:

对于Python中的命名实体识别(NER),推荐以下几个主流库:
spaCy(最推荐)
- 优点:速度快、使用简单、预训练模型效果好
- 安装:
pip install spacy - 使用示例:
import spacy nlp = spacy.load("zh_core_web_sm") # 中文模型 doc = nlp("苹果公司在北京成立") for ent in doc.ents: print(ent.text, ent.label_)
Stanford CoreNLP
- 优点:学术界经典,多语言支持好
- 安装:
pip install stanfordcorenlp - 需要下载Java和模型文件
HanLP
- 优点:中文处理能力强,支持多种模型
- 安装:
pip install hanlp - 使用示例:
import hanlp recognizer = hanlp.load(hanlp.pretrained.ner.MSRA_NER_ALBERT_BASE) print(recognizer(["苹果公司在1997年成立于北京"]))
transformers (Hugging Face)
- 优点:可以使用最新的预训练模型(BERT、RoBERTa等)
- 安装:
pip install transformers - 使用示例:
from transformers import pipeline ner = pipeline("ner", model="bert-base-chinese") results = ner("苹果公司在北京成立")
选择建议:
- 快速上手:spaCy
- 中文NER:HanLP 或 spaCy(中文模型)
- 最新技术:transformers(Hugging Face)
- 生产环境:spaCy(性能最优)
如果主要做中文NER,建议先用spaCy或HanLP试试。