-2

我是一个刚开始学习python的韩国人。我正在用韩文做wordcloud。我想我做了我所教的一切。但是我得到了这个我无法解决的错误。请帮我。

from wordcloud import WordCloud
from collections import Counter
from konlpy.tag import Okt

这是输入

text=''
with open("./res/대한민국헌법.txt", encoding="utf-8") as f:

text = f.read()

nlp =Okt()
nouns =nlp.nouns(text)
# print(nouns)
# print("-" * 30)

words=[]
for n in nouns:
    if len(n) >1:
        words.append(n)
# print(words)
print("-" * 30)

#Counter 객체를 통해 리스트 원소들의 빈도수 계산
count =Counter(words)

most=count.most_common(100)
# print(most)
print("-" * 30)

tags={}
for t in most:
    n, c= t
    tags[n]=c

print(tags)

wc= WordCloud(font_path="NanumGothic", width=1200, height=800,
                scale=2.0, max_font_size=250)

wc.generate_from_frequencies(tags) # <- this is where I got the error from
wc.to_file("대한민국헌법-주요단어.png")

这是输出

Trackback(最近的最新通话):文件“ D:\ Hyeokjun \ 01-파이썬기초,데이터화\ 09-데이터데이터-데이터-가로++++++++++워드\워드\파이썬\파이썬\파이썬\파이썬\파이썬\파이썬\파이썬\파이썬\파이썬\ 09강\ section09 \ section09 \ 01 \ 01 \ 01 .nu​​meric.py”,第 40 行,在 wc.generate_from_frequencies(tags) 文件“C:\Users\newjh\AppData\Local\Programs\Python\Python37\lib\site-packages\wordcloud\wordcloud.py”,第 496 行,在 generate_from_frequencies font = ImageFont.truetype(self.font_path, font_size) 文件“C:\Users\newjh\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFont.py”中,第 648 行,在 truetype 中返回 freetype(字体)文件“C:\Users\newjh\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFont.py”,第 645 行,在 freetype 中返回 FreeTypeFont(字体,大小,索引,编码,layout_engine)文件“C:\Users\newjh\AppData\Local\Programs\Python\Python37\lib\site-packages\PIL\ImageFont.py",第 194 行,在init font, size, index, encoding, layout_engine=layout_engine OSError: cannot open resource [Finished in 5.9s]

4

1 回答 1

-1

似乎无法检索字体。尝试将字体“NanumGothic”复制到您的项目文件夹中,或更新font_path="NanumGothic"以下行中的字体以指向正确的字体文件:

wc= WordCloud(font_path="/path/to/fontfile", width=1200, height=800,
                scale=2.0, max_font_size=250)
于 2020-05-24T12:57:03.177 回答