Pythonで、ツイートを格納したテキストをMeCab(+natto)で形態素解析し、抽出した名詞のtf-idfのスコアを出して並べ替えたいです。コードを走らせた結果、以下のエラーが出ました。
プログラミングを始めたばかりで頼れる人もおらず、何が起きていて、どのように直せばいいのか本当に分からず、質問させていただきました。
お知恵をお貸しいただけませんでしょうか? 宜しくお願いします。
Traceback (most recent call last): File "tfidf_test_dataset.py", line 41, in <module> tfidf = vectorizer.fit_transform(corpus) File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py", line 1652, in fit_transform X = super().fit_transform(raw_documents) File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py", line 1058, in fit_transform self.fixed_vocabulary_) File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py", line 970, in _count_vocab for feature in analyze(doc): File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py", line 352, in <lambda> tokenize(preprocess(self.decode(doc))), stop_words) File "/Users/macuser/Workspaces/jxpress/trendword/.direnv/python-3.7.3/lib/python3.7/site-packages/sklearn/feature_extraction/text.py", line 256, in <lambda> return lambda x: strip_accents(x.lower()) AttributeError: 'generator' object has no attribute 'lower'
該当のソースコード
python
1from natto import MeCab 2import codecs 3import sys 4import re 5from sklearn.feature_extraction.text import TfidfVectorizer 6import numpy as np 7#ファイル読み込み 8with codecs.open("tfidf_test.txt", "r", "utf-8") as f: 9 corpus = f.read().split("\n") 10 11mecab = MeCab('-d /usr/local/lib/mecab/dic/mecab-ipadic-neologd') 12#形態素解析 13#if tagger.lang == 'ja': 14for txt in corpus: 15 words = mecab.parse(txt, as_nodes=True) 16 17 for w in words: 18 rm_list = ["RT","https","co"] 19 if w.feature.split(",")[0] == "名詞": 20 if len(w.surface) >= 2: 21 if not any(rm in w.surface for rm in rm_list): 22 print(str(w.surface)) 23 else: 24 print("") 25 else: 26 print("") 27 else: 28 print("") 29 30corpus = [mecab.parse(txt, as_nodes=True) for line in corpus] 31#tf-idf計算 32vectorizer = TfidfVectorizer() 33tfidf = vectorizer.fit_transform(corpus) 34 35#スコアの表示 36print(tfidf.toarray()) 37# テキストの数、出現した単語の数 38print(tfidf.shape) 39 40#並べ替え 41feature_names = np.array(vectorizer.get_feature_names()) 42for vec in tfidf: 43 index = np.argsort(vec.toarray(), axis=1)[:,::-1] 44 feature_words = feature_names[index] 45 print(feature_words[:,:10])
txt
1自転車やバイクで世界を回っている男性が必死で追いかけてくる子猫と出会い、彼の旅を変えたおはなし 2 3京都吹奏楽コンクール高校生小編成の部で金賞をとることができました!ここまで支えてくださった方々のおかげです沢山の応援ありがとうござました 4 5今年も平谷村役場裏のひまわり畑で撮影しました。撮影した殆どの写真が変顔の自分。いちばんまともな顔の写真を。どこにいるかわかりにくいですが
補足情報(FW/ツールのバージョンなど)
iOS 10.12.6, Python 3.7.3, Atom

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/15 05:47