{{category python}} !!! spaCy {{outline}} ---- https://spacy.io/ !!インストール https://spacy.io/usage#quickstart *Windows のコンソールで pip install spacy *Windowsのシェルで管理者として実行(右ボタン) python -m spacy download en *Macの場合 pip3 install spacy python3 -m spacy download en_core_web_sm *Pythonのシェルで import spacy *それか、スクリプトの先頭に書いておく {{pre import spacy nlp = spacy.load('en_core_web_sm') 以下スクリプト }} !Anaconda NavigatorのEnvironmentsで追加できないときは、 https://github.com/conda/conda/issues/9423 *PowerShellで、 conda install -c conda-forge spacy conda install -c conda-forge spacy-model-en_core_web_sm https://anaconda.org/conda-forge/spacy !!しくみ https://spacy.io/usage/spacy-101 *言語処理に必要な「データベース」が作ってある。 *その「データベース」に基づいて、言語処理(自然言語解析)を行う。 !処理 *tokenizer: 単語に分けて *tagger : 品詞分析して *parser : 構文解析して *ner : エンティティ付与 !手順 *「データベース("en_core_web_sm")」の読み込み spacy.load("en_core_web_sm") *読み込んだものを例えば nlp という名前で保存して、それを使って処理できるようにする。 nlp = spacy.load("en_core_web_sm") *処理するデータを変数に入れて sample = "Parents drive their children everywhere." *nlpで処理した結果を変数(sample_doc)に保存する sample_doc = nlp(sample) *あとは、このsample_docにたいして、spaCyの関数を使って「処理」をする。 **表面上は、sample_docを表示させると、普通に文が表示されるだけ。 ***その裏で、処理結果の情報も蓄えられている。 !!関数 !依存関係の可視化 https://spacy.io/usage/visualizers {{pre import spacy from spacy import displacy nlp = spacy.load("en_core_web_sm") doc = nlp("This is a sentence.") displacy.serve(doc, style="dep") }} *Jupyter Notebookで実行する場合、最後のdisplacy.serveの代わりに、displacy.renderを使う。 {{pre sample = "Parents drive their children everywhere." sample_doc = nlp(sample) displacy.render(sample_doc, style="dep") }} {{ref_image parents.jpg}} !!Demo https://explosion.ai/demos/ !!References https://githubja.com/explosion/spacy https://ishitonton.hatenablog.com/entry/2018/11/24/004748 https://spacy.io/usage/spacy-101