実現したいこと
・弊プロジェクトで現在、アンケート調査で集計した自由回答部分を使って共起語ネットワークを構築し、画像で出力すればインサイトなどが見えるのでは?という仮説に基づいて以下のコードのように共起語ネットワークを組んでいます。
前提
アンケート調査で集めた自由回答をCSVの同じ列にまとめ、そこから特徴的なワードや類語を共起語ネットワークの形に起こし、マーケティングに役立てていきたいと思っています。
発生している問題・エラーメッセージ
RuntimeError Traceback (most recent call last) File ~\anaconda3\lib\site-packages\MeCab\__init__.py:133, in Tagger.__init__(self, rawargs) 132 try: --> 133 super(Tagger, self).__init__(args) 134 except RuntimeError as ee: RuntimeError: The above exception was the direct cause of the following exception: RuntimeError Traceback (most recent call last) Cell In[4], line 12 9 raw_df = pd.read_csv("Zgen-Book1.csv", encoding="utf-8") 10 raw_text = raw_df['comments'].values ---> 12 tagger = MeCab.Tagger("-Ochasen") 14 word_list = [] 15 for text in raw_text: File ~\anaconda3\lib\site-packages\MeCab\__init__.py:135, in Tagger.__init__(self, rawargs) 133 super(Tagger, self).__init__(args) 134 except RuntimeError as ee: --> 135 raise RuntimeError(error_info(rawargs)) from ee RuntimeError: ---------------------------------------------------------- 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: -Ochasen [!tmp.empty()] unknown format type [chasen] ----------------------------------------------------------
該当のソースコード
Python
1import MeCab 2import pandas as pd 3import numpy as np 4import networkx as nx 5from collections import Counter 6import matplotlib.pyplot as plt 7 8# データを読み込みます 9raw_df = pd.read_csv("ファイル名.csv", encoding="utf-8") 10raw_text = raw_df['comments'].values 11 12tagger = MeCab.Tagger("-Ochasen") 13 14word_list = [] 15for text in raw_text: 16 node = tagger.parse(text).split("\n") 17 for word in node: 18 features = word.split("\t") 19 # 名詞のみ取得する 20 if features[0] == "EOS" or features[0] == "": 21 continue 22 elif features[3].startswith('名詞'): 23 word_list.append(features[0]) 24 25# 単語の共起を計算します 26word_counter = Counter(word_list) 27word_to_id = {word: i for i, word in enumerate(word_counter.keys())} 28id_to_word = {i: word for word, i in word_to_id.items()} 29 30co_occurrence_matrix = np.zeros((len(word_to_id), len(word_to_id)), dtype=np.int32) 31 32# 共起行列を作成する 33for i, text in enumerate(raw_text): 34 words = text.split("\t") 35 for j in range(len(words) - 1): 36 if words[j] in word_to_id.keys() and words[j+1] in word_to_id.keys(): 37 co_occurrence_matrix[word_to_id[words[j]], word_to_id[words[j+1]]] += 1 38 co_occurrence_matrix[word_to_id[words[j+1]], word_to_id[words[j]]] += 1 39 40# networkxのグラフオブジェクトを作成します。 41G = nx.from_numpy_matrix(co_occurrence_matrix) 42 43# ネットワークを描画します。 44plt.figure(figsize=(10, 10)) 45nx.draw_networkx(G, with_labels=False, node_size=100, node_color="blue", alpha=0.6, edge_color="gray", font_size=16) 46plt.axis('off') 47plt.show()
試したこと
・tagger = MeCab.Tagger("-Ochasen")部分の撤去(解消されませんでした)
・
補足情報(FW/ツールのバージョンなど)
・自環境はwindows10、バージョンは21H2(OSビルド19044.3086)で、Anaconda Navigator上でJupiter Notebookを用いてコードを書いています。
同様質問あるので参考にして質問修正または自己解決ください。
https://teratail.com/questions/371343
