開始使用免費開始

找出新聞文章中提到的人名

在本題中,給你一段出自《TechCrunch》的新聞內容。你的任務是撰寫函式 find_people,用來辨識一段文字中被提到的人名。接著你會使用 find_people 找出文章中相關的人物。

文章內容已存為字串 tc,並已列印到主控台。所需的 spaCy 模型也已載入為 nlp

本練習屬於課程

Python 中文本特徵工程

檢視課程

練習說明

  • text 建立一個 Doc 物件。
  • 使用串列生成式迭代 doc.ents,建立一個標註為 PERSON 的命名實體清單。
  • 使用 find_persons(),列印在 tc 中提到的人名。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

def find_persons(text):
  # Create Doc object
  doc = ___(___)
  
  # Identify the persons
  persons = [ent.____ for ent in doc.____ if ent.____ == 'PERSON']
  
  # Return persons
  return persons

print(____(____))
編輯並執行程式碼