使用 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])