前提・実現したいこと
fasttextで文章の分類を行おうと試みているのですが、いろんな方のサイトを参考にしてみてもうまくいきません...
発生している問題・エラーメッセージ
Traceback (most recent call last): File "prediction.py", line 44, in <module> pre.tweet_class("".join(sys.argv[1:])) File "prediction.py", line 32, in tweet_class estimate = self.classifier.predict_proba([words], k=3)[0][0] IndexError: list index out of range
該当のソースコード
import sys import fasttext as ft import MeCab class predict: def __init__(self): # モデル読み込み self.classifier = ft.load_model('model.bin') def get_surfaces(self, content): """ 文書を分かち書き """ tagger = MeCab.Tagger('') tagger.parse('') surfaces = [] node = tagger.parseToNode(content) while node: surfaces.append(node.surface) node = node.next return surfaces def tweet_class(self, content): """ ツイートを解析して分類を行う """ words = " ".join(self.get_surfaces(content)) estimate = self.classifier.predict_proba([words], k=3)[0][0] if estimate[0] == "__label__1,": print('美容系', estimate[1]) elif estimate[0] == "__label__2,": print('エンタメ系', estimate[1]) elif estimate[0] == "__label__3,": print('暮らし系', estimate[1]) if __name__ == '__main__': pre = predict() pre.tweet_class("".join(sys.argv[1:]))
試したこと
$ python3 prediction.py "私は犬が好きだ"
と実行したところこのようなエラーが出てしまいます。
補足情報(FW/ツールのバージョンなど)
Python 3.7.2
fasttext 0.8.3
を使用しています。またmodel.binを生成するコードは以下の通りです。
import sys import fasttext as ft argvs = sys.argv input_file = argvs[0] output_file = argvs[1] classifier = ft.supervised(input_file, output_file)
あなたの回答
tips
プレビュー