I'm doing text mining in Wordcloud, but when I run the program, I get OSError: cannot open resource
.code:
word = " 選挙 期間 中 自民党 候補 者 たち 全国 各地 我々 経済 政策 安全 保障 日本 将来 決意 地域 想い 皆様 国 自民党 皆様 歩み 1 2 日間 声援 外国 人 旅行 者 数 5 年間 3 倍 自民党 次元 観光 資源 施策 取り組み 我が国 伝統 文化財 等 国内 観光 資源 強化 観光 産業 活性 化 実現 明日 10月 2 1 日 ( 土 ) 安倍 晋 三 総裁 岐阜 県 愛知 県 東京 都 詳細 是非 自民党 特設 サイト 確認 我々 スタート 給付 型 奨学 金 授業 料 免除 制度 拡充 支援 必要 子供 たち 高等 教育 無償 化 自民党 誰 事 安心 社会 明日 10月 2 0 日 ( 金 ) 安倍 晋 三 総裁 神奈川 県 東京 都 詳細 是非 自民党 特設 サイト 確認 明日 10月 1 9 日 ( 木 ) 安倍 晋 三 総裁 奈良 県 京都 府 滋賀 県 詳細 是非 自民党 特設 サイト 確認 3 年間 4 0 代 以下 農業 就農 者 3 年 連続 2 万 人 自民党 若者 意欲 農林 漁業 者 全力 応援 挑戦 夢 希望 農政 時代 私 たち 自民党 日本 経済 中小 企業 応援 思い 中小 小規模 事業 者 固定 資産 税 3 年間 半減 制度 始め 3 万 件 利用 制度 実現 中小 企業 倒産 3 割 減少 生産 性 ため 支援 大胆 明日 10月 1 8 日 ( 水 ) 安倍 晋 三 総裁 埼玉 県 東京 都 詳細 是非 自民党 特設 サイト 確認 選挙 北朝鮮 脅威 国民 生命 幸せ 暮らし の 選挙 自民党 今 外交 力 国際 社会 連携 姿勢 北朝鮮 問題 明日 10月 1 6 日 ( 月 ) 安倍 晋 三 総裁 大阪 府 詳細 是非 自民党 特設 サイト 確認 アベノミクス 3 本 矢 はじめ 我々 政策 政権 交替 後 gdp 5 0 兆 円 都道府県 有効 求人 倍率 1 倍 景気 回復 着実 日本 経済 成長 皆様 一人ひとり 成長 実感 よう 努力 所存 明日 10月 1 5 日 ( 日 ) 安倍 晋 三 総裁 北海道 詳細 是非 自民党 特設 サイト 確認 # 自民党 # 安倍 晋 三 # 街頭 演説 熊本 地震 一 年 半 被害 不自由 生活 多く 被災 者 皆様 安心 元 生活 よう 全力 復興 # 国 # 衆院 "
import matplotlib.pyplot asplt
from wordcloud import WordCloud
wordcloud=WordCloud(background_color="white", font_path='ipag.ttc', width=600, height=400, min_font_size=10)
wordcloud.generate(word)
plt.imshow(wordcloud)
plt.show()
error messages:
OSError Traceback (most recent call last)
<ipython-input-20-ae615ff8e919>in<module>
4#fpath="/Library/Fonts//Hiragino Marugo ProN W4.ttc"
5 wordcloud=WordCloud(background_color="white", font_path='ipag.ttc', width=600, height=400, min_font_size=10)
---->6wordcloud.generate(word)
7plt.imshow(wordcloud)
8plt.show()
...... Abbreviated......
~\anaconda3\lib\site-packages\PIL\ImageFont.py in __init__(self, font, size, index, encoding, layout_engine)
186 return
187 self.font = core.getfont(
-->188 font, size, index, encoding, layout_engine = layout_engine
189 )
190 else:
OSError:cannot open resource
Find the Japanese font using the command below and try again.
$find/Library/Fonts/
/Library/Fonts//Hiragino Marugo ProN W4.ttc
fpath="/Library/Fonts//Hiragino Marugo ProN W4.ttc"
wordcloud=WordCloud(background_color="white", font_path='ipag.ttc', width=600, height=400, min_font_size=10)
wordcloud.generate(word)
plt.imshow(wordcloud)
plt.show()
OSError: cannot open resource error.
What is the cause of the error?
I restarted kernel and Anaconda Navigator
I received your reply and tried the code below.
import matplotlib.pyplot as plt
from wordcloud import WordCloud
wordcloud=WordCloud(background_color="white", font_path='/Library/Fonts//Hiragino Marugo ProN W4.ttc', width=600, height=400, min_font_size=10)
wordcloud.generate(word)
plt.imshow(wordcloud)
plt.show()
The following error will appear this time.
FileNotFoundError: [Errno2] No such file or directory: '/Library/Fonts//Hiragino Marugo ProN W4.ttc'
Do you mean I can't find the font even though I have it?
python
Why don't you check if the file is really there?
For example,
from pathlib import Path
p=Path('/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc')
p.exists()#True
Look for it
>>>from pathlib import Path
>> base=Path('/usr/share/fonts')
>>for pin base.glob('**/Noto*.ttc'):
... print(p)
...
/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc
/usr/share/fonts/opentype/noto/NotoSansCJK-Light.ttc
/usr/share/fonts/opentype/noto/NotServiceifCJK-Black.ttc
findSystemFonts()
to get the path, list and show examples (check colab)
#!apt install fonts-noto-cjk
word = " 選挙 期間 中 自民党 候補 者 たち 全国 各地 我々 経済 政策 安全 保障 日本 将来 決意 地域 想い 皆様 国 自民党 皆様 歩み 1 2 日間 声援 外国 人 旅行 者 数 5 年間 3 倍 自民党 次元 観光 資源 施策 取り組み 我が国 伝統 文化財 等 国内 観光 資源 強化 観光 産業 活性 化 実現 明日 10月 2 1 日 ( 土 ) 安倍 晋 三 総裁 岐阜 県 愛知 県 東京 都 詳細 是非 自民党 特設 サイト 確認 我々 スタート 給付 型 奨学 金 授業 料 免除 制度 拡充 支援 必要 子供 たち 高等 教育 無償 化 自民党 誰 事 安心 社会 明日 10月 2 0 日 ( 金 ) 安倍 晋 三 総裁 神奈川 県 東京 都 詳細 是非 自民党 特設 サイト 確認 明日 10月 1 9 日 ( 木 ) 安倍 晋 三 総裁 奈良 県 京都 府 滋賀 県 詳細 是非 自民党 特設 サイト 確認 3 年間 4 0 代 以下 農業 就農 者 3 年 連続 2 万 人 自民党 若者 意欲 農林 漁業 者 全力 応援 挑戦 夢 希望 農政 時代 私 たち 自民党 日本 経済 中小 企業 応援 思い 中小 小規模 事業 者 固定 資産 税 3 年間 半減 制度 始め 3 万 件 利用 制度 実現 中小 企業 倒産 3 割 減少 生産 性 ため 支援 大胆 明日 10月 1 8 日 ( 水 ) 安倍 晋 三 総裁 埼玉 県 東京 都 詳細 是非 自民党 特設 サイト 確認 選挙 北朝鮮 脅威 国民 生命 幸せ 暮らし の 選挙 自民党 今 外交 力 国際 社会 連携 姿勢 北朝鮮 問題 明日 10月 1 6 日 ( 月 ) 安倍 晋 三 総裁 大阪 府 詳細 是非 自民党 特設 サイト 確認 アベノミクス 3 本 矢 はじめ 我々 政策 政権 交替 後 gdp 5 0 兆 円 都道府県 有効 求人 倍率 1 倍 景気 回復 着実 日本 経済 成長 皆様 一人ひとり 成長 実感 よう 努力 所存 明日 10月 1 5 日 ( 日 ) 安倍 晋 三 総裁 北海道 詳細 是非 自民党 特設 サイト 確認 # 自民党 # 安倍 晋 三 # 街頭 演説 熊本 地震 一 年 半 被害 不自由 生活 多く 被災 者 皆様 安心 元 生活 よう 全力 復興 # 国 # 衆院 "
import matplotlib.font_manager as fm
import matplotlib.pyplot asplt
from wordcloud import WordCloud
import pandas aspd
df = pd.DataFrame([(fm.FontProperties(fname=f).get_name(),f) for fin fm.findSystemFonts(),
columns=['name', 'path'])
display(df)
get 'Noto Sans CJK JP' path with fontpath=df.iloc[8]['path']# line specification
wordcloud=WordCloud(background_color="white", font_path=fontpath, width=600, height=400, min_font_size=10)
wordcloud.generate(word)
wordcloud.to_image()
The original code simply specifies the font_path='ipag.ttc'
filename, so you will go to the same folder (directory) as the Python code you ran.
The modified code is also not used only by defining fpath
.
For example, try specifying the font file with the absolute path as follows:
wordcloud=WordCloud(background_color="white", font_path='/Library/Fonts//Hiragino Marugo ProN W4.ttc', width=600, height=400,min_font_size=10)
727 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
547 Understanding How to Configure Google API Key
543 Unable to install versioned in Google Colab
550 PHP ssh2_scp_send fails to send files as intended
540 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2024 OneMinuteCode. All rights reserved.