python - 词云阿拉伯语

标签 python python-3.x word-cloud wordcloud2

当我尝试运行阿拉伯语大数据时,Python 中的 WordCloud 代码遇到了一些问题 这是我的代码:

from os import path
import codecs
from wordcloud import WordCloud
import arabic_reshaper
from bidi.algorithm import get_display
d = path.dirname(__file__)
f = codecs.open(path.join(d, 'C:/example.txt'), 'r', 'utf-8')
text = arabic_reshaper.reshape(f.read())
text = get_display(text)
wordcloud = WordCloud(font_path='arial',background_color='white', mode='RGB',width=1500,height=800).generate(text)
wordcloud.to_file("arabic_example.png")

这是我得到的错误:

Traceback (most recent call last):

File "", line 1, in runfile('C:/Users/aam20/Desktop/python/codes/WordClouds/wordcloud_True.py', wdir='C:/Users/aam20/Desktop/python/codes/WordClouds')

File "C:\Users\aam20\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace)

File "C:\Users\aam20\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/aam20/Desktop/python/codes/WordClouds/wordcloud_True.py", line 28, in text = get_display(text)

File "C:\Users\aam20\Anaconda3\lib\site-packages\bidi\algorithm.py", line 648, in get_display resolve_implicit_levels(storage, debug)

File "C:\Users\aam20\Anaconda3\lib\site-packages\bidi\algorithm.py", line 466, in resolve_implicit_levels

'%s not allowed here' % _ch['type']

AssertionError: RLI not allowed here

有人可以帮忙解决这个问题吗?

最佳答案

我尝试用下面提到的方法预处理文本!在调用 reshape 器之前,它对我有用。

def removeWeirdChars(text):
    weirdPatterns = re.compile("["
                               u"\U0001F600-\U0001F64F"  # emoticons
                               u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                               u"\U0001F680-\U0001F6FF"  # transport & map symbols
                               u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                               u"\U00002702-\U000027B0"
                               u"\U000024C2-\U0001F251"
                               u"\U0001f926-\U0001f937"
                               u'\U00010000-\U0010ffff'
                               u"\u200d"
                               u"\u2640-\u2642"
                               u"\u2600-\u2B55"
                               u"\u23cf"
                               u"\u23e9"
                               u"\u231a"
                               u"\u3030"
                               u"\ufe0f"
                               u"\u2069"
                               u"\u2066"
                               u"\u200c"
                               u"\u2068"
                               u"\u2067"
                               "]+", flags=re.UNICODE)
    return weirdPatterns.sub(r'', text)

关于python - 词云阿拉伯语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49362300/

相关文章:

linux - R中没有 "tm"的词云

python - 保留列名顺序 to_dict

python - 从 n 个相等的项目中挑选与最小数字相关的项目

python-3.x - 如何使用 Selenium 在 Steam Guard 提示符中输入文本?

python-3.x - 带有 Flask、sqlalchemy 和 sqlite 的 Openshift 应用程序 - 数据库恢复问题

java - 如何使用Kumo Java词云?

javascript - 使用种子值在刷新之间保持 d3-cloud 一致?

Python Regex 返回带括号的值

python - InterX 到 python 的翻译代码出现故障

python - 使用 setdefault,但不想重新分配回 map/dict