166
118

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.

ubuntu 21.10 に mecab をインストール

Last updated at Posted at 2017-06-23
sudo apt install mecab
sudo apt install libmecab-dev
sudo apt install mecab-ipadic-utf8

この段階で動作確認

$ mecab
特急はくたか
特急    名詞,一般,*,*,*,*,特急,トッキュウ,トッキュー
は      助詞,係助詞,*,*,*,*,は,ハ,ワ
く      動詞,自立,*,*,カ変・クル,体言接続特殊2,くる,ク,ク
た      助動詞,*,*,*,特殊・タ,基本形,た,タ,タ
か      助詞,副助詞/並立助詞/終助詞,*,*,*,*,か,カ,カ
EOS

neologd をインストール

git clone https://github.com/neologd/mecab-ipadic-neologd.git
cd mecab-ipadic-neologd
sudo bin/install-mecab-ipadic-neologd

/etc/mecabrc を編集する

dicdir = /usr/lib/x86_64-linux-gnu/mecab/dic/mecab-ipadic-neologd

この段階で動作確認

$ mecab
特急はくたか
特急    名詞,一般,*,*,*,*,特急,トッキュウ,トッキュー
はくたか        名詞,固有名詞,一般,*,*,*,はくたか,ハクタカ,ハクタカ
EOS

python3 で使えるようにする

sudo apt install swig
sudo apt install python3-pip
sudo pip3 install mecab-python3

サンプルのプログラム

mecab_sample.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#	mecab_sample.py
#
#						Jun/02/2018
#
# ----------------------------------------------------------------------
import sys
import MeCab
#
mt = MeCab.Tagger("mecabrc")
str_in="特急はくたかで富山に向かいます。それから、金沢に行って、兼六園に行きます。"
res = mt.parseToNode(str_in)

while res:
#    print (res.surface)
    arr = res.feature.split(",")
    if (arr[1] == "固有名詞"):
        print(res.feature)
        print(arr[6])
    res = res.next
#
# ----------------------------------------------------------------------

実行結果

$ ./mecab_sample.py 
名詞,固有名詞,一般,*,*,*,はくたか,ハクタカ,ハクタカ
はくたか
名詞,固有名詞,地域,一般,*,*,富山,トヤマ,トヤマ
富山
名詞,固有名詞,地域,一般,*,*,金沢,カナザワ,カナザワ
金沢
名詞,固有名詞,地域,一般,*,*,兼六園,ケンロクエン,ケンロクエン
兼六園

次の環境で確認しました。

$ uname -a
Linux iwata 5.13.0-21-generic #21-Ubuntu SMP Tue Oct 19 08:59:28 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

$ cat /etc/os-release 
PRETTY_NAME="Ubuntu 21.10"
NAME="Ubuntu"
VERSION_ID="21.10"
VERSION="21.10 (Impish Indri)"
VERSION_CODENAME=impish
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=impish

次のエラーが出た場合

------------------- ERROR DETAILS ------------------------
arguments: mecabrc
[ifs] no such file or directory: /usr/local/etc/mecabrc
----------------------------------------------------------

/usr/local/etc/mecabrc を作成します。

/usr/local/etc/mecabrc
dicdir = /usr/lib/x86_64-linux-gnu/mecab/dic/mecab-ipadic-neologd
166
118
6

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
166
118

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?