gensim+mecabでコーパスと辞書を作成
gensimでコーパスと辞書を簡単に作成できる機能があったためメモ mecabやgensimの参考は以下
ソース
from gensim import corpora, models import MeCab NEOLOGD="/usr/lib/mecab/dic/mecab-ipadic-neologd/" tagger=MeCab.Tagger("-Owakati"+" -d "+NEOLOGD) docs1="統計的潜在意味解析という手法を用います。ざっくり言うと、文書がどんなトピックを持っている か、何に関する文書なのか、を推定してくれるものです。" docs2="統計的潜在意味解析が必要な場面では、PCのスペック不足が気になることはあまりない" docs1=tagger.parse(docs1) docs2=tagger.parse(docs2) wordlist1=docs1.split(" ") wordlist2=docs2.split(" ") words=[wordlist1,wordlist2] dictionary = corpora.Dictionary(words) dictionary.save_as_text('./text.dict') corpus = [dictionary.doc2bow(word) for word in words] corpora.MmCorpus.serialize('./text.mm', corpus)
辞書
単語ID 単語 出現回数
2 0 2 32 PC 1 1 、 2 2 。 1 33 あまり 1 3 いる 1 4 か 1 5 が 2 6 くれる 1 34 こと 1 7 ざっくり 1 8 し 1 9 て 1 35 で 1 10 です 1 11 と 1 12 という 1 13 どんな 1 14 な 2 36 ない 1 37 なる 1 38 に 1 15 に関する 1 16 の 2 39 は 1 17 ます 1 18 もの 1 19 を 1 40 スペック 1 20 トピック 1 41 不足 1 21 何 1 42 場面 1 43 必要 1 22 意味 2 23 手法 1 24 持っ 1 25 推定 1 26 文書 1 44 気 1 27 潜在 2 28 用い 1 29 統計的 2 30 解析 2 31 言う 1
コーパス
doc_id word_id 出現頻度
%%MatrixMarket matrix coordinate real general 2 45 54 1 1 1 1 2 3 1 3 2 1 4 1 1 5 2 1 6 1 1 7 1 1 8 1 1 9 1 1 10 2 1 11 1 1 12 1 1 13 1 1 14 1 1 15 1 1 16 1 1 17 1 1 18 1 1 19 1 1 20 3 1 21 1 1 22 1 1 23 1 1 24 1 1 25 1 1 26 1 1 27 2 1 28 1 1 29 1 1 30 1 1 31 1 1 32 1 2 1 1 2 2 1 2 6 2 2 15 1 2 17 1 2 23 1 2 28 1 2 30 1 2 31 1 2 33 1 2 34 1 2 35 1 2 36 1 2 37 1 2 38 1 2 39 1 2 40 2 2 41 1 2 42 1