使用 spaCy 進行 POS 標註
在這個練習中,你會實作 POS 標註。POS 標註在 NLP 中很實用,因為它能讓演算法理解句子的語法結構,並判定像是 watch、play 這類多重詞性的單字。
本練習已為你載入 en_core_web_sm 並命名為 nlp。同時提供了 3 則來自 Airline Travel Information System(ATIS)資料集的留言,存放在名為 texts 的清單中。
本練習屬於課程
使用 spaCy 的自然語言處理
練習說明
- 使用串列生成式,將
texts清單中的每個文字轉成doc容器,組成documents串列。 - 對於每個
doc容器,使用巢狀 for 迴圈走訪documents與各doc的詞元,列印每個詞元的文字及其對應的 POS 標記。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Compile a list of all Doc containers of texts
documents = [____(text) for text in texts]
# Print token texts and POS tags for each Doc container
for doc in documents:
for ____ in doc:
print("Text: ", ____, "| POS tag: ", ____)
print("\n")