Abdulrahman
Abdulrahman

Reputation: 11

Reflect Arabic Word-Cloud

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:

WordCloud with Arabic in reverse

Can someone help me to solve it?

Upvotes: 0

Views: 6097

Answers (4)

M Siraj Abbasi
M Siraj Abbasi

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

Mohamed Berrimi
Mohamed Berrimi

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

Aziz Alto
Aziz Alto

Reputation: 20341

Update

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)

enter image description here


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:

enter image description here

Also, there is a PR discussion on this here: https://github.com/amueller/word_cloud/pull/315

Upvotes: 1

Ammar Al Zayer
Ammar Al Zayer

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

Related Questions