この記事では、依存性解析とは何か、そしてPythonプログラミング言語を使ってどのように実装するかを理解します。
依存関係解析とは?
文中の単語間の依存関係に基づいて文の文法構造を評価する手法を依存性解析といいます。
係り受け解析では、文中の2つの単語の関係を表すさまざまなタグがあります。
これが従属性タグです。
1
2
|
import spacy
from spacy import displacy
|
なぜ依存関係解析が必要なのか?
係り受け解析は、構文解析のように文法規則を用いるのではなく、文中の単語間の関係をタグで判断して解析木を構築するため、単語の順序が変わっても柔軟性が高いのが特徴です。
Python による依存性解析の実装
依存性解析の実装では、Pythonの spaCy
モジュールを使用します。
まず、このモジュールをインポートします。
1
2
3
4
|
text1 = 'It took me more than two hours to translate a few pages of English.'
text2 = 'The old rusted farm equipment surrounded the house predicting its demise.'
text3 = 'I am a great listener, really good with empathy vs sympathy and all that, but I hate people.'
text4 = 'The ants enjoyed the barbecue more than the family.'
|
次に、係り受け解析の対象となる文章をランダムに選びます。
nlp = spacy.load( 'en_core_web_sm' )
|
次に、言語オブジェクトを生成し、モデルデータと重みをロードして、それを返す spacy.load
関数を使う。
1
|
displacy.render(nlp(text1),jupyter = True )
|
最後に、前出のテキストに対する依存関係解析の出力を表示します。





まとめ
あなたは、NLP を使って文の係り受け解析図を作る方法を学びました。
このチュートリアルは気に入りましたか?いずれにせよ、以下のチュートリアルをご覧になることをお勧めします。
- Pythonでの語幹解析とレマット化
- PythonでゼロからBag of Wordsモデルを作成する
- PythonによるWord Cloudの作成
- Pythonを使ったセンチメント分析
お時間をいただき、ありがとうございました 何か新しいことを学べたでしょうか!