LoginSignup
4
1

More than 3 years have passed since last update.

WordcloudでMr.Talkboxの英語歌詞のメッセージを可視化する

Last updated at Posted at 2019-05-11

目的

過去に、Mr.Talkboxの歌詞に出てくる単語を出現数順にソートしました。

pythonのCounterを使用してMr.TalkBoxの英語歌詞の単語を出現数順にソートした記録

今回は、歌詞に出てくる単語を使ってword cloudで可視化します。
コードはこちらを参考にさせて頂きました。

準備

word cloud用ライブラリをインストール

git clone https://github.com/amueller/word_cloud
cd word_cloud
python setup.py install

その他ライブラリをインストール

pip install beautifulsoup4
pip install requests

コード

$ python sample.py
sample.py
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from bs4 import BeautifulSoup
import requests
import MeCab as mc

def create_wordcloud(text):

    fontpath = "/System/Library/Fonts/HelveticaNeue.ttc"
    stop_words = [ u'am', u'is', u'of', u'and', u'the', u'to', u'it', u'for', u'in', u'as', u'or', u'are', u'be', u'this', u'that', u'will', u'there', u'was']

    wordcloud = WordCloud(background_color="white",font_path=fontpath, width=900, height=500, \
                          stopwords=set(stop_words)).generate(text)

    plt.figure(figsize=(15,12))
    plt.imshow(wordcloud)
    plt.axis("off")
    plt.show()


wordlist = """
I feel something 
feel something 
I feel something 
feel something good inside
I feel something 
feeling something 
I feel something 
feel something 

There is something that I’m going try to explain
(省略)
"""

create_wordcloud(wordlist)

テスト

下記word cloudを作成できました。
Figure_1.png

feel something good inside shoo, のような感じが出ています。
coolです。

CodingError対策

ModuleNotFoundError: No module named 'wordcloud.query_integral_image'

他のword cloud用リソースが重なっている際に出るエラーのようです。
こちらを参照し、下記をword cloudをcloneしたパスで実行して解決。

$ python setup.py build_ext -i

--

OSError: cannot open resource

osコマンドで参照しているファイルが見つかりません。
今回の場合だとfont pathに存在するファイルを指定して解決。
 fontpath = "/System/Library/Fonts/HelveticaNeue.ttc"

参考

Word Cloudで文章の単語出現頻度を可視化する。[Python]
No module named 'wordcloud.query_integral_image'

4
1
5

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
4
1