PythonのWord Cloudで表示しない単語を増やす

Pythonのwordcloudモジュールでワードクラウドを作るときに、表示する単語の制限を増やしてみました。

目次

  1. ワードクラウドに含めない単語を指定する
  2. ワードクラウドから一部の単語を排除してみた

ワードクラウドに含めない単語を指定する

ワードクラウドは文章中の単語を使用頻度に応じてフォントの大きさや色を変えて表示するものです。ただ実際のところ、文書に頻出する単語としては代名詞や接続詞が固有名詞などよりも多くなります。

そうすると、表示する単語を規制したくなりますね。

wordcloudモジュールでは、wordcloudオブジェクトを作るときのコンストラクター引数に使用しない単語を指定することができます。

W = wordcloud.WordCloud([stopwords]).generate(text)

変数

内容

text

str

ワードクラウドの元になるテキスト。

stopwords

set

省略可。既定値はNone。

W

wordcloud

wordcloudオブジェクト。

stopwords引数がNoneのとき、実際には何も省かないわけではなくて、最初からthoseやhisなどの単語を省くようになっています。最初から省くようになっている単語はSTOPWORDSというsetに定義されています。

ではSTOPWORDSセットの中身を見てみましょう。

>>> from wordcloud import STOPWORDS
>>> print(type(STOPWORDS))
<class 'set'>
>>> print(STOPWORDS)
{'those', 'doing', 'his', 'through', 'on', 'down', "we've", 'other', 'because', 'between', 'com', "how's", "that's", 'what', "there's", 'r', 'theirs', 'each', "what's", 'before', 'like', 'http', 'very', 'else', 'yourself', 'being', 'below', "i'm", 'me', 'your', 'otherwise', 'from', "he's", 'into', 'has', "she'll", 'why', 'both', 'they', 'not', 'during', "i'd", "i'll", 'should', 'just', "we'd", 'up', "isn't", "shan't", 'until', 'her', 'himself', 'the', 'their', 'and', 'have', 'yours', 'only', "they'll", 'ought', 'we', "why's", 'or', 'be', 'ourselves', "she'd", "shouldn't", 'out', 'who', 'more', 'were', 'get', 'www', "you'll", 'all', 'here', 'for', 'k', 'than', "where's", 'against', "can't", 'would', 'whom', 'does', 'myself', 'no', 'any', "we're", 'ever', 'then', 'itself', "aren't", "it's", "didn't", 'these', "when's", 'which', 'yourselves', 'how', 'since', "let's", 'so', "you've", "won't", 'such', 'at', 'shall', 'to', 'but', 'ours', 'where', "you'd", 'in', 'as', 'of', 'few', "they've", "hasn't", 'my', "haven't", 'with', 'about', 'our', "weren't", 'over', 'he', 'further', 'are', "he'll", "here's", "who's", 'cannot', 'above', 'was', 'had', 'also', "you're", 'nor', 'them', 'it', 'off', 'been', 'this', 'under', 'by', "doesn't", 'do', 'if', 'some', "wasn't", 'after', 'am', 'could', 'themselves', "wouldn't", 'him', 'again', 'too', 'you', 'hers', 'that', 'can', "don't", 'most', 'did', "they'd", 'she', 'i', 'own', 'an', 'its', "couldn't", "i've", "they're", 'herself', "he'd", "we'll", 'once', "hadn't", 'same', 'is', "she's", 'however', "mustn't", 'there', 'a', 'when', 'having', 'while'}
>>>

まあ、英語ですね。

ワードクラウドから一部の単語を排除してみた

wordcloudのGitHubのreadme.mdをワードクラウドにしてみます。

from wordcloud import WordCloud
from matplotlib import pyplot as plt

with open('readme.md', mode='rt', encoding='utf-8') as fo:
    cloud_text = fo.read()

word_cloud = WordCloud(background_color='white', colormap='bone').generate(cloud_text)

plt.imshow(word_cloud)
plt.axis('off')
plt.show()
既定値で描画

はい、httpsという単語が大きく表示されます。これはハイパーリンク用のurlの文字列がカウントされてしまっているわけですね。

というわけで、httpsを除外します。

from wordcloud import WordCloud
from matplotlib import pyplot as plt
from wordcloud import STOPWORDS

with open('readme.md', mode='rt', encoding='utf-8') as fo:
    cloud_text = fo.read()

STOPWORDS.add('https')
word_cloud = WordCloud(background_color='white', colormap='bone').generate(cloud_text)

plt.imshow(word_cloud)
plt.axis('off')
plt.show()

STOPWORDSというセットにaddメソッドでhttpsという単語を足してから、WordCloudコンストラクターでオブジェクトを作りました。そうすると、コンストラクターに何も指定しなくても使用されない単語が増える訳ですね。

基本的に定数なものにプログラム内で変更を加えるのは作法としてどうなんでしょうか。

このプログラムを実行するとこうなります。

変更値で描画

httpsが無くなりました。

公開日

広告