#第5第6回コード例 #################################################### #p7 #MeCabのインストール !pip install mecab-python3 #UniDic辞書のインストールとダウンロード !pip install unidic !python -m unidic download #################################################### #p8 #MeCabとunidicを使用するためのインポート文 import MeCab import unidic #文書を形態素に分けてタグ付けする機能を持つオブジェクト(タガー)を取得する tagger = MeCab.Tagger() #parseメソッドで形態素解析を実行する print(tagger.parse("静岡県立大学でpythonの講座を受講しています。")) #################################################### #p10(インストールは現在難しくなっています) #neologd辞書のインストール !apt install mecab libmecab-dev git make curl xz-utils file !git clone --depth 1 https://github.com/neologd/mecab-unidic-neologd.git !echo yes | mecab-unidic-neologd/bin/install-mecab-unidic-neologd -n #################################################### #p11(p10が成功した場合に実行してください) #辞書の指定 dic = "-d /usr/lib/x86_64-linux-gnu/mecab/dic/mecab-unidic-neologd" tagger = MeCab.Tagger(dic) print(tagger.parse("静岡県立大学でpythonの講座を受講しています。")) #################################################### #p12 node = tagger.parseToNode("今日はとても寒いです。") while node: print(node.surface, "\t", node.feature) node = node.next #################################################### #p15 #pandasのインポート import pandas as pd file = pd.ExcelFile("comment.xlsx") data = file.parse("回答") data.head() #################################################### #p16 comments = data["自由回答"].tolist() #自由回答の列をtolist()でリスト型に変換 words = [] #単語リスト(最初は空) for c in comments: #コメントを1つずつcに取り出して繰り返し node = tagger.parseToNode(c) #形態素解析を実行(最初の形態素がnodeに入る) #################################################### #p17 comments = data["自由回答"].tolist() #自由回答の列をtolist()でリスト型に変換 words = [] #単語リスト(最初は空) for c in comments: #コメントを1つずつcに取り出して繰り返し node = tagger.parseToNode(c) #形態素解析を実行(最初の形態素がnodeに入る) while node: #形態素がなくなるまで繰り返し hinshi = node.feature.split(",")[0] #品詞を抽出 if hinshi in ["名詞"]: #品詞が名詞ならば words.append(node.surface) #wordsリストに表層形を追加 node = node.next #次のnodeを新たなnodeとする print(len(words)) #単語の件数 print(words[:20]) #最初の20件の単語を表示 #################################################### #p18 from collections import Counter #Counter関数のインポート hindo = Counter(words) #単語の出現頻度の集計 print(hindo) #################################################### #p19 for p in hindo.most_common(): if p[1] > 20: print(p[0], "\t", p[1]) #################################################### #p22 import matplotlib.pyplot as plt from wordcloud import WordCloud #WordCloudクラスのインポート #フォントファイルの指定(どちらか2種類切り替え) #fpath = "ipaexg.ttf" fpath = "ZenAntique-Regular.ttf" #WordCouldオブジェクトの生成 wordcloud = WordCloud(background_color="white", #背景色 width=600, #横幅 height=500, #高さ max_font_size=150, #フォントの最大サイズ font_path=fpath, #フォントファイル colormap="plasma") #色合い wordcloud.generate_from_frequencies(hindo) #hindoを元にワードクラウドを生成 plt.figure(figsize=(15,12)) #wordcloud画像の出力サイズ指定(幅と高さの実寸をインチ指定) plt.imshow(wordcloud) #wordcloud画像を出力 plt.axis("off") #縦軸と横軸は非表示 plt.show() #出力の確定 #################################################### #p25 swords = [] #ストップワードの一覧リスト(最初は空) f = open("stopword.txt") #ストップワードの一覧ファイルを開く txt = f.readlines() #ストップワードの一覧ファイルを行毎に読みtxtリストに格納 f.close() #ストップワードの一覧ファイルを閉じる print(txt) #確認用 swords = [line.strip() for line in txt] #ストップワードの行末の改行文字「¥n」をstripメソッドで取る print(swords) #確認用 comments = data["自由回答"].tolist() #自由回答の列をtolist()でリスト型に変換 words = [] #単語リスト(最初は空) for c in comments: #コメントを1つずつcに取り出して繰り返し node = tagger.parseToNode(c) #形態素解析を実行(最初の形態素がnodeに入る) while node: #形態素がなくなるまで繰り返し hinshi = node.feature.split(",")[0] #品詞を抽出 #品詞が名詞かつスットプワードで無いならば if hinshi in ["名詞"] and not node.surface in swords: words.append(node.surface) #wordsリストに表層形を追加 node = node.next #次のnodeを新たなnodeとする print(len(words)) #単語の件数 print(words[:20]) #最初の20件の単語を表示 #################################################### #p28 swords = [] #ストップワードの一覧リスト(最初は空) f = open("stopword.txt") #ストップワードの一覧ファイルを開く txt = f.readlines() #ストップワードの一覧ファイルを行毎に読みtxtリストに格納 f.close() #ストップワードの一覧ファイルを閉じる print(txt) #確認用 swords = [line.strip() for line in txt] #ストップワードの行末の改行文字「¥n」をstripメソッドで取る print(swords) #確認用 comments = data["自由回答"].tolist() #自由回答の列をtolist()でリスト型に変換 words = [] #単語リスト(最初は空) for c in comments: #コメントを1つずつcに取り出して繰り返し node = tagger.parseToNode(c) #形態素解析を実行(最初の形態素がnodeに入る) while node: #形態素がなくなるまで繰り返し hinshi = node.feature.split(",")[0] #品詞を抽出 #品詞が名詞かつスットプワードで無いならば if hinshi in ["名詞", "形容詞", "感動詞"] and not node.surface in swords: words.append(node.surface) #wordsリストに表層形を追加 node = node.next #次のnodeを新たなnodeとする print(len(words)) #単語の件数 print(words[:20]) #最初の20件の単語を表示 #################################################### #p29 #pandasのインポート import pandas as pd file = pd.ExcelFile("comment.xlsx") data = file.parse("回答") data.head() data = data[data["居住地"] == "静岡県"] data #################################################### #p30 #自由回答から「500」という文字列を含む行をretに抽出 ret = data[data["自由回答"].str.contains("500")] #抽出したretの各行をforで繰り返す(kに行インデックス、vに列がリストとして入る) for (k, v) in ret.iterrows(): #行インデックスと1列目(自由回答)を表示 print(k, v[1])