识别新闻标题中的命名实体
新闻机构常在标题中标注人物、地点、组织等命名实体,以改进搜索、索引和推荐。您的任务是使用 Hugging Face 的 pipeline 自动检测并归类新闻标题中的这些实体。
本练习是课程的一部分
Python 中的自然语言处理(NLP)
练习说明
- 使用
"dslim/bert-base-NER"模型创建一个ner_pipeline。 - 从给定的
headline中抽取命名实体。
交互式实操练习
通过完成这段示例代码来试试这个练习。
from transformers import pipeline
# Create the NER pipeline
ner_pipeline = pipeline(
task="____",
model="____",
grouped_entities=True
)
headline = "Apple is planning to open a new office in San Francisco next year."
# Get named entities
entities = ____
for entity in entities:
print(f"{entity['entity_group']}: {entity['word']}")