如何使用Python生成阿拉伯文字wordcloud并从频率函数生成?

问题描述 投票:0回答:1

使用Python生成阿拉伯文本wordcloud和generate_from_frequencies函数。

python-3.x arabic word-cloud
1个回答
0
投票
import arabic_reshaper
from pyarabic.araby import tokenize
from pyarabic.unshape import unshaping_text
from pyarabic import araby
import matplotlib.pyplot as pPlot
from wordcloud import WordCloud, STOPWORDS
import numpy as npy
from PIL import Image
from matplotlib import pyplot as plt
# you can read file as follow:
# path = "path/file.txt"
# text = open(path, encoding='utf-8')
# newtext = text.read() or simply by direct string variable
text = 'الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ  الرَّحْمَٰنِ الرَّحِيمِ  مَالِكِ يَوْمِ الدِّينِ  إِيَّاكَ نَعْبُدُ وَإِيَّاكَ نَسْتَعِينُ  اهْدِنَا الصِّرَاطَ الْمُسْتَقِيمَ  صِرَاطَ الَّذِينَ أَنْعَمْتَ عَلَيْهِمْ غَيْرِ الْمَغْضُوبِ عَلَيْهِمْ وَلَا الضَّالِّينَ'
text_to_be_reshaped = text
reshaped_text = arabic_reshaper.reshape(text_to_be_reshaped)
rev_text = reshaped_text[::-1]  
dictionary = {}
lst = tokenize(rev_text)
for elements in lst:  
    if elements in dictionary: 
        dictionary[elements] += 1
    else: 
        dictionary.update({elements: 1})
print(dictionary)
cloud = WordCloud(background_color = "white", font_path = 'arial.ttf', max_words = 200, stopwords = set(STOPWORDS))
cloud.generate_from_frequencies(dictionary)
cloud.to_file("wordCloud.png")

# Many thanks also to Coursera and Google IT Automation with Python 
# Professional Certificate Instructors
# And of course many thanks to stackoverflow which allowed me to get advanced 
# help for free
# by Hamdi.Br. The quieter you are, the more you will listen.
© www.soinside.com 2019 - 2024. All rights reserved.