Python 反映阿拉伯语单词Cloud

Python 反映阿拉伯语单词Cloud,python,python-3.x,word-cloud,bidi,wordcloud2,Python,Python 3.x,Word Cloud,Bidi,Wordcloud2,这段代码运行大量数据,但所有阿拉伯单词的书写方式都是相反的: 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())

这段代码运行大量数据,但所有阿拉伯单词的书写方式都是相反的:

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()
这是我的数据:

أحمد
خالد
سلمان
سليمان
عبدالله
عبدالرحمن
عبدالرحمن
خالد
صالح
最后,我得到的是:


有人能帮我解决吗?

首先,您需要导入arabic_resharper软件包,然后使用get_显示功能,并将其传递给wordcloud,如下所示:

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()

首先需要导入arabic_resharper包,然后使用get_display函数将其传递给wordcloud,如下所示:

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()
更新 嗯。刚刚创建了一个小小的阿拉伯语包装器()来实现这一点。我希望有帮助

$pip安装ar\u wordcloud

从ar_wordcloud导入ArabicWordCloud
awc=阿拉伯云(背景颜色=“白色”)
t=‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’
wc=awc.自_文本(t)


或者,下面是另一个没有包装器的示例:

从集合导入计数器
从wordcloud导入wordcloud#pip安装wordcloud
将matplotlib.pyplot作为plt导入
#--阿拉伯文文本依赖项
从arabic_整形器导入整形器#pip安装arabic整形器
从bidi.algorithm import get#u display#pip install python bidi
rtl=lambda w:get_显示(重塑(f'{w}'))
计数=计数器
计数={rtl(k):v代表k,v代表计数。最常见(10)}
font#u file='./NotoNaskhArabic Regular.ttf'#下载自:https://www.google.com/get/noto
wordcloud=wordcloud(字体路径=字体文件)。根据频率(计数)生成字体
plt.imshow(wordcloud,interpolation=“双线性”)
plt.轴(“关闭”)
plt.show()
结果是:

此外,这里还有一个公关讨论:

更新 嗯。刚刚创建了一个小小的阿拉伯语包装器()来实现这一点。我希望有帮助

$pip安装ar\u wordcloud

从ar_wordcloud导入ArabicWordCloud
awc=阿拉伯云(背景颜色=“白色”)
t=‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’、‘t’
wc=awc.自_文本(t)


或者,下面是另一个没有包装器的示例:

从集合导入计数器
从wordcloud导入wordcloud#pip安装wordcloud
将matplotlib.pyplot作为plt导入
#--阿拉伯文文本依赖项
从arabic_整形器导入整形器#pip安装arabic整形器
从bidi.algorithm import get#u display#pip install python bidi
rtl=lambda w:get_显示(重塑(f'{w}'))
计数=计数器
计数={rtl(k):v代表k,v代表计数。最常见(10)}
font#u file='./NotoNaskhArabic Regular.ttf'#下载自:https://www.google.com/get/noto
wordcloud=wordcloud(字体路径=字体文件)。根据频率(计数)生成字体
plt.imshow(wordcloud,interpolation=“双线性”)
plt.轴(“关闭”)
plt.show()
结果是:


此外,这里还有一个公关讨论:

这是一个关于如何生成阿拉伯语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")

这里有一个关于如何在上执行的链接:

这里有一个关于如何生成阿拉伯语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")

这里有一个关于如何在上完成的链接:

你能再解释一下你的问题吗?您对算法的期望是什么?阿拉伯文整形器的作用是什么?当您省略该行或仅在该操作前后打印数据时会发生什么?文本中也可能有一些从右到左的隐藏标记,被python的
打印
,或
阿拉伯文重塑器
,或单词云生成器忽略。您是否尝试过使用
s[::-1]
“手动”反转字符串?我预期会出现阿拉伯单词云,但我得到的是类似上图的阿拉伯反转单词。如果我删除它,阿拉伯语重塑器不会做任何事,将得到相同的结果。关于s[::-1]你认为应该加哪一行?你能再解释一下你的问题吗?您对算法的期望是什么?阿拉伯文整形器的作用是什么?当您省略该行或仅在该操作前后打印数据时会发生什么?文本中也可能有一些从右到左的隐藏标记,被python的
打印
,或
阿拉伯文重塑器
,或单词云生成器忽略。您是否尝试过使用
s[::-1]
“手动”反转字符串?我预期会出现阿拉伯单词云,但我得到的是类似上图的阿拉伯反转单词。如果我删除它,阿拉伯语重塑器不会做任何事,将得到相同的结果。关于s[::-1]您认为应该添加哪一行?