LoginSignup
3
6

More than 3 years have passed since last update.

polyglotを使ってpythonで形態素解析をする準備から品詞タグ付けまで

Posted at

準備

polyglot (ドキュメント) を使っていく.

以下は Python 3.8.5 で動作を確認した.
まず,

pip install numpy
pip install polyglot
pip install six
pip install pycld2
pip install morfessor
pip install pyicu

の順番でインストール.
ただし,ModuleNotFoundError で icu を入れろと言われた時は

pip install icu

ではなく

pip install pyicu

とする.icu をインストールして使おうとすると cannot import name xxx のエラーが出てくるはず.別物なので注意.

ここでうまくいかない時は,Error installing pip pyicu を見る.

解析していく

公式の Part of Speech Tagging を見て,品詞を調べてみる.

from polyglot.text import Text

blob = "You never fail until you stop trying."
tokens = Text(blob)
print(tokens.pos_tags)

これで文章中の全ての単語の品詞がわかるはずだが,エラーが出るはず.

ValueError: This resource is available in the index but not downloaded, yet. Try to run

polyglot download embeddings2.en

というわけで

git clone https://github.com/web64/nlpserver.git

した後に,nlpserver.py の 14 行目に

app.config['JSON_AS_ASCII'] = False

を追加した後に,

polyglot download embeddings2.en
polyglot download pos2.en

を入れる.この部分は,Not able to pull polyglot files に書いてあった.

これで英語の解析ができるようになったので,先ほどのコードは動き,

from polyglot.text import Text

blob = "You never fail until you stop trying."
tokens = Text(blob)
print(tokens.pos_tags)

の結果として

[('You', 'PRON'), ('never', 'ADV'), ('fail', 'VERB'), ('until', 'SCONJ'), ('you', 'PRON'), ('stop', 'VERB'), ('trying', 'VERB'), ('.', 'PUNCT')]

が得られる.
結果が一行じゃ見にくいから,最後の行を pprint を使って

import pprint
pprint.pprint(tokens.pos_tags)

とすることで

[('You', 'PRON'),
 ('never', 'ADV'),
 ('fail', 'VERB'),
 ('until', 'SCONJ'),
 ('you', 'PRON'),
 ('stop', 'VERB'),
 ('trying', 'VERB'),
 ('.', 'PUNCT')]

とする,など工夫しても良い.
品詞の名前は以下の通り.省略名と説明 (英語) の部分は Part of Speech Tagging から持ってきた.

省略名 説明 (英語) 説明 (日本語)
ADJ adjective 形容詞
ADP adposition 接置詞
ADV adverb 副詞
AUX auxiliary verb 助動詞
CONJ coordinating conjunction 等位接続詞
DET determiner 限定詞
INTJ interjection 間投詞
NOUN noun 名詞
NUM numeral 数詞
PART particle 不変化詞
PRON pronoun 代名詞
PROPN proper noun 固有名詞
PUNCT punctuation 句読点
SCONJ subordinating conjunction 従属接続し
SYM symbol 記号
VERB verb 動詞
X other そのほか

参考

インストールの参考
https://qiita.com/sawada/items/528da0b22546045122b2

polyglot の特徴についての参考
http://lab.astamuse.co.jp/entry/try-polyglot

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6