実現したいこと
・python-mecab3
に辞書パス(NEologd)を通したい
・Django Webフレームワークにおいて、python-mecab3
と辞書mecab-unidic-neologd
を用いた形態素解析アプリを開発したい
前提
・Django Webフレームワークにて、pyhon-mecab3
とmecab-unidic-neologd
を用いたアプリを開発している
・Django テスト環境では問題なく辞書パス(NEologd)が通り、期待通りの形態素解析ができる
・pip install unidic-lite
をインストールしているため、mecab = MeCab.Tagger()
の場合は問題なく稼働する
・Docker環境になると、表題の通り、辞書パスが通らずに下記のエラーが発生する
・辞書パス(NEologd)を指定しない場合は、Docker環境上でも形態素解析が行えるが、期待された結果にならない
・辞書データについては/reviewer/mecab/dic/mecab-unidic-neologd
に格納している
発生している問題・エラーメッセージ
・Docker環境上で、下記のコードを実行した場合、mecab-python3
に辞書パス(NEologd)が通らずに、下記のエラーを吐き出す
Djangoデバッグで表示されたエラー
1RuntimeError at / 2---------------------------------------------------------- 3 4Failed initializing MeCab. Please see the README for possible solutions: 5 6 https://github.com/SamuraiT/mecab-python3#common-issues 7 8If you are still having trouble, please file an issue here, and include the 9ERROR DETAILS below: 10 11 https://github.com/SamuraiT/mecab-python3/issues 12 13issueを英語で書く必要はありません。 14 15------------------- ERROR DETAILS ------------------------ 16arguments: -d /django/reviewer/mecab/dic/mecab-unidic-neologd 17[pos != std::string::npos] format error: 18---------------------------------------------------------- 19Request Method: POST 20Request URL: http://localhost:8000/ 21Django Version: 4.1.4 22Exception Type: RuntimeError 23Exception Value: 24---------------------------------------------------------- 25 26Failed initializing MeCab. Please see the README for possible solutions: 27 28 https://github.com/SamuraiT/mecab-python3#common-issues 29 30If you are still having trouble, please file an issue here, and include the 31ERROR DETAILS below: 32 33 https://github.com/SamuraiT/mecab-python3/issues 34 35issueを英語で書く必要はありません。 36 37------------------- ERROR DETAILS ------------------------ 38arguments: -d /django/reviewer/mecab/dic/mecab-unidic-neologd 39[pos != std::string::npos] format error: 40---------------------------------------------------------- 41Exception Location: /usr/local/lib/python3.10/site-packages/MeCab/__init__.py, line 135, in __init__ 42Raised during: reviewer.views.home_view 43Python Executable: /usr/local/bin/python 44Python Version: 3.10.10 45Python Path: 46['/django', 47 '/usr/local/lib/python310.zip', 48 '/usr/local/lib/python3.10', 49 '/usr/local/lib/python3.10/lib-dynload', 50 '/usr/local/lib/python3.10/site-packages'] 51Server time: Sat, 18 Feb 2023 19:11:28 +0900
なお、通常ドキュメントに記載されていた通り、パスが通らない場合は、以下の通り[ifs] no such file or directory
と表示される。(パス自体はDjangoテスト環境で通っている。)
------------------- ERROR DETAILS ------------------------ arguments: -d /django/reviewer/mecab/dic [ifs] no such file or directory: /django/reviewer/mecab/dic/dicrc ----------------------------------------------------------
エラーに[pos != std::string::npos] format error:
と表示されていることが関係していると推測される。
該当のソースコード
python
1def mecab_text(text): 2 # MeCabのインスタンスを作成(辞書はmecab-ipadic-neologdを使用) 3 # dicdir = "-d ./reviewer/mecab/dic/mecab-unidic-neologd" # Djangoテスト環境上でのパス 4 dicdir = "-d /django/reviewer/mecab/dic/mecab-unidic-neologd" 5 mecab = MeCab.Tagger(dicdir) 6 7 # 形態素解析 8 node = mecab.parseToNode(text) 9 10 # 形態素解析した結果を格納するリスト 11 wordlist = [] 12 13 while node: 14 # 名詞のみリストに格納する 15 if node.feature.split(",")[0] == "名詞": 16 wordlist.append(node.surface) 17 # 形容詞を取得、elifで追加する 18 elif node.feature.split(",")[0] == "形容詞": 19 wordlist.append(node.surface) 20 # 動詞を取得、elifで追加する 21 # elif node.feature.split(',')[0] == '動詞': 22 # wordlist.append(node.surface) 23 node = node.next 24 return wordlist 25
docker
1FROM python:3.10-buster 2ENV PYTHONBUFFERED=1 3 4WORKDIR /django 5COPY requirements.txt requirements.txt 6 7RUN pip3 install -r requirements.txt 8COPY . . 9 10CMD python manage.py runserver 0.0.0.0:8000
yml
1version: "3.10" 2services: 3 app: 4 build: . 5 volumes: 6 - .:/django 7 ports: 8 - 8000:8000 9 image: ez-app-reviewer-repository:latest 10 container_name: django_container 11 command: python manage.py runserver 0.0.0.0:8000
試したこと
・python-mecab3のドキュメントの通読
https://github.com/SamuraiT/mecab-python3
・先行事例の検索
・Dockerfileの書き換え
・ChatGPTへの質問
最後に
一日ほど方法を探しましたが、解決策が見つかりませんでしたので質問させていただきました。
お手数ではございますが、何卒よろしくお願いいたします。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2023/02/19 02:26 編集