stanzaを使ってみる 6
以前の投稿でstanzaを使ってみたことをまとめた.
今回はその続きでLemmatization(単語をその基本形、原形に変換する処理)をstanzaで行ってみたい.
コードは下記.
import stanza
# モデルダウンロード(初回のみ)
stanza.download('en')
# 原形化を含むパイプライン構築
nlp = stanza.Pipeline(lang='en', processors='tokenize,pos,lemma')
# 処理するテキスト
text = "The children were playing in the park while their parents watched."
# NLPパイプラインを通す
doc = nlp(text)
# 結果出力
for sentence in doc.sentences:
for word in sentence.words:
print(f"{word.text} → 原形: {word.lemma} / 品詞: {word.upos}")
Output例
The → 原形: the / 品詞: DET
children → 原形: child / 品詞: NOUN
were → 原形: be / 品詞: AUX
playing → 原形: play / 品詞: VERB
in → 原形: in / 品詞: ADP
the → 原形: the / 品詞: DET
park → 原形: park / 品詞: NOUN
while → 原形: while / 品詞: SCONJ
their → 原形: their / 品詞: PRON
parents → 原形: parent / 品詞: NOUN
watched → 原形: watch / 品詞: VERB
. → 原形: . / 品詞: PUNCT