( 環境 )
OS: MacOS
言語: Python3
【 結論 】
wordcloudには、分かち書きしたスペース区切りの単語の集まりを、Listではなく、string型オブジェクトで渡す必要がある。
-1. text = ["WordCloud","化","したい","文章"] は、実行エラー
-2. text = "WordCloud 化 したい 文章" は、成功
-3. text = "Wordcloud化したい文章" は、実行できるが、入力文が、そのまま画像化されるだけ
直面したこと
以下の解説サイトでは、WordCloudに渡す引数(以下、textと表記)に、和文を分かち書きにした単語の__リスト(配列List)__を渡している。
しかし、これはエラーになった。
textは、list ではなく、string で渡す必要があるらしい。
>>> text = ["WordCloud","化","したい","文章"]
>>> wordcloud = WordCloud(background_color="red", colormap="winter",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(text)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/wordcloud/wordcloud.py", line 632, in generate
return self.generate_from_text(text)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/wordcloud/wordcloud.py", line 613, in generate_from_text
words = self.process_text(text)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/wordcloud/wordcloud.py", line 575, in process_text
words = re.findall(regexp, text, flags)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/re.py", line 241, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or bytes-like object
>>>
__string型オブジェクト__を使うとうまくいく。
>>> text = "Word cloud 化 したい 文章"
>>> wordcloud = WordCloud(background_color="red", colormap="winter",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(text)
>>> plt.imshow(wordcloud)
<matplotlib.image.AxesImage object at 0x11022ec10>
>>> plt.show()
>>>
分かち書きにされていない、string型オブジェクトの場合
この場合は、エラーを出さずに実行は完了する。
入力したそのままの文が、そのまま画像化されるだけ。
>>> text = "Wordcloud化したい文章"
>>> wordcloud = WordCloud(background_color="red", colormap="winter",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(text)
>>> plt.imshow(wordcloud)
>>> plt.show()
( 長文で実験 )
以下のコードは、以下の記事を参考にしました。
>>> from bs4 import BeautifulSoup
>>> import requests
>>> from wordcloud import WordCloud
>>> url = 'https://qiita.com/kobaboy/items/46ff9f19b9ea5638fa8e'
>>> res = requests.get(url)
>>> soup = BeautifulSoup(res.text)
>>> text = soup.body.section.get_text().replace('\n', '').replace('\t', '')
>>> stop_words = ['てる', 'いる', 'なる', 'れる', 'する', 'ある', 'こと', 'これ', 'さん', 'して']
>>> print(text)
PyCon JP 2018 ひろがるPythonの資料まとめです1日目[基調講演] Argentina in Python: community, dreams, travels and learning動画[招待講演] 東大松尾研流 実践的AI人材育成法動画実践・競馬データサイエンススライド動画Why you should care about types: Python Typing in the Facebook Backendスライド動画Applying serverless architecture pattern to distributed data processing動画Webアプリケーションの仕組みスライド動画DjangoではじめるPyCharm実践入門スライド動画PyCon JP における子ども向けワークショップの活動事例と実施の意義動画Building Maintainable Python Web App using Flaskスライドオンザフライ高速化パッケージの比較:Numba, TensorFlow, Dask, etc動画Django REST Framework におけるAPI実装プラクティススライド動画あなたと私いますぐパッケージンスライド動画Pythonで時系列のデータを分析してみよう。スライドGitHub動画Fun with Python and Kanjiスライド動画The Modern OAuth 2.0スライド動画Jupyterで広がるPythonの可能性スライドGitHubInteractive Network Visualization using Python 〜 NetworkX + BokehでPEPの参照関係を可視化するスライドGitHub動画メルカリにおける AI 活用事例スライド動画Pythonistaの選球眼(せんきゅうがん) - エンジニアリングと野球の目利きになる技術スライド動画Introduce syntax and history of Python from 2.4 to 3.7スライド動画Rust と Pythonスライド動画Pythonで解く大学入試数学スライド動画自分が欲しいものをPythonで書く方法(Python for Myself)スライド動画2日目[基調講演] 「Pythonでやってみた」:広がるプログラミングの愉しみスライド動画Integrate Full-text Search service with Djangoスライド動画Migrating from Py2 application to Py3: first trial in MonotaRO / Python2 から Python3 への移植: MonotaRO での取り組み動画SymPyによる数式処理スライドGitHub動画niconicoにおけるコンテンツレコメンドの取り組みスライド動画HomeSecurity with Pythonスライド動画JVM上で動くPython3処理系cafebabepyの実装詳解スライド動画Sphinx-2.0 とドキュメントの未来スライド動画Djangoだってカンバンつくれるもん(Django Channels + Vue)スライド動画Djangoアプリケーションにおけるトイル撲滅戦記スライド動画REST API に疲れたあなたへ贈る GraphQL 入門スライド動画複数アプリケーションのプロセスとログを管理するための新しいツールと手法スライド動画Make a Drone using RaspberryPi and Google VoiceKit by Pythonスライド動画契約書データ関連のAI開発に伴う、前処理及び匿名化処理についての実例スライドArtisanal Async Adventures動画Pythonでざっくり学ぶUnixプロセススライド動画WILDCAT SDKは量子コンピュータビジネスの味方となるのか!?動画※数日前にWILDCAT->WILDQATに変更になったらしいNotebook as Web API: Turn your notebook into Web API動画From Data to Web Application: Anime Character Image Recognition with Transfer Learningスライド詳細動画1次元畳み込みフィルターを利用した音楽データのオートエンコーダスライド動画Pythonによる異常検知入門動画Python研修の作り方-Teaching Is Learning-スライド動画C拡張と共に乗り切るPython 2→3移行術スライドGitHub動画Django を Zappaで構築してServerless Python のベストプラクティスを探る動画料理写真が美味しく撮れる! 開発現場から覗くAI料理カメラの裏側スライド動画Pythonで始めるウェブスクレイピング実践入門スライド動画Pythonで「お絵描きパズル」を解いてみた。スライド動画Python, AWS and FinTech動画AltJSとしてのPython - フロントエンドをPythonで書こうスライド動画ライトニングトーク1日目動画2日目動画その他現状ある情報をまとめました。スライドも動画も見つからない発表については記載していません。もしここに載っていない情報がありましたら、コメントもしくは編集リクエスト等で教えて下さると嬉しいです!
>>>
>>> type(text)
<class 'str'>
>>>
>>> wordcloud = WordCloud(background_color="red", colormap="winter",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(text)
>>> plt.imshow(wordcloud)
>>> plt.show()
>>> wordcloud.to_file('wc1.png')
<wordcloud.wordcloud.WordCloud object at 0x1066bd5e0>
>>> quit()
( 考察 )
・ textは、ところどころスペースがあるので、分かち書き形式のデータとして扱われたと考えられる。
・ そのためエラーは起きなかった。
・ しかし、アルファベットの単語以外は、ほとんどの文が、単語単位で分かち書きされていない。
・ その結果、Python や using、andなどの英単語ばかりが、wordcloudに表示された。
・ 非英単語としては、「基調講演」がwordcloudに表示された。しかし、「基調」「講演」ではなく、「基調講演」が、一つの単語として、wordcloud図に、切り出されていた。
そこで、分かち書きにして、wordcloudの出力を見る。
以下を参考にしました。
>>> import MeCab as mc
>>> st_text = text.encode('utf-8')
>>> tagger = mc.Tagger('-Ochasen')
>>> words = tagger.parse(st_text)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: in method 'Tagger_parse', argument 2 of type 'char const *'
Additional information:
Wrong number or type of arguments for overloaded function 'Tagger_parse'.
Possible C/C++ prototypes are:
MeCab::Tagger::parse(MeCab::Lattice *) const
MeCab::Tagger::parse(char const *)
以下を行わずに行うとうまくいく。
>>> st_text = text.encode('utf-8')
__成功 __
>>> words = tagger.parse(text)
>>> print(words)
'PyCon JP 2018 \u3000 ひろがる Python の 資料 まとめ です 1 日 目 [ 基調 講演 ] Argentina in Python : community , dreams , travels and learning 動画 [ 招待 講演 ] 東大 松尾 研 流 実践 的 AI 人材 育成 法 動画 実践 ・ 競馬 データサイエンススライド 動画 Why you should care about types : Python Typing in the Facebook Backend スライド 動画 Applying serverless architecture pattern to distributed data processing 動画 Web アプリケーション の 仕組み スライド 動画 Django で はじめる PyCharm 実践 入門 スライド 動画 PyCon JP における 子ども 向け ワークショップ の 活動 事例 と 実施 の 意義 動画 Building Maintainable Python Web App using Flask スライド オンザフライ 高速 化 パッケージ の 比較 : Numba , TensorFlow , Dask , etc 動画 Django REST Framework における API 実装 プラクティススライド 動画 あなた と 私 いま すぐ パッケージンスライド 動画 Python で 時 系列 の データ を 分析 し て みよ う 。 スライド GitHub 動画 Fun with Python and Kanji スライド 動画 The Modern OAuth 2 . 0 スライド 動画 Jupyter で 広がる Python の 可能 性 スライド GitHubInteractive Network Visualization using Python 〜 NetworkX + Bokeh で PEP の 参照 関係 を 可視 化 する スライド GitHub 動画 メルカリ における AI 活用 事例 スライド 動画 Pythonista の 選 球 眼 ( せん きゅう がん ) - エンジニアリング と 野球 の 目利き に なる 技術 スライド 動画 Introduce syntax and history of Python from 2 . 4 to 3 . 7 スライド 動画 Rust と Python スライド 動画 Python で 解く 大学 入試 数学 スライド 動画 自分 が 欲しい もの を Python で 書く 方法 ( Python for Myself ) スライド 動画 2 日 目 [ 基調 講演 ] 「 Python で やっ て み た 」 : 広がる プログラミング の 愉し み スライド 動画 Integrate Full - text Search service with Django スライド 動画 Migrating from Py 2 application to Py 3 : first trial in MonotaRO / Python 2 から Python 3 へ の 移植 : MonotaRO で の 取り組み 動画 SymPy による 数式 処理 スライド GitHub 動画 niconico における コンテンツレコメンド の 取り組み スライド 動画 HomeSecurity with Python スライド 動画 JVM 上 で 動く Python 3 処理 系 cafebabepy の 実装 詳解 スライド 動画 Sphinx - 2 . 0 と ドキュメント の 未来 スライド 動画 Django だって カンバ ン つくれる もん ( Django Channels + Vue ) スライド 動画 Django アプリケーション における トイル 撲滅 戦記 スライド 動画 REST API に 疲れ た あなた へ 贈る GraphQL 入門 スライド 動画 複数 アプリケーション の プロセス と ログ を 管理 する ため の 新しい ツール と 手法 スライド 動画 Make a Drone using RaspberryPi and Google VoiceKit by Python スライド 動画 契約 書 データ 関連 の AI 開発 に 伴う 、 前 処理 及び 匿名 化 処理 について の 実例 スライド Artisanal Async Adventures 動画 Python で ざっくり 学ぶ Unix プロセス スライド 動画 WILDCAT SDK は 量子 コンピュータ ビジネス の 味方 と なる の か ! ? 動画 ※ 数 日 前 に WILDCAT -> WILDQAT に 変更 に なっ た らしい Notebook as Web API : Turn your notebook into Web API 動画 From Data to Web Application : Anime Character Image Recognition with Transfer Learning スライド 詳細 動画 1 次元 畳み込み フィルター を 利用 し た 音楽 データ の オートエンコーダスライド 動画 Python による 異常 検知 入門 動画 Python 研修 の 作り方 - Teaching Is Learning - スライド 動画 C 拡張 と共に 乗り切る Python 2 → 3 移行 術 スライド GitHub 動画 Django を Zappa で 構築 し て Serverless Python の ベストプラクティス を 探る 動画 料理 写真 が 美味しく 撮れる ! 開発 現場 から 覗く AI 料理 カメラ の 裏側 スライド 動画 Python で 始める ウェブスクレイピング 実践 入門 スライド 動画 Python で 「 お 絵描き パズル 」 を 解い て み た 。 スライド 動画 Python , AWS and FinTech 動画 AltJS として の Python - フロント エンド を Python で 書こ う スライド 動画 ライトニングトーク 1 日 目 動画 2 日 目 動画 その他 現状 ある 情報 を まとめ まし た 。 スライド も 動画 も 見つから ない 発表 について は 記載 し て い ませ ん 。 もし ここ に 載っ て い ない 情報 が あり まし たら 、 コメント もしくは 編集 リクエスト 等 で 教え て 下さる と 嬉しい です ! \n'
>>>
>>> print(type(words))
<class 'str'>
>>>
>>> print(type(words))
<class 'str'>
>>> wordcloud = WordCloud(background_color="red", colormap="winter",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(words)
>>> plt.imshow(wordcloud)
<matplotlib.image.AxesImage object at 0x10db5e3a0>
>>> plt.show()
以下でもOK
・ [Python]MeCabで誰でも簡単に分かち書きをする方法
>>>tagger = mc.Tagger ("-Owakati")
>>>words = tagger.parse (text)
>>>print(words)
PyCon JP 2018 ひろがる Python の 資料 まとめ です 1 日 目 [ 基調 講演 ] Argentina in Python : community , dreams , travels and learning 動画 [ 招待 講演 ] 東大 松尾 研 流 実践 的 AI 人材 育成 法 動画 実践 ・ 競馬 データサイエンススライド 動画 Why you should care about types : Python Typing in the Facebook Backend スライド 動画 Applying serverless architecture pattern to distributed data processing 動画 Web アプリケーション の 仕組み スライド 動画 Django で はじめる PyCharm 実践 入門 スライド 動画 PyCon JP における 子ども 向け ワークショップ の 活動 事例 と 実施 の 意義 動画 Building Maintainable Python Web App using Flask スライド オンザフライ 高速 化 パッケージ の 比較 : Numba , TensorFlow , Dask , etc 動画 Django REST Framework における API 実装 プラクティススライド 動画 あなた と 私 いま すぐ パッケージンスライド 動画 Python で 時 系列 の データ を 分析 し て みよ う 。 スライド GitHub 動画 Fun with Python and Kanji スライド 動画 The Modern OAuth 2 . 0 スライド 動画 Jupyter で 広がる Python の 可能 性 スライド GitHubInteractive Network Visualization using Python 〜 NetworkX + Bokeh で PEP の 参照 関係 を 可視 化 する スライド GitHub 動画 メルカリ における AI 活用 事例 スライド 動画 Pythonista の 選 球 眼 ( せん きゅう がん ) - エンジニアリング と 野球 の 目利き に なる 技術 スライド 動画 Introduce syntax and history of Python from 2 . 4 to 3 . 7 スライド 動画 Rust と Python スライド 動画 Python で 解く 大学 入試 数学 スライド 動画 自分 が 欲しい もの を Python で 書く 方法 ( Python for Myself ) スライド 動画 2 日 目 [ 基調 講演 ] 「 Python で やっ て み た 」 : 広がる プログラミング の 愉し み スライド 動画 Integrate Full - text Search service with Django スライド 動画 Migrating from Py 2 application to Py 3 : first trial in MonotaRO / Python 2 から Python 3 へ の 移植 : MonotaRO で の 取り組み 動画 SymPy による 数式 処理 スライド GitHub 動画 niconico における コンテンツレコメンド の 取り組み スライド 動画 HomeSecurity with Python スライド 動画 JVM 上 で 動く Python 3 処理 系 cafebabepy の 実装 詳解 スライド 動画 Sphinx - 2 . 0 と ドキュメント の 未来 スライド 動画 Django だって カンバ ン つくれる もん ( Django Channels + Vue ) スライド 動画 Django アプリケーション における トイル 撲滅 戦記 スライド 動画 REST API に 疲れ た あなた へ 贈る GraphQL 入門 スライド 動画 複数 アプリケーション の プロセス と ログ を 管理 する ため の 新しい ツール と 手法 スライド 動画 Make a Drone using RaspberryPi and Google VoiceKit by Python スライド 動画 契約 書 データ 関連 の AI 開発 に 伴う 、 前 処理 及び 匿名 化 処理 について の 実例 スライド Artisanal Async Adventures 動画 Python で ざっくり 学ぶ Unix プロセス スライド 動画 WILDCAT SDK は 量子 コンピュータ ビジネス の 味方 と なる の か ! ? 動画 ※ 数 日 前 に WILDCAT -> WILDQAT に 変更 に なっ た らしい Notebook as Web API : Turn your notebook into Web API 動画 From Data to Web Application : Anime Character Image Recognition with Transfer Learning スライド 詳細 動画 1 次元 畳み込み フィルター を 利用 し た 音楽 データ の オートエンコーダスライド 動画 Python による 異常 検知 入門 動画 Python 研修 の 作り方 - Teaching Is Learning - スライド 動画 C 拡張 と共に 乗り切る Python 2 → 3 移行 術 スライド GitHub 動画 Django を Zappa で 構築 し て Serverless Python の ベストプラクティス を 探る 動画 料理 写真 が 美味しく 撮れる ! 開発 現場 から 覗く AI 料理 カメラ の 裏側 スライド 動画 Python で 始める ウェブスクレイピング 実践 入門 スライド 動画 Python で 「 お 絵描き パズル 」 を 解い て み た 。 スライド 動画 Python , AWS and FinTech 動画 AltJS として の Python - フロント エンド を Python で 書こ う スライド 動画 ライトニングトーク 1 日 目 動画 2 日 目 動画 その他 現状 ある 情報 を まとめ まし た 。 スライド も 動画 も 見つから ない 発表 について は 記載 し て い ませ ん 。 もし ここ に 載っ て い ない 情報 が あり まし たら 、 コメント もしくは 編集 リクエスト 等 で 教え て 下さる と 嬉しい です !
>>>
>>> print(type(words))
<class 'str'>
>>> wordcloud = WordCloud(background_color="red", colormap="winter",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(words)
>>> plt.imshow(wordcloud)
<matplotlib.image.AxesImage object at 0x10db5e3a0>
>>> plt.show()
水色で描画してみる
>>> wordcloud = WordCloud(background_color="skyblue", colormap="winter",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(words)
>>> plt.imshow(wordcloud)
<matplotlib.image.AxesImage object at 0x10d5e36a0>
>>> plt.show()
background_colorをskyblueにしたまま、colormapをspringに変えた結果
>>> wordcloud = WordCloud(background_color="skyblue", colormap="spring",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(words)
>>> plt.imshow(wordcloud) <matplotlib.image.AxesImage object at 0x11dc84340>
>>> plt.show() >>>
background_colorをskyblueにしたまま、colormapをsummerに変えた結果
>>> wordcloud = WordCloud(background_color="skyblue", colormap="summer",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(words)
>>> plt.imshow(wordcloud)
<matplotlib.image.AxesImage object at 0x11f217f10>
>>> plt.show() >>>
background_colorをskyblueにしたまま、colormapをautumnに変えた結果
>>> wordcloud = WordCloud(background_color="skyblue", colormap="autumn",width=900, height=500, font_path=fpath, stopwords=set(stop_words)).generate(words)
>>> plt.imshow(wordcloud)
<matplotlib.image.AxesImage object at 0x11e93db50>
>>> plt.show() >>>
colormapを*fall**にすると、エラーになる
( 参考 )
・ WordCloudの使い方
・ wordcloud macでフォント指定 pythonで動かす
・ PythonでWordCloudを作成してみました