LoginSignup
2
2

More than 3 years have passed since last update.

日本語のワードクラウドを使って新型iPhoneのニュース記事を可視化する

Last updated at Posted at 2019-09-14

目的

日本語のワードクラウドを使って新型iPhoneのニュース記事を可視化した際の備忘録です

準備

wordcloudをインストールします。

$ pip install wordcloud

フォントを下記サイトからダウンロードします
https://www.google.com/get/noto/help/install/

macOS
Download the font package (.zip)
Double click on the package to uncompress it
Open Font Book (Go to Finder → Applications → Font Book)
Select all of the font files and drag them to the Font column (i.e., the second column) of Font Book

コード

ネットから消費税が10%増税する関連のニュースをコピーします。

sample.py
# $ pip install wordcloud
# https://www.google.com/get/noto/help/install/

import matplotlib.pyplot as plt
from wordcloud import WordCloud
from MeCab import Tagger

t = Tagger()
text = """
消費税率の10%...(以下省略)
"""

splitted = " ".join([x.split("\t")[0] for x in t.parse(text).splitlines()[:-1]])

stop_words = ['する', 'せる', 'られる', 'あの', 'する', 'ある', 'とこ', 'なる', 'ない', 'ああ', 'れる', 'さん', 'やる', 'この', 'どう', 'そう', 'は', 'の', 'を', 'に', 'が', 'で','と','た','へ','し','も','や', 'て', 'か','いる', 'な' , 'ば', 'い', 'なかっ', 'いけ', 'でき', 'あっ', 'こと', 'ため', 'ます', 'これ', 'それ', 'その', 'さ', 'という', 'だけ', 'あろう', 'また', 'もの', 'よう', 'から', 'る', 'あろう', 'だ', 'れ', 'だから', "そして", 'あろう', 'あれ', 'あろ', 'う', 'oo', 'いい', 'ご', 'たい', 'なく', 'ぞ', 'とき', 'そこ', 'しかし', 'として', 'ooo', 'ぬ', 'ず', 'なっ', 'ZO', 'Zoo', 'O', 'なら', 'あ', 'こ' ]

wc = WordCloud(font_path="~/Desktop/python/wordcloud-jp/.fonts/NotoSansCJKjp-Regular.otf", regexp="[\w']+", stopwords=set(stop_words))
wc.generate(splitted)

plt.imshow(wc)
plt.show()

増税するらしき事が可視化によって明確になりました。
加えて、税率、消費、対応などが記事の焦点となっている事がわかります。

Figure_1.png

新型iPhoneの記事を入れると以下のような感じです。
Figure_1.png

プロセッサー、容量、ベンチマークなど、新型は性能が注目されている事がわかります。

追記

ファイル(text.txt)から読み込む場合は下記

# $ pip install wordcloud
# https://www.google.com/get/noto/help/install/

import matplotlib.pyplot as plt
from wordcloud import WordCloud
from MeCab import Tagger

t = Tagger()

f = open('./text.txt')
text = f.read()  # ファイル終端まで全て読んだデータを返す
f.close()
#text = ""

splitted = " ".join([x.split("\t")[0] for x in t.parse(text).splitlines()[:-1]])

stop_words = ['する', 'せる', 'られる', 'あの', 'する', 'ある', 'とこ', 'なる', 'ない', 'ああ', 'れる', 'さん', 'やる', 'この', 'どう', 'そう', 'は', 'の', 'を', 'に', 'が', 'で','と','た','へ','し','も','や', 'て', 'か','いる', 'な' , 'ば', 'い', 'なかっ', 'いけ', 'でき', 'あっ', 'こと', 'ため', 'ます' ]
wc = WordCloud(font_path="/Users/seigo/Desktop/python/wordcloud-jp/.fonts/NotoSansCJKjp-Regular.otf", regexp="[\w']+", stopwords=set(stop_words))
wc.generate(splitted)

plt.imshow(wc)
plt.show()

参考

【備忘録】日本語のワードクラウドを作る
Google Noto Fonts
PythonのWordCloudで遊んでみた【題材 : 吾輩は猫である】

2
2
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
2
2