stanzaを使ってみる 8
以前の投稿でstanzaを使ってみたことをまとめた.
今回はその続きで同一文を多言語で処理し構文比較をstanzaで行ってみたい.
コードは下記.
import stanza
# モデルを準備
stanza.download('en')
stanza.download('fr')
# 英語とフランス語のNLPパイプライン
nlp_en = stanza.Pipeline(lang='en', processors='tokenize,pos,lemma,depparse')
nlp_fr = stanza.Pipeline(lang='fr', processors='tokenize,pos,lemma,depparse')
# 同一内容の文
text_en = "The children are playing in the park."
text_fr = "Les enfants jouent dans le parc."
# 解析
doc_en = nlp_en(text_en)
doc_fr = nlp_fr(text_fr)
# 結果表示(英語)
print("-- English Dependency --")
for word in doc_en.sentences[0].words:
head = doc_en.sentences[0].words[word.head - 1].text if word.head > 0 else "ROOT"
print(f"{word.text} --{word.deprel}→ {head}")
# 結果表示(フランス語)
print("-- French Dependency --")
for word in doc_fr.sentences[0].words:
head = doc_fr.sentences[0].words[word.head - 1].text if word.head > 0 else "ROOT"
print(f"{word.text} --{word.deprel}→ {head}")
Output例
— English Dependency —
The –det→ children
children –nsubj→ playing
are –aux→ playing
playing –root→ ROOT
in –case→ park
the –det→ park
park –obl→ playing
. –punct→ playing
— French Dependency —
Les –det→ enfants
enfants –nsubj→ jouent
jouent –root→ ROOT
dans –case→ parc
le –det→ parc
parc –obl:arg→ jouent
. –punct→ jouent