在Google Colab中使用Python3创建word cloud时,我应该如何使用Arial字体?
我有一个twitter文本数据集,它是英语、阿拉伯语和波斯语的混合体。我想用它创造一个词云。不幸的是,我的单词云在照片中显示了阿拉伯语和波斯语单词的空白方块。我碰巧听到了解决这个问题的三种方法:在Google Colab中使用Python3创建word cloud时,我应该如何使用Arial字体?,python,fonts,word-cloud,Python,Fonts,Word Cloud,我有一个twitter文本数据集,它是英语、阿拉伯语和波斯语的混合体。我想用它创造一个词云。不幸的是,我的单词云在照片中显示了阿拉伯语和波斯语单词的空白方块。我碰巧听到了解决这个问题的三种方法: 使用不同的编码:我尝试了“UTF-8”、“UTF-16”、“UTF-32”和“ISO-8859-1”,但没有解决问题 使用阿拉伯语_整形器:不起作用 使用同时支持三种语言(如“Arial”字体)的字体:在word cloud中尝试将字体更改为Arial时,我收到以下错误: 输入 cannot open
cannot open resource
wordcloud=wordcloud(font\u path='arial',stopwords=stopwords,background\u color=“white”,max\u font\u size=50,max\u words=100)。生成(重塑文本)
plt.imshow(wordcloud,插值为双线性)
plt.轴(“关闭”)
plt.show()
输出
cannot open resource
此代码在Anaconda中运行良好,但在Google Colab中不起作用。唯一需要解决的问题是我应该为Google Colab中的字体路径输入什么路径使用波斯语,您有三个问题需要解决:
阿拉伯文\u整形器
的整形
功能。记住这并不能完全解决你的问题,你需要第三步python bidi
库解决这个问题import matplotlib.pyplot as plt
import arabic_reshaper
from bidi.algorithm import get_display
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
txt = '''I would love to try or hear the sample audio your app can produce. I do not want to purchase, because I've purchased so many apps that say they do something and do not deliver.
Can you please add audio samples with text you've converted? I'd love to see the end results.
Thanks!
سلام حال سلام سلام سلام حال شما چطوره است نیست
'''
word_cloud = WordCloud(font_path='arial', stopwords=STOPWORDS, background_color="white", max_font_size=50, max_words=100)
word_cloud = word_cloud.generate_from_text(get_display(arabic_reshaper.reshape(txt)))
plt.imshow(word_cloud, interpolation='bilinear')
plt.axis("off")
plt.show()
使用波斯语,您需要解决三个问题:
阿拉伯文\u整形器
的整形
功能。记住这并不能完全解决你的问题,你需要第三步python bidi
库解决这个问题import matplotlib.pyplot as plt
import arabic_reshaper
from bidi.algorithm import get_display
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
txt = '''I would love to try or hear the sample audio your app can produce. I do not want to purchase, because I've purchased so many apps that say they do something and do not deliver.
Can you please add audio samples with text you've converted? I'd love to see the end results.
Thanks!
سلام حال سلام سلام سلام حال شما چطوره است نیست
'''
word_cloud = WordCloud(font_path='arial', stopwords=STOPWORDS, background_color="white", max_font_size=50, max_words=100)
word_cloud = word_cloud.generate_from_text(get_display(arabic_reshaper.reshape(txt)))
plt.imshow(word_cloud, interpolation='bilinear')
plt.axis("off")
plt.show()
我将字体上传到我的谷歌硬盘,并使用了以下代码:
wordcloud=wordcloud(font\u path='/content/drive/My drive/ARIAL.TTF',stopwords=stopwords,background\u color=“white”,max\u font\u size=50,max\u words=100)。生成(get\u display(阿拉伯语重塑器。重塑(所有tweets))
我将字体上传到我的谷歌硬盘,并使用了以下代码:
wordcloud=wordcloud(font\u path='/content/drive/My drive/ARIAL.TTF',stopwords=stopwords,background\u color=“white”,max\u font\u size=50,max\u words=100)。生成(get\u display(阿拉伯语重塑器。重塑(所有tweets))
您可能需要测试这些特定的词云库的波斯语版本
您可能需要测试这些特定的word cloud库的波斯语版本
我认为您对文件的寻址不正确。它必须是这样的:
wordcloud=wordcloud(font\u path='/Library/Fonts/Arial.ttf')。生成(text)
是的,我想我必须修复font\u路径,但是您提到的路径也返回了相同的错误。顺便说一句,我正在使用google colab平台。这段代码在Anaconda中运行良好,但在google colab中不起作用。唯一需要解决的问题是,我应该在Google ColabI中为font_path输入什么路径?我认为您没有正确寻址文件。它必须是这样的:wordcloud=wordcloud(font\u path='/Library/Fonts/Arial.ttf')。生成(text)
是的,我想我必须修复font\u路径,但是您提到的路径也返回了相同的错误。顺便说一句,我正在使用google colab平台。这段代码在Anaconda中运行良好,但在google colab中不起作用。唯一需要解决的问题是我应该在Google ColabYes中为font_path输入什么路径谢谢,但我的问题的主要部分仍然存在,那就是为font_path参数提供正确的路径。我还尝试了font_path='/Library/Fonts/Arial.ttf',它再次返回了相同的错误(无法打开源代码)。请注意,我的波斯语和阿拉伯语文本打印正确,但未显示在word cloud照片中。另外,我正在使用google colabI在我的本地平台上运行你的代码,它在那里工作。谷歌colab似乎就是这样。如果你能找到给font_path参数加上arial字体的方法,我想这是可以做到的。再次非常感谢是的,谢谢,但是我的问题的主要部分仍然存在,那就是给字体路径参数提供正确的路径。我还尝试了font_path='/Library/Fonts/Arial.ttf',它再次返回了相同的错误(无法打开源代码)。请注意,我的波斯语和阿拉伯语文本打印正确,但未显示在word cloud照片中。另外,我正在使用google colabI在我的本地平台上运行你的代码,它在那里工作。谷歌colab似乎就是这样。如果你能找到给font_path参数加上arial字体的方法,我想这是可以做到的。再次非常感谢