堀 浩一

Home About

   English

MeCabを使っていると、"'utf-8' codec can't decode byte 0xfa in position 0: invalid start byte" というエラーが起こる

(老いぼれ人工知能研究者のプログラミング日記)

2019年5月14日

形態素解析に mecab-python3 を使っています。

どういうわけかわからないのですが、 "'utf-8' codec can't decode byte 0xfa in position 0: invalid start byte" というエラーが、再現性なく起こります。

検索してみましたらところ、その原因は誰もわかっていらっしゃらないようなのですが、所望のparsingを行う前にnull stringをparseさせたらそのエラーを回避できる、と複数の方々が書かれていました。
試してみましたら、確かにうまく行きました。

下の通りです。


import MeCab

def extractNouns(text):
    tagger = MeCab.Tagger()
    normallyprocessed = True

    tagger.parse("")
    # No one seems to know why this works,
    # but this tagger.parse("") can avoid the unicode decoding error
    # in the following parsing.
    
    node = tagger.parseToNode(text)
    keywords = []
    while node:
        try:
            word = node.surface
        except Exception as e:
            print(str(e))
            print('parsing error occured but ignored')
            normallyprocessed = False
        if normallyprocessed and word.isalpha():
            meta = node.feature.split(",")
            if meta[0] == '名詞':
               keywords.append(word)
        node = node.next
        normallyprocessed = True
    return keywords



追記: その後、教え子から、この問題の原因はgarbage collectionだ、という情報を教えてもらいました。その説明は、ここに載っています。


CC0
To the extent possible under law, the person who associated CC0 with this work has waived all copyright and related or neighboring rights to this work.




関連項目(自動計算):
WindowsのコマンドプロンプトでUnicodeを使う
モバイル端末のための css <pre> と <code>
Ruby on RailsでURLをredirectする
WindowsからMacにVNCで接続しようとするとlogin windowが固まる
Google検索の結果にfaviconを表示する
WindowsでPythonを使う
FacebookボタンとTwitterボタンをきちんと並べる
五七五, 五七五七七などの俳句や短歌みたいな語列をテキストから抽出するプログラム
長崎原爆の日
機械屋と電気屋では振動のメンタルモデルが異なる
UAV/UGV Autonomous Cooperation
UNESCO: `Do you know AI or AI knows you better? Thinking Ethics of AI'
堀 浩一: 最終講義
人工知能として認識されない人工知能の埋め込まれる社会に向けて
倫理的AIの設計を支援するためのAI
映画「グリーンブック」を観て、年寄り人工知能研究者が考えること - 人種その他の差別とAI(人工知能)について
文化国家としての技術立国 ー 猪瀬博教授の思い出
AI ELSI賞
AI(人工知能)と哲学
東京大学学術資産等アーカイブズポータル ー UTokyo Digital Collections
工学と理学の違い
文明と文化と科学と技術
堀 浩一 ホーム
人工知能とは
堀 浩一