如何在python中生成孟加拉语文本的wordcloud?

2024-05-18 18:21:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试了下面的代码:

!pip install python-bidi
from wordcloud import WordCloud
from matplotlib import pyplot as plt
from bidi.algorithm import get_display

text="""মুস্তাফিজ"""

bidi_text = get_display(text)
print(bidi_text)
# https://github.com/amueller/word_cloud/issues/367
# https://stackoverflow.com/questions/54063438/create-wordcloud-in-python-for-foreign-language-hebrew
# https://www.omicronlab.com/bangla-fonts.html
rgx = r"[\u0980-\u09FF]+"
wordcloud = WordCloud(font_path='/content/Siyamrupali.ttf').generate(bidi_text)

#wordcloud = WordCloud(font_path='/content/FreeSansBold.ttf').generate(bidi_text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

然后我得到这个错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-87-56d899c0de07> in <module>()
     12 # https://www.omicronlab.com/bangla-fonts.html
     13 rgx = r"[\u0980-\u09FF]+"
---> 14 wordcloud = WordCloud(font_path='/content/Siyamrupali.ttf').generate(bidi_text)
     15 
     16 #wordcloud = WordCloud(font_path='/content/FreeSansBold.ttf').generate(bidi_text)

2 frames
/usr/local/lib/python3.6/dist-packages/wordcloud/wordcloud.py in generate_from_frequencies(self, frequencies, max_font_size)
    381         if len(frequencies) <= 0:
    382             raise ValueError("We need at least 1 word to plot a word cloud, "
--> 383                              "got %d." % len(frequencies))
    384         frequencies = frequencies[:self.max_words]
    385 

ValueError:我们至少需要1个单词来绘制单词云,得到0。

此行没有选取孟加拉语单词:wordcloud=wordcloud(font\u path='/content/Siyamrupali.ttf')。生成(bidi\u text)

我尝试了几乎所有的孟加拉语字体:https://www.omicronlab.com/bangla-fonts.html

什么都不管用


Tags: pathtextfromhttpsimportcompltcontent
3条回答

您没有使用word cloud中定义的正则表达式更改regexp。在word cloud中处理文本时,它无法匹配模式并返回空列表。 创建word cloud对象时传递rgx变量将解决您的问题

wordcloud = WordCloud(font_path='/content/Siyamrupali.ttf',regexp=rgx).generate(bidi_text)

下面是完整的代码片段

!pip install python-bidi
from wordcloud import WordCloud
from matplotlib import pyplot as plt
from bidi.algorithm import get_display

text="""মুস্তাফিজ"""

bidi_text = get_display(text)
print(bidi_text)
# https://github.com/amueller/word_cloud/issues/367
# https://stackoverflow.com/questions/54063438/create-wordcloud-in-python-for-foreign-language-hebrew
# https://www.omicronlab.com/bangla-fonts.html
rgx = r"[\u0980-\u09FF]+"
wordcloud = WordCloud(font_path='/content/Siyamrupali.ttf', 
regexp=rgx).generate(bidi_text)

#wordcloud = WordCloud(font_path='/content/FreeSansBold.ttf').generate(bidi_text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

我使用以下代码生成了一个孟加拉语单词cloud。您可以尝试一下:

def生成单词云(self、author\u post、vocabularyWordnumber、img\u文件、stop\u Word\u root\u路径):

stop_word_file = stop_word_root_path+'/stopwords-bn.txt'
print(stop_word_file)
f = open(stop_word_file, "r", encoding="utf8")
stop_word = f.read().split("\n")
print(stop_word)

final_text = " ".join(author_post)
print(final_text)
wordcloud = WordCloud(stopwords = stop_word, font_path='/usr/share/fonts/truetype/freefont/kalpurush.ttf',
    width = 600, height = 500,max_font_size=300, max_words=vocabularyWordnumber,
                      min_word_length=4, background_color="black").generate(final_text)
wordcloud.to_file(img_file)

我遵循了this comment,最终可以在Ubuntu中解决这个问题

步骤1

!sudo apt-get install libfreetype6-dev libharfbuzz-dev libfribidi-dev gtk-doc-tools

步骤2

!wget -O raqm-0.7.0.tar.gz https://raw.githubusercontent.com/python-pillow/pillow-depends/master/raqm-0.7.0.tar.gz

现在raqm-0.7.0.tar.gz文件应该在下载部分

步骤3

!tar -xzvf raqm-0.7.0.tar.gz

步骤4

!cd raqm-0.7.0

步骤5

!./configure  prefix=/usr && make -j4 && sudo make -j4 install

步骤6

现在您只需重新安装枕头库。激活正确的环境。然后运行以下命令:

python3 -m pip install  upgrade pip
python3 -m pip install  upgrade Pillow

就这样!现在,您有了一个工作枕头库,可以在图像中生成正确的孟加拉文和其他印度文字体

此外,正如@Farzana Eva在她的评论中所建议的,您需要在wordcloud对象中传递rgx变量

相关问题 更多 >

    热门问题