16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Google Colab で MeCab と CaboCha を使う最強の方法

Last updated at Posted at 2020-05-06

実際最強かは知らないけども、自分的にこれだなって思っている方法

Google Colab の コードセルにコピペで動きます(たぶん)。make に少し時間がかかるのでコーヒーでも飲んで待ちましょう☕️

Gist ソースコード

google_colab.ipynb
%%bash
# mecabとmecab-python3の依存関係をインストール
apt-get install mecab swig libmecab-dev mecab-ipadic-utf8
# mecab-pythonのインストール
pip install mecab-python3

# crfppダウンロード(cabochaの依存関係)
curl -sL -o CRF++-0.58.tar.gz "https://drive.google.com/uc?export=download&id=0B4y35FiV1wh7QVR6VXJ5dWExSTQ"
tar -zxf CRF++-0.58.tar.gz
# crfppインストール
cd CRF++-0.58
./configure && make && make install && ldconfig
cd ..

# cabochaダウンロード
url="https://drive.google.com/uc?export=download&id=0B4y35FiV1wh7SDd1Q1dUQkZQaUU"
curl -sc /tmp/cookie ${url} >/dev/null
code="$(awk '/_warning_/ {print $NF}' /tmp/cookie)"
curl -sLb /tmp/cookie ${url}"&confirm=${code}" -o cabocha-0.69.tar.bz2
tar -jxf cabocha-0.69.tar.bz2
# cabochaインストール
cd cabocha-0.69
./configure -with-charset=utf-8 && make && make check && make install && ldconfig
# cabocha-pythonのインストール
pip install python/
cd ..

動作検証

%%bash
mecab -v
pip show mecab-python3 | grep -e Name -e Version && echo
cabocha -v && echo
pip show cabocha-python | grep -e Name -e Version 
output
mecab of 0.996

Name: mecab-python3
Version: 0.996.5

cabocha of 0.69

Name: cabocha-python
Version: 0.69
import MeCab
tagger = MeCab.Tagger()
print(tagger.parse("隣の客はよく柿食う客だ"))
output
隣	名詞,一般,*,*,*,*,隣,トナリ,トナリ
の	助詞,連体化,*,*,*,*,の,ノ,ノ
客	名詞,一般,*,*,*,*,客,キャク,キャク
は	助詞,係助詞,*,*,*,*,は,ハ,ワ
よく	副詞,一般,*,*,*,*,よく,ヨク,ヨク
柿	名詞,一般,*,*,*,*,柿,カキ,カキ
食う	動詞,自立,*,*,五段・ワ行促音便,基本形,食う,クウ,クウ
客	名詞,一般,*,*,*,*,客,キャク,キャク
だ	助動詞,*,*,*,特殊・ダ,基本形,だ,ダ,ダ
EOS
import CaboCha
cp = CaboCha.Parser()
print(cp.parseToString("隣の客はよく柿食う客だ"))
output
隣の-D        
  客は-------D
    よく---D |
        柿-D |
        食う-D
          客だ
EOS
16
14
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
16
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?