用詞性標記進行文字分析
某款語言學習 App 想幫助使用者理解句子結構,方法是把每個字的文法角色用醒目方式標示。你的任務是使用 Hugging Face 的 pipeline,為給定句子的每個詞加上對應的詞性(PoS)標記。
本練習屬於課程
Python 的 Natural Language Processing(NLP)
練習說明
- 使用模型
"vblagoje/bert-english-uncased-finetuned-pos"建立pos_pipeline。 - 對提供的
sentence套用該 pipeline。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
from transformers import pipeline
# Create the PoS tagging pipeline
pos_pipeline = pipeline(
task="____",
model="____",
grouped_entities=True
)
sentence = "I am meeting my friends for coffee this afternoon."
# Get PoS tags
pos_tags = ____
for token in pos_tags:
print(f"{token['word']}: {token['entity_group']}")