!!!mod_python + CentOS 5.4 + mecab *投稿者: みゅ *カテゴリ: Python *優先度: 普通 *状態: 完了 *日時: 2010年02月09日 17時57分24秒 //{{bugstate}} !!内容 *mod_pythonでMeCabを使う !!手順 *「[[BugTrack-その他のメモ/14]] - MeCab インストール」に沿って、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 "", 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 >>> *先ほど([[BugTrack-その他のメモ/21]])のフォルダに mecab1.py を作成する # -*- 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("

" + string + "

") def res(req, text): req.content_type = 'text/html; charset=utf-8' req.write("This is a mod_python test program") req.write("") req.write("") #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("") req.write("") return apache.OK def index(req, text=None): html = """\

text:
""" return html *サイトにアクセス. **http://***.***.***.***/test2/mecab1.py/index *うまくいった !!コメント //{{comment}}