開始使用免費開始

使用 spaCy 進行 NER

具名實體辨識(Named Entity Recognition,NER)可幫助你輕鬆識別文件中的關鍵元素,例如人名與地名。它能整理非結構化資料並偵測重要資訊,這在你處理大型資料集時尤其重要。在本練習中,你會實作具名實體辨識。

en_core_web_sm 已替你載入為 nlp。來自 Airline Travel Information System(ATIS)資料集的 3 則留言已放在名為 texts 的列表中供你使用。

本練習屬於課程

使用 spaCy 的自然語言處理

檢視課程

練習說明

  • 使用串列生成式建立 documents,其中包含 texts 中每一段文字所對應的 Doc 容器。
  • 對於每個 doc 容器,透過迭代 doc.ents,列印每個實體的文字與對應標籤。
  • 列印第 6 個權杖的文字,以及第二個 Doc 容器的實體型別。

動手互動練習

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

# Compile a list of all Doc containers of texts
documents = [____ for text in texts]

# Print the entity text and label for the entities in each document
for doc in documents:
    print([(____, ____) for ent in ____])
    
# Print the 6th token's text and entity type of the second document
print("\nText:", documents[1][5].____, "| Entity type: ", documents[1][5].____)
編輯並執行程式碼