Reputation: 11
This code run the huge data but with all Arabic words written in reverse:
from bidi.algorithm import get_display
import os
import matplotlib.pyplot as plt
from wordcloud import WordCloud
os.chdir("C:")
f = open('example.txt', 'r', encoding = 'utf-8')
data = arabic_reshaper.reshape(f.read())
WordCloud = WordCloud(font_path='arial',background_color='white', mode='RGB',width=2000,height=1000).generate(data)
plt.title("wordcloud")
plt.imshow(WordCloud)
plt.axis("off")
plt.show()
This is my data:
أحمد
خالد
سلمان
سليمان
عبدالله
عبدالرحمن
عبدالرحمن
خالد
صالح
Finally this what I get:
Can someone help me to solve it?
Upvotes: 0
Views: 6097
Reputation: 61
here is the solution , converting an Arabic string to wordcloud using arworldcloud library
pip install ar_wordcloud
from ar_wordcloud import ArabicWordCloud
awc = ArabicWordCloud(font='NotoSansArabic-ExtraBold.ttf')
t = f"عيدفطر2020 سعيد، كل عام وانتم بخير"
awc.from_text(t).to_image()
reference: [for more details visit][1] [1]: https://pypi.org/project/ar_wordcloud/
Upvotes: 0
Reputation: 148
Here is a Good example on how you can generate Arabic wordCloud.
import arabic_reshaper
from bidi.algorithm import get_display
reshaped_text = arabic_reshaper.reshape(text)
bidi_text = get_display(reshaped_text)
wordcloud = WordCloud(font_path='NotoNaskhArabic-Regular.ttf').generate(bidi_text)
wordcloud.to_file("worCloud.png")
Here is a link on how you can do it on : Google colab
Upvotes: 1
Reputation: 20341
Ok. Just created a tiny Arabic wrapper (ar_wordcloud
) to do this. I hope it helps.
$ pip install ar_wordcloud
from ar_wordcloud import ArabicWordCloud
awc = ArabicWordCloud(background_color="white")
t = 'أهلاً وسهلا، اللغة العربية جميلة'
wc = awc.from_text(t)
Or, here is another example without the wrapper:
from collections import Counter
from wordcloud import WordCloud # pip install wordcloud
import matplotlib.pyplot as plt
# -- Arabic text dependencies
from arabic_reshaper import reshape # pip install arabic-reshaper
from bidi.algorithm import get_display # pip install python-bidi
rtl = lambda w: get_display(reshape(f'{w}'))
COUNTS = Counter("السلام عليكم ورحمة الله و بركاته السلام كلمة جميلة".split())
counts = {rtl(k):v for k, v in COUNTS.most_common(10)}
font_file = './NotoNaskhArabic-Regular.ttf' # download from: https://www.google.com/get/noto
wordcloud = WordCloud(font_path=font_file).generate_from_frequencies(counts)
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
The result:
Also, there is a PR discussion on this here: https://github.com/amueller/word_cloud/pull/315
Upvotes: 1
Reputation: 41
First you need to import the arabic_resharper package then use get_display function and pass it to the wordcloud as the following:
from bidi.algorithm import get_display
import os
import matplotlib.pyplot as plt
from wordcloud import WordCloud
import arabic_reshaper # this was missing in your code
# os.chdir("C:")
f = open('example.txt', 'r', encoding='utf-8')
data = arabic_reshaper.reshape(f.read())
data = get_display(data) # add this line
WordCloud = WordCloud(font_path='arial', background_color='white',
mode='RGB', width=2000, height=1000).generate(data)
plt.title("wordcloud")
plt.imshow(WordCloud)
plt.axis("off")
plt.show()
Upvotes: 4