python Mecabを使うときの注意点

プログラミング

下記で動くようにするには少し工夫が必要です。

import MeCab

def extract_words(text):
    # MeCabを使って形態素解析を行う
    tagger = MeCab.Tagger("")
    tagger.parse('')
    node = tagger.parseToNode(text)

    # 動詞、名詞、形容詞だけを抽出する
    extracted_words = []
    while node:
        part_of_speech = node.feature.split(',')[0]
        if part_of_speech in ['動詞', '名詞', '形容詞']:
            extracted_words.append(node.surface)
        node = node.next

    return extracted_words
   

やり方

私にはこんなエラーが出ました。

 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:
[ifs] no such file or directory: c:\mecab\mecabrc
----------------------------------------------------------

私の環境では、pip install Mecabではだめでした。下記でインストールします。

pip install mecab-python3 

そのあとに下記も実行します。

pip install unidic-lite

人によっては下記が必要な可能性はあります。

python -m unidic download

エラーが出たら公式ドキュメントを見ましょう!chatGPTに聞いてもいいかもね。

コメント

タイトルとURLをコピーしました