開始使用免費開始

使用 spaCy 進行句子切分

在這個練習中,你會練習句子切分。於 NLP 中,把文件切成一句一句是很實用的基本操作。它常是許多更進一步任務(例如命名實體辨識)的前置步驟。此外,擷取句子數量也能幫你了解文字大約包含多少資訊。

你可以在名為 texts 的清單中存取 10 則美食評論。

en_core_web_sm 模型已經以 nlp 名稱為你載入完成。

本練習屬於課程

使用 spaCy 的自然語言處理

檢視課程

練習說明

  • texts 清單中的每個項目執行 spaCy 模型,彙整成所有 Doc 容器所組成的清單 documents
  • 透過迭代 documents 清單,取出每個 doc 容器的句子,並將它們加入名為 sentences 的清單。
  • 使用 sentences 清單來計算每個 doc 容器中的句子數量。

動手互動練習

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

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

# Iterate through documents and append sentences in each doc to the sentences list
sentences = []
for doc in documents:
  sentences.append([s for s in ____.____])
  
# Find number of sentences per each doc container
print([len(____) for s in sentences])
編輯並執行程式碼