gpt4 book ai didi

python - 用阿拉伯字符绘制直方图

转载 作者:行者123 更新时间:2023-12-04 12:00:58 24 4
gpt4 key购买 nike

我正在尝试绘制以 arabic 书写的最常用单词的直方图,但我想不出办法做到这一点。我所能得到的只是切片字符,而不是编译后的单词。

这是我得到的一个例子:

enter image description here

import seaborn as sns

import pandas as pd

res = {
'الذكاء': 8,
'الاصطناعي': 9,
'هو': 2,
'سلوك': 1,
'وخصائص': 1,
'معينة': 1,
'تتسم': 1
}

df = pd.DataFrame(res.items(), columns=['word', 'count'])

sns.set(style="whitegrid")
ax = sns.barplot(x="count", y="word", data=df)

如上图所示,我希望对这些字符进行编译,就像字典中提到的那样。

最佳答案

这似乎与 arabic_reshaper 运行良好和 bidi正如@Sheldore 所指出的。

import seaborn as sns
import pandas as pd
import arabic_reshaper
from bidi.algorithm import get_display

res = {
'الذكاء': 8,
'الاصطناعي': 9,
'هو': 2,
'سلوك': 1,
'وخصائص': 1,
'معينة': 1,
'تتسم': 1
}

res2 = {get_display(arabic_reshaper.reshape(k)): v for k,v in res.items()}

df = pd.DataFrame(res2.items(), columns=['word', 'count'])

sns.set(style="whitegrid")
ax = sns.barplot(x="count", y="word", data=df)
barplot with arabic labels

关于python - 用阿拉伯字符绘制直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56292850/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com