在python中为外语(希伯来语)创建wordcloud [英] create wordcloud in python for foreign language (Hebrew)

查看:182
本文介绍了在python中为外语(希伯来语)创建wordcloud的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个wordcloud。
当我的字符串为英语时,一切正常:

  from wordcloud import WordCloud 
from matplotlib import pyplot as plt
text = Softrock 40-接近P6D要求的6 MHz(根据6.062)-https://groups.yahoo.com/neo/groups/softrock40/conversations/messages
我想要具有可控(非固定)中心频率的USB模型。
wordcloud = WordCloud()。generate(text)
plt.imshow(wordcloud,interpolation ='bilinear' )
plt.axis( off)
plt.show()



但是当我在希伯来语中执行相同操作时,它不会检测到字体,而我只能得到空矩形:

  text = מגשמיהסערהשתחלהיום 
wordcloud = WordCloud()。generate(文本)
plt.imshow(wordcloud,插值='双线性')
plt.axis( off)
plt.show()



有什么想法吗?

解决方案

这与wordcloud本身并没有多大关系,但与呈现方式有更多关系:您使用(默认为)字体,而该字体根本不包含希伯来字符的任何定义。



但是,我们可以使用支持希伯来语字符的字体,例如 FreeSansBold 。我们可以通过 WordCloud 构造函数传递字体的路径:

 从wordcloud进口WordCloud $ b。从matplotlib进口pyplot $ b为PLT 

文本= תחילתושלחורףמאכזבלמדיי,מומחיהמיםבישראלמאמיניםכילראשונההשנהמפלסהכנרתיעלהבצורהמשמעותיתמגשמיהסערהשתחלהיום
wordcloud = WordCloud( font_path ='/ usr / share / fonts / truetype / freefont / FreeSansBold.ttf')。generate(text)
plt.imshow(wordcloud ,插值='双线性')
plt.axis( off)
plt.show()

然后将生成以下词云:





我对希伯来语不是很熟悉,但是我的印象是单词是从左到右书写的,而不是从右到左书写的。无论如何,如果这是一个问题,我们可以使用


I want to create a wordcloud. When my string is in English, everything works fine:

from wordcloud import WordCloud
from matplotlib import pyplot as plt
text="""Softrock 40 - close to the 6 MHz that the P6D requires (6.062 according) - https://groups.yahoo.com/neo/groups/softrock40/conversations/messages
I want the USB model that has a controllable (not fixed) central frequency."""
wordcloud = WordCloud().generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

But when I'm doing the same in Hebrew, it doesn't detect the font, and I get only empty rectangles:

text="""תחילתו של חורף מאכזב למדיי, מומחי המים בישראל מאמינים כי לראשונה השנה מפלס הכנרת יעלה בצורה משמעותית מגשמי הסערה שתחל היום"""
wordcloud = WordCloud().generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

Any ideas?

解决方案

This has not that much to do with the wordcloud itself, but more with the rendering: you use (well the default is) a font that simply does not contains any "definitions" for Hebrew characters. It thus simply renders rectangles instead.

We can however use a font that supports Hebrew characters, for example FreeSansBold. We can pass a path to the font through the WordCloud constructor:

from wordcloud import WordCloud
from matplotlib import pyplot as plt

text="""תחילתו של חורף מאכזב למדיי, מומחי המים בישראל מאמינים כי לראשונה השנה מפלס הכנרת יעלה בצורה משמעותית מגשמי הסערה שתחל היום"""
wordcloud = WordCloud(font_path='/usr/share/fonts/truetype/freefont/FreeSansBold.ttf').generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

then this generates the following word cloud:

I'm not very familiar with Hebrew, but I have the impression that the words are written left-to-right, instead of right-to-left. Anyway, if that is an issue, we can use python-bidi to first process the direction of a language, like:

from wordcloud import WordCloud
from matplotlib import pyplot as plt
from bidi.algorithm import get_display

text="""תחילתו של חורף מאכזב למדיי, מומחי המים בישראל מאמינים כי לראשונה השנה מפלס הכנרת יעלה בצורה משמעותית מגשמי הסערה שתחל היום"""

bidi_text = get_display(text)

wordcloud = WordCloud(font_path='/usr/share/fonts/truetype/freefont/FreeSansBold.ttf').generate(bidi_text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.show()

For the given text, we then obtain the following image:

这篇关于在python中为外语(希伯来语)创建wordcloud的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆