トップ 差分 一覧 ソース 検索 ヘルプ RSS ログイン

BugTrack-その他のメモ/22

R備忘録 /状態空間モデリング/donlp2/その他のメモ

mod_python + CentOS 5.4 + mecab

  • 投稿者: みゅ
  • カテゴリ: Python
  • 優先度: 普通
  • 状態: 完了
  • 日時: 2010年02月09日 17時57分24秒

内容

  • mod_pythonでMeCabを使う

手順

echo "mod_pythonでMeCabを使う" | mecab
mod     名詞,固有名詞,組織,*,*,*,*
_       名詞,サ変接続,*,*,*,*,*
python  名詞,一般,*,*,*,*,*
で      助詞,格助詞,一般,*,*,*,で,デ,デ
MeCab   名詞,一般,*,*,*,*,*
を      助詞,格助詞,一般,*,*,*,を,ヲ,ヲ
使う    動詞,自立,*,*,五段・ワ行促音便,基本形,使う,ツカウ,ツカウ
EOS
  • mecab-Pythonをインストールする
tar xvf mecab-python-0.98.tar.gz
cd mecab-python-0.98
su
python setup.py install
  • 動作確認

$ python

Python 2.4.3 (#1, Sep  3 2009, 15:37:12)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MeCab
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "MeCab.py", line 28, in ?
    import _MeCab
ImportError: libmecab.so.1: cannot open shared object file: No such file or directory
  • うん、パスが通っていないぽく.
  • 適当なところ(例えば/etc/bashrc)に以下を追加
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
  • (シェルを立ち上げなおして)うまくいく.
$ python
Python 2.4.3 (#1, Sep  3 2009, 15:37:12)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-46)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MeCab
>>>
# -*- encoding: utf-8 -*-
from mod_python import apache
#import smtplib
import sys
import re
from sets import Set
import MeCab

kaigyo = re.compile('\n')
mc = MeCab.Tagger('-l1')


def writeP(req, string):
	req.write("<p>" + string + "<p>")

def res(req, text):
	req.content_type = 'text/html; charset=utf-8'
	req.write("<title>This is a mod_python test program</title>")
	req.write("<html>")
	req.write("<body>")
	#writeP(req, text)
	sp = mc.parseNBest(2, text)
	#writeP(req, sp)
	sp1 = kaigyo.split(sp)
	while 'EOS' in sp1:
		sp1.remove('EOS')
	for mm in sp1:
		writeP(req, mm)
	
	req.write("</body>")
	req.write("</html>")
	return apache.OK

def index(req, text=None):
	html = """\
<html>
<head><META HTTP-EQUIV="content-type" CONTENT="text/html; charset=utf-8"></head>
<p>
<form action="res" method="POST">
	text:    <input type="text" name="text" size="80"><br>
	<input type="submit">
</form>
</html>
"""
	return html

コメント