Python word cloud from a html page: Difference between revisions
From wikiluntti
Line 74: | Line 74: | ||
</pre> | </pre> | ||
== The code == | |||
See the code at Github. | |||
== Exercises == | == Exercises == |
Revision as of 23:58, 18 August 2021
Introduction
Analyze html tables using word clouds.
Theory
Fetching the table
Data scraping is easiest using Pandas. BeautifulSoup is an other good option.
Linguistic analyzation
The Finnish language is used, thus Voikko morphological analyzer is used to lemmatize the words into the base format.
sudo apt -y install -y voikko-fi python-libvoikko pip3 install libvoikko
References
https://data.solita.fi/finnish-stemming-and-lemmatization-in-python/
The word cloud
WordCloud library is easy to use. It can create svg graphics, but fonts might get mixed.
#The word cloud from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt stopwords = set(STOPWORDS) #.generate_from_frequencies() wordcloud = WordCloud(width = 800, height = 800, background_color ='white', stopwords = stopwords, collocations=False, min_font_size = 10).generate( ' '.join(map(str, bwords)))
Save in svg format
name = title + "_" + AS.replace(" ","") wordcloud_svg = wordcloud.to_svg(embed_font=True) f = open(name + ".svg","w+") f.write(wordcloud_svg ) f.close() # plot the WordCloud image plt.figure(figsize = (8, 8), facecolor = None) plt.imshow(wordcloud) plt.axis("off") plt.tight_layout(pad = 0) #plt.rcParams['svg.fonttype'] = 'non plt.rcParams["savefig.format"] = 'png' print( name ) plt.savefig( name ) plt.show()
The code
See the code at Github.