0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

口コミを形態素解析・ワードクラウド

Posted at

はじめに

前回食べログでラーメン店(博多一双)の口コミをスクレイピングしたので、今回はそれを用いて、形態素解析・ワードクラウドを作成しました。

環境

・google colab
・python

コード

読み込み等

!apt-get -q -y install sudo file mecab libmecab-dev mecab-ipadic-utf8 git curl > /dev/null
!git clone --depth 1 https://github.com/neologd/mecab-ipadic-neologd.git > /dev/null
!echo yes | mecab-ipadic-neologd/bin/install-mecab-ipadic-neologd -n > /dev/null 2>&1

# シンボリックリンクによるエラー回避
!rm -f /usr/local/etc/mecabrc # Remove existing symbolic link
!ln -s /etc/mecabrc /usr/local/etc/mecabrc

!pip install mecab-python3
!pip install japanize-matplotlib
!pip install venn

from wordcloud import WordCloud
import MeCab
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

!apt-get -y install fonts-ipafont-gothic #日本語フォントのインストール


形態素解析

df = pd.read_csv('./一双口コミ.csv')
kutikomi = df['口コミ'].values

mecab = MeCab.Tagger('-Ochasen')
mecab.parse('')

output = []
for i in range(len(kutikomi)):
  node = mecab.parseToNode(kutikomi[i])
  while node:
    word_type = node.feature.split(',')[0]
    if word_type in ['名詞', '形容詞', '副詞', '動詞']:
      output.append(node.surface.upper())
    node = node.next
print(output)

スクリーンショット 2025-08-27 13.38.31.png
うまく分けられています!

ワードクラウド

text = ' '.join(output)

wordcloud_df = pd.DataFrame(output,columns=['word'])
wordcloud_df['count'] = 1
wordcloud_df = wordcloud_df.groupby('word').sum()
wordcloud_df = wordcloud_df.sort_values('count', ascending=False)
wordcloud_df = wordcloud_df.reset_index()

fpath = '/usr/share/fonts/truetype/fonts-japanese-gothic.ttf'
wordcloud = WordCloud(background_color="white",
                      font_path = fpath,
                      width = 800,
                      height = 600,
                      collocations = False).generate(" ".join(wordcloud_df['word']))
plt.figure(figsize=(12,10))
plt.imshow(wordcloud)
plt.axis("off") #メモリの非表示
plt.show()

スクリーンショット 2025-08-27 13.42.03.png

できました!!!
口コミから行列ができるほどの人気のラーメン店であることがわかります!
豚骨が豚と骨に分かれてるのはちょっと面白いかも。

このお店で食べたことありますが、ラーメンが泡々しているので、'泡'があるかなと思っていましたが、口コミにはあまりなかったようです。

最後に

今回は口コミデータをもとに形態素解析とワードクラウドを作成しました。
2年近くpythonを書いていなかったので、もっとpython力を高めていきたいです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?