Build a WordCloud Visual Using Python and Keyword Extractor API

blog details
author


WordCloud is the most popular visualization format to present the most frequent keywords from a specific object. It’s helpful on machine learning, SEO, and social sentiment dataset generation. In this piece, I would walk through how to generate a WordCloud from a list dataset using Python.

Table of Contents: Build a WordCloud Visual Using Python and Keyword Extractor API


Install Related Python Packages

For interacting with keyword extractor and outputting WordCloud photo, we need 5 main packages as follows:


from wordcloud import WordCloud
from matplotlib import pyp. lot as plt
from PIL import Image
import requests
import numpy as np

Prepare a PNG image

Frequent keyword visualization can be engaging with audiences using different shape of photos. The photos can fit in the presentation for the purpose to better convey the data insights.

Thus, before building a wordcloud, we need to select an image in PNG format, which can be filled with keywords later on.

Arraywise the Image Mask

Raw materials are ready. First thing first, we use Pillow to process the image, that includes setting the colors, size, and color mode. Here are the code sample as follows:


icon = Image.open('imageMask2.png')
image_mask = Image.new(mode='RGB', size=icon.size, color=(255,255,255))
image_mask.paste(icon, box=icon)
# Convert an image object to an array ##
rgb_array = np.array(image_mask)
print(rgb_array.shape)

Then, we need to array wise the image mask strings using Numpy.

Import a list of keywords from the target content

Image materials and array dataset are ready. Now, we need to import a list of keywords you aim to build a WordCloud. Here is the keyword extractor API and you can generate top 100 frequent keywords from the co. ntent you plugin.

https://www.buyfromlo.com/api/keyword-extractor

If you need to scrape the article content and social content before applying them in keyword extractor, please check out BUYFROMLO Scraper APIs

https://www.buyfromlo.com/api?allType=Scraper

Convert the List Data type into Strings

For the WordCloud Python package, they can’t read list datatypes. Thus, we need to convert the keyword list into strings using a join method like a full article stored in txt file. Here is the code sample as follows:


stringSmartHome = (
).join(whole_frequent_words)

Decorate and output the WordCloud

We’re almost there and here it’s time to decorate the WordCloud using WordCloud class.

In the WordCloud class, there are several arguments we need to deploy. Here is a list

  • Mask: Import the image mask array dataset
  • background_color : Set up the WordCloud background color
  • Max_words: Define the amount of keywords showing in the WordCloud photo
  • Colormap: color tone
  • Max_font_size: keyword font size

Here is the sample code as follows:


word_cloud = WordCloud(mask=rgb_array, background_color='white', max_words=8000, colormap='ocean', max_font_size=300).generate(stringSmartHome.upper())

Show the WordCloud image

Last but not least, we use matplotlib to output the photos


plt.imshow(word_cloud, interpolation=. 'bilinear')
plt.axis('off')
plt.show()

Full Python Script of WordCloud using BuyfromLo Keyword Extractor


No Comment at the moment...