開始使用免費開始

辨識新聞標題中的命名實體

新聞媒體常會在標題中標記人名、地名、組織等命名實體,以提升搜尋、索引與推薦的效果。你的工作是使用 Hugging Face 的 pipeline,自動偵測並分組這些新聞標題裡的實體。

本練習屬於課程

Python 的 Natural Language Processing(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']}")
編輯並執行程式碼