SamuraiT / mecab-python3

:snake: mecab-python. you can find original version here:http://taku910.github.io/mecab/
https://pypi.python.org/pypi/mecab-python3
Other
528 stars 51 forks source link

no such file or directory: /mnt/integration_test/outputs/custom.dic #89

Closed paulhindemith closed 1 year ago

paulhindemith commented 1 year ago

現在の状況

mecab-python3 を利用して MeCab で形態素解析を行うアプリケーションを開発しています。 mecab のオプション -u を利用して /mnt/integration_test/outputs/custom.dic に存在するユーザー辞書を利用した形態素解析を行いたいです。 上記のパスにはファイルが確かに存在し、直前に os.path.exists('/mnt/integration_test/outputs/custom.dic') としても True となるので間違いなくファイルは存在するのですが、下記の通り no such file or directory: /mnt/integration_test/outputs/custom.dic のエラーが出てしまいます。

エラーが起きている環境は、python:3.11.1-bullseye イメージを利用したコンテナ内で起きています。 この現象はローカルの Windows Docker 環境では再現できないのですが、Google Cloud の Cloud Run を利用した場合に起きております。

なぜこのようなエラーになってしまうのか考えうる原因について、お力を貸していただけませんでしょうか。

Error Message

----------------------------------------------------------

Failed initializing MeCab. Please see the README for possible solutions:

    https://github.com/SamuraiT/mecab-python3#common-issues

If you are still having trouble, please file an issue here, and include the
ERROR DETAILS below:

    https://github.com/SamuraiT/mecab-python3/issues

issueを英語で書く必要はありません。

------------------- ERROR DETAILS ------------------------
arguments: -d "/usr/local/lib/mecab/dic/mecab-ipadic-neologd" -u "/mnt/integration_test/outputs/custom.dic" -F "%m,%c,%H
"
) [tokenizer_->open(param)] tokenizer.cpp(127) [d->open(dicfile[i])] dictionary.cpp(79) [dmmap_->open(file, mode)] no such file or directory: /mnt/integration_test/outputs/custom.dic 
----------------------------------------------------------
polm commented 1 year ago

ご利用ありがとうございます。すみませんが、このようなエラーは見たことがありません……

paulhindemith commented 1 year ago

ご返信いただきありがとうございますmm

頂いたご質問に一つ一つお答えしますと

ファイルを読む権限はありますか?

はい、問題のコードの直前に、os.access("/mnt/integration_test/outputs/custom.dic", os.R_OK) と権限の確認コードを仕込みましたが、True でした。

CLIからMeCabに同じ引数を渡すとうまく動作しますか?

動作しませんでした。同じく no such file or directory のエラーになりました。mecab-python3 ではなく、mecab の動作が意図しないものになるみたいですね...

-u の引数だけを使うと動作しますか?

CLIからMeCabに -u の引数のみ指定しましたが、 no such file or directory のエラーになりました。

上記のエラーは Cloud Run でのみ発生しており、Windows のローカルで実行した docker 環境ではエラーにならないのです...

何かアーキテクチャの違いがでるのでしょうか...

引数の文字列を作成するコードは共有できますか?もしかして括弧が問題になっているかもしれません。

アプリのコードは以下で、

self.mecab = MeCab.Tagger(f'-d "{DIC_DIR_PATH}" -u "{userdic}" -F "{FORMAT}"')

デバッグのために書いた以下もあります。

res = subprocess.run(
    f'echo "あ" | mecab -u "/mnt/integration_test/outputs/custom.dic"',
    shell=True,
    capture_output=True)
ctx.logger.info(f"stdout: {res.stdout.decode('utf-8')}")

どちらもエラーになりました。

paulhindemith commented 1 year ago

Windows からではなく、Mac(M1、M2 でない)のローカル環境からもMeCab コマンドを試しましたが、上記のエラーは再現できませんでした。 Google Cloud の Cloud run と MeCab との相性が悪いような気がします。

paulhindemith commented 1 year ago

こちら解決いたしましたmm

/mnt を WORKDIR として設定している /app に設定することで無事参照可能になりました。 Python からは参照できていたので非常にわかりにくかったのですが、どうも /mnt 以下はなにがしかの制御が行われているみたいです。

もうやめてしまおうかと思っていたのですが、ご相談乗っていただき希望が湧きました。

どうもありがとうございましたmm

polm commented 1 year ago

ご報告ありがとうございます、解決できてなによりです!