LoginSignup
2

More than 5 years have passed since last update.

形態素解析してみた(Python初心者)

Posted at

1.Pythonインストール

とりあえず打ってみた

python --version
Python 2.7.10

入門者必見python2と3、どっちを学習すべき?違いを徹底解説!
【超初心者向け】MacにPython 3をインストールする方法+アンインストール方法

上記の2つの記事を見てpython3をインストールすることに。

まずhomebrewインストール

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

homebrewの説明はここ
【決定版】MacでPythonを使って『機械学習』を学ぶための環境構築

できた!!

python3 -V
Python 3.7.1

2.Mecab導入

日本語を形態素解析

MeCab

MeCabで使用できる言語はC、C#、C++、Java、Perl、Python、Ruby、Rとたくさんあります。
また、さまざまな辞書と連結させることもできるため、日本語の形態素解析エンジンの中では最も良く使われています。

juman

jumanの特徴として挙げられるのは、文字コードであるUTF-8に対応している点、またWEBテキストから自動獲得された辞書、
Wikipediaから抽出された辞書を使用できる点です。
なお、jumanは同じ形態素解析ツールであるMeCabよりも単語の意味分類を細かく実施します。

英語を形態素解析

Tree Tagger

英語の形態素解析ができるツールとして有名なのは「Tree Tagger」です。
Tree Taggerは英語だけではなくドイツ語、フランス語、スペイン語など様々な言語に対応しています。
また、Tree TaggerはWindows、Mac、Linuxどの環境でも使用することができます。
またPythonからTree Taggerを使用することも可能です。
そしてTree Taggerを使用して単語を分類した時に品詞コードが表示されます。
表示される品詞コードの日本語訳の一覧はこちらで確認できます。

NLTK

NLTKとはNatural Language Tool Kitの略称で、Python用ライブラリになります。
NLTKは、品詞のタグ付けに構文解析、また意味解析などが簡単にできるのが大きな特徴です。
また、NLTKは文字列を対象としたデータマイニングであるテキストマイニングにも利用できます。

結局

数多くあるから以前会社で使っていたMecabを使うことに。

参考
Python3からMeCabを使う

git clone --depth 1 git@github.com:neologd/mecab-ipadic-neologd.git
Cloning into 'mecab-ipadic-neologd'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

githubのSSHの設定がだめだった。
Permission denied

わかりやすい説明
https://qiita.com/tedkuma/items/3b539e79010a4808337c

git clone --depth 1 https://github.com/neologd/mecab-ipadic-neologd.git

場所確認

echo `mecab-config --dicdir`"/mecab-ipadic-neologd"

3.形態素解析してみた

Python3で形態素解析エンジンMeCabを使ってみた
上記でコードを書いても、whileのところがなにかおかしかった。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import MeCab

sentence = """AWSの有名なサービスにAmazon Elastic Compute Cloud (EC2) とAmazon Simple Storage Service (S3) がある。"""

#t = MeCab.Tagger('')
#print(t.parse(sentence))

#t = MeCab.Tagger('-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd')
#print(t.parse(sentence))


t = MeCab.Tagger('-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd')
t.parse('')
m = t.parseToNode(sentence)


while m:
    if m.feature.split(',')[0] == '名詞':
        print(m.surface)
    m = m.next
AWSの有名なサービスにAmazon Elastic Compute Cloud (EC2) とAmazon Simple Storage Service (S3) がある。
有名なサービスにAmazon Elastic Compute Cloud (EC2) とAmazon Simple Storage Service (S3) がある。
サービスにAmazon Elastic Compute Cloud (EC2) とAmazon Simple Storage Service (S3) がある。
Amazon Elastic Compute Cloud (EC2) とAmazon Simple Storage Service (S3) がある。
Elastic Compute Cloud (EC2) とAmazon Simple Storage Service (S3) がある。
Compute Cloud (EC2) とAmazon Simple Storage Service (S3) がある。
Cloud (EC2) とAmazon Simple Storage Service (S3) がある。
EC2) とAmazon Simple Storage Service (S3) がある。
Amazon Simple Storage Service (S3) がある。
Simple Storage Service (S3) がある。
Storage Service (S3) がある。
Service (S3) がある。
S3) がある。

python初心者はここでつまずいた。

参考
http://tech.innovation.co.jp/2017/07/28/mecab.html
https://qiita.com/kazuhikoyamashita/items/d4d2d08c8e80ead17c8b
https://dev.classmethod.jp/server-side/mecab-using-python3-ja/

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
2