Final Project - Word Cloud#


1. Introduction#

In this project, we will create a word cloud from a given text by processing the text, removing punctuation, ignoring cases, counting the frequencies, and ignoring uninteresting or irrelevant words. The project will generate a dictionary containing the word frequencies, which the word cloud module will use to generate an image.

Real-world data processing and visualization applications, such as analyzing customer reviews, news articles, or social media posts, inspired the project task. Completing this project provides valuable experience in processing textual data and generating visual representations of the processed data.

Beginners can use the project and provide the input text by copying and pasting from a website or using a file containing the text. The project includes both pre-written code and code that had to be completed by the learner, allowing them to understand and modify the code to suit their needs.

Overall, this project provides a hands-on experience of working with textual data and generating visualizations, which can be valuable in various fields, including data analysis, natural language processing, and data journalism.

2. Provided Materials#

To begin, you will need to install and import the necessary packages and libraries for the word cloud script and the uploader widget. Running code cells will perform all the necessary installations and imports, but it may take some time to complete. Once you see the final line of output, the code execution is complete, and you can proceed with the instructions for the project.

# Here are all the installations and imports needed to generate the word cloud

%pip install wordcloud
%pip install fileupload
%pip install ipywidgets
!jupyter nbextension install --py --user fileupload
!jupyter nbextension enable --py fileupload
Requirement already satisfied: wordcloud in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (1.8.2.2)
Requirement already satisfied: numpy>=1.6.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from wordcloud) (1.21.5)
Requirement already satisfied: matplotlib in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from wordcloud) (3.5.1)
Requirement already satisfied: pillow in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from wordcloud) (9.0.1)
Requirement already satisfied: packaging>=20.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from matplotlib->wordcloud) (21.3)
Requirement already satisfied: python-dateutil>=2.7 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from matplotlib->wordcloud) (2.8.2)
Requirement already satisfied: pyparsing>=2.2.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from matplotlib->wordcloud) (3.0.4)
Requirement already satisfied: cycler>=0.10 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from matplotlib->wordcloud) (0.11.0)
Requirement already satisfied: fonttools>=4.22.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from matplotlib->wordcloud) (4.25.0)
Requirement already satisfied: kiwisolver>=1.0.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from matplotlib->wordcloud) (1.4.2)
Requirement already satisfied: six>=1.5 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from python-dateutil>=2.7->matplotlib->wordcloud) (1.16.0)
Note: you may need to restart the kernel to use updated packages.
Requirement already satisfied: fileupload in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (0.1.5)
Requirement already satisfied: traitlets>=4.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from fileupload) (5.1.1)
Requirement already satisfied: ipywidgets>=5.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from fileupload) (7.6.5)
Requirement already satisfied: notebook>=4.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from fileupload) (6.4.11)
Requirement already satisfied: ipython>=4.0.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets>=5.1->fileupload) (8.3.0)
Requirement already satisfied: ipykernel>=4.5.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets>=5.1->fileupload) (6.9.1)
Requirement already satisfied: ipython-genutils~=0.2.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets>=5.1->fileupload) (0.2.0)
Requirement already satisfied: nbformat>=4.2.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets>=5.1->fileupload) (5.3.0)
Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets>=5.1->fileupload) (1.0.0)
Requirement already satisfied: widgetsnbextension~=3.5.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets>=5.1->fileupload) (3.5.2)
Requirement already satisfied: jupyter-client<8.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets>=5.1->fileupload) (7.2.2)
Requirement already satisfied: nest-asyncio in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets>=5.1->fileupload) (1.5.5)
Requirement already satisfied: debugpy<2.0,>=1.0.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets>=5.1->fileupload) (1.5.1)
Requirement already satisfied: appnope in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets>=5.1->fileupload) (0.1.2)
Requirement already satisfied: tornado<7.0,>=4.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets>=5.1->fileupload) (6.1)
Requirement already satisfied: matplotlib-inline<0.2.0,>=0.1.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets>=5.1->fileupload) (0.1.2)
Requirement already satisfied: stack-data in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets>=5.1->fileupload) (0.2.0)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets>=5.1->fileupload) (3.0.20)
Requirement already satisfied: jedi>=0.16 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets>=5.1->fileupload) (0.18.1)
Requirement already satisfied: pexpect>4.3 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets>=5.1->fileupload) (4.8.0)
Requirement already satisfied: pickleshare in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets>=5.1->fileupload) (0.7.5)
Requirement already satisfied: backcall in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets>=5.1->fileupload) (0.2.0)
Requirement already satisfied: decorator in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets>=5.1->fileupload) (5.1.1)
Requirement already satisfied: pygments>=2.4.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets>=5.1->fileupload) (2.11.2)
Requirement already satisfied: setuptools>=18.5 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets>=5.1->fileupload) (61.2.0)
Requirement already satisfied: parso<0.9.0,>=0.8.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jedi>=0.16->ipython>=4.0.0->ipywidgets>=5.1->fileupload) (0.8.3)
Requirement already satisfied: jupyter-core>=4.9.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets>=5.1->fileupload) (4.10.0)
Requirement already satisfied: python-dateutil>=2.8.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets>=5.1->fileupload) (2.8.2)
Requirement already satisfied: entrypoints in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets>=5.1->fileupload) (0.4)
Requirement already satisfied: pyzmq>=22.3 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets>=5.1->fileupload) (22.3.0)
Requirement already satisfied: fastjsonschema in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbformat>=4.2.0->ipywidgets>=5.1->fileupload) (2.15.1)
Requirement already satisfied: jsonschema>=2.6 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbformat>=4.2.0->ipywidgets>=5.1->fileupload) (3.2.0)
Requirement already satisfied: six>=1.11.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jsonschema>=2.6->nbformat>=4.2.0->ipywidgets>=5.1->fileupload) (1.16.0)
Requirement already satisfied: attrs>=17.4.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jsonschema>=2.6->nbformat>=4.2.0->ipywidgets>=5.1->fileupload) (21.4.0)
Requirement already satisfied: pyrsistent>=0.14.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jsonschema>=2.6->nbformat>=4.2.0->ipywidgets>=5.1->fileupload) (0.18.0)
Requirement already satisfied: terminado>=0.8.3 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.2->fileupload) (0.13.1)
Requirement already satisfied: prometheus-client in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.2->fileupload) (0.13.1)
Requirement already satisfied: Send2Trash>=1.8.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.2->fileupload) (1.8.0)
Requirement already satisfied: jinja2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.2->fileupload) (2.11.3)
Requirement already satisfied: nbconvert>=5 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.2->fileupload) (6.4.4)
Requirement already satisfied: argon2-cffi in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.2->fileupload) (21.3.0)
Requirement already satisfied: beautifulsoup4 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.2->fileupload) (4.11.1)
Requirement already satisfied: bleach in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.2->fileupload) (4.1.0)
Requirement already satisfied: nbclient<0.6.0,>=0.5.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.2->fileupload) (0.5.13)
Requirement already satisfied: pandocfilters>=1.4.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.2->fileupload) (1.5.0)
Requirement already satisfied: defusedxml in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.2->fileupload) (0.7.1)
Requirement already satisfied: testpath in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.2->fileupload) (0.5.0)
Requirement already satisfied: mistune<2,>=0.8.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.2->fileupload) (0.8.4)
Requirement already satisfied: jupyterlab-pygments in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.2->fileupload) (0.1.2)
Requirement already satisfied: MarkupSafe>=0.23 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jinja2->notebook>=4.2->fileupload) (2.0.1)
Requirement already satisfied: ptyprocess>=0.5 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from pexpect>4.3->ipython>=4.0.0->ipywidgets>=5.1->fileupload) (0.7.0)
Requirement already satisfied: wcwidth in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=4.0.0->ipywidgets>=5.1->fileupload) (0.2.5)
Requirement already satisfied: argon2-cffi-bindings in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from argon2-cffi->notebook>=4.2->fileupload) (21.2.0)
Requirement already satisfied: cffi>=1.0.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from argon2-cffi-bindings->argon2-cffi->notebook>=4.2->fileupload) (1.15.0)
Requirement already satisfied: pycparser in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->notebook>=4.2->fileupload) (2.21)
Requirement already satisfied: soupsieve>1.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from beautifulsoup4->nbconvert>=5->notebook>=4.2->fileupload) (2.3.1)
Requirement already satisfied: packaging in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from bleach->nbconvert>=5->notebook>=4.2->fileupload) (21.3)
Requirement already satisfied: webencodings in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from bleach->nbconvert>=5->notebook>=4.2->fileupload) (0.5.1)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from packaging->bleach->nbconvert>=5->notebook>=4.2->fileupload) (3.0.4)
Requirement already satisfied: pure-eval in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from stack-data->ipython>=4.0.0->ipywidgets>=5.1->fileupload) (0.2.2)
Requirement already satisfied: executing in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from stack-data->ipython>=4.0.0->ipywidgets>=5.1->fileupload) (0.8.3)
Requirement already satisfied: asttokens in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from stack-data->ipython>=4.0.0->ipywidgets>=5.1->fileupload) (2.0.5)
Note: you may need to restart the kernel to use updated packages.
Requirement already satisfied: ipywidgets in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (7.6.5)
Requirement already satisfied: traitlets>=4.3.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets) (5.1.1)
Requirement already satisfied: ipython-genutils~=0.2.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets) (0.2.0)
Requirement already satisfied: ipython>=4.0.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets) (8.3.0)
Requirement already satisfied: jupyterlab-widgets>=1.0.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets) (1.0.0)
Requirement already satisfied: nbformat>=4.2.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets) (5.3.0)
Requirement already satisfied: widgetsnbextension~=3.5.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets) (3.5.2)
Requirement already satisfied: ipykernel>=4.5.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipywidgets) (6.9.1)
Requirement already satisfied: matplotlib-inline<0.2.0,>=0.1.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets) (0.1.2)
Requirement already satisfied: tornado<7.0,>=4.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets) (6.1)
Requirement already satisfied: nest-asyncio in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets) (1.5.5)
Requirement already satisfied: debugpy<2.0,>=1.0.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets) (1.5.1)
Requirement already satisfied: jupyter-client<8.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets) (7.2.2)
Requirement already satisfied: appnope in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipykernel>=4.5.1->ipywidgets) (0.1.2)
Requirement already satisfied: pickleshare in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets) (0.7.5)
Requirement already satisfied: pygments>=2.4.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets) (2.11.2)
Requirement already satisfied: pexpect>4.3 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets) (4.8.0)
Requirement already satisfied: setuptools>=18.5 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets) (61.2.0)
Requirement already satisfied: decorator in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets) (5.1.1)
Requirement already satisfied: backcall in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets) (0.2.0)
Requirement already satisfied: jedi>=0.16 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets) (0.18.1)
Requirement already satisfied: stack-data in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets) (0.2.0)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from ipython>=4.0.0->ipywidgets) (3.0.20)
Requirement already satisfied: parso<0.9.0,>=0.8.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jedi>=0.16->ipython>=4.0.0->ipywidgets) (0.8.3)
Requirement already satisfied: pyzmq>=22.3 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (22.3.0)
Requirement already satisfied: jupyter-core>=4.9.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (4.10.0)
Requirement already satisfied: python-dateutil>=2.8.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (2.8.2)
Requirement already satisfied: entrypoints in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jupyter-client<8.0->ipykernel>=4.5.1->ipywidgets) (0.4)
Requirement already satisfied: fastjsonschema in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbformat>=4.2.0->ipywidgets) (2.15.1)
Requirement already satisfied: jsonschema>=2.6 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbformat>=4.2.0->ipywidgets) (3.2.0)
Requirement already satisfied: attrs>=17.4.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jsonschema>=2.6->nbformat>=4.2.0->ipywidgets) (21.4.0)
Requirement already satisfied: pyrsistent>=0.14.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jsonschema>=2.6->nbformat>=4.2.0->ipywidgets) (0.18.0)
Requirement already satisfied: six>=1.11.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jsonschema>=2.6->nbformat>=4.2.0->ipywidgets) (1.16.0)
Requirement already satisfied: ptyprocess>=0.5 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from pexpect>4.3->ipython>=4.0.0->ipywidgets) (0.7.0)
Requirement already satisfied: wcwidth in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=4.0.0->ipywidgets) (0.2.5)
Requirement already satisfied: notebook>=4.4.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from widgetsnbextension~=3.5.0->ipywidgets) (6.4.11)
Requirement already satisfied: argon2-cffi in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (21.3.0)
Requirement already satisfied: nbconvert>=5 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (6.4.4)
Requirement already satisfied: prometheus-client in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.13.1)
Requirement already satisfied: terminado>=0.8.3 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.13.1)
Requirement already satisfied: jinja2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.11.3)
Requirement already satisfied: Send2Trash>=1.8.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.8.0)
Requirement already satisfied: testpath in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.5.0)
Requirement already satisfied: pandocfilters>=1.4.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.5.0)
Requirement already satisfied: bleach in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (4.1.0)
Requirement already satisfied: defusedxml in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.7.1)
Requirement already satisfied: beautifulsoup4 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (4.11.1)
Requirement already satisfied: mistune<2,>=0.8.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.8.4)
Requirement already satisfied: jupyterlab-pygments in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.1.2)
Requirement already satisfied: nbclient<0.6.0,>=0.5.0 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.5.13)
Requirement already satisfied: MarkupSafe>=0.23 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from jinja2->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.0.1)
Requirement already satisfied: argon2-cffi-bindings in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (21.2.0)
Requirement already satisfied: cffi>=1.0.1 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (1.15.0)
Requirement already satisfied: pycparser in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from cffi>=1.0.1->argon2-cffi-bindings->argon2-cffi->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.21)
Requirement already satisfied: soupsieve>1.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from beautifulsoup4->nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (2.3.1)
Requirement already satisfied: webencodings in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from bleach->nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (0.5.1)
Requirement already satisfied: packaging in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from bleach->nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (21.3)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from packaging->bleach->nbconvert>=5->notebook>=4.4.1->widgetsnbextension~=3.5.0->ipywidgets) (3.0.4)
Requirement already satisfied: executing in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from stack-data->ipython>=4.0.0->ipywidgets) (0.8.3)
Requirement already satisfied: pure-eval in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from stack-data->ipython>=4.0.0->ipywidgets) (0.2.2)
Requirement already satisfied: asttokens in /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages (from stack-data->ipython>=4.0.0->ipywidgets) (2.0.5)
Note: you may need to restart the kernel to use updated packages.
Installing /Users/rajdeep_ch/opt/anaconda3/lib/python3.9/site-packages/fileupload/static -> fileupload
Up to date: /Users/rajdeep_ch/Library/Jupyter/nbextensions/fileupload/widget.js
Up to date: /Users/rajdeep_ch/Library/Jupyter/nbextensions/fileupload/extension.js
Up to date: /Users/rajdeep_ch/Library/Jupyter/nbextensions/fileupload/fileupload/widget.js
- Validating: OK

    To initialize this nbextension in the browser every time the notebook (or other app) loads:
    
          jupyter nbextension enable fileupload --user --py
    
Enabling notebook extension fileupload/extension...
      - Validating: OK
import wordcloud
import numpy as np 
from matplotlib import pyplot as plt 
from IPython.display import display 
import fileupload
import io 
import sys

To upload the text file, run the following cell that contains all the code for a custom uploader widget.

Once the cell is run, a Browse buttion should appear below it. Clicking this button will open a nagivation window that allows locating the saved text file.

def _upload():

    _upload_widget = fileupload.FileUploadWidget()

    def _cb(change):
        global file_contents
        decoded = io.StringIO(change['owner'].data.decode('utf-8'))
        filename = change['owner'].filename
        print('Uploaded `{}` ({:.2f} kB)'.format(
            filename, len(decoded.read()) / 2 **10))
        file_contents = decoded.getvalue()

    _upload_widget.observe(_cb, names='data')
    display(_upload_widget)

3. Writing the function#

The function takes a sentence as input, cleans it by removing punctuations and uninteresting words, and then counts the frequency of each word. The frequency count is stored in a dictionary, which is then used to generate a word cloud image. The output is a Numpy array representing the image.

The function iterates through the words in the text, removes punctuation, ignores word cases (lower-case and upper-case are treated similarly) and words that do not contain all alphabets, and counts the frequency of each remaining word.

Additionally, the function ignores boring words such as “and” or “the”.

def generate_from_frequencies(sentence):
    """ 
    Process the input sentence to generate a word cloud

    Parameters:
        sentence (str): A string containing the input sentence

    Returns:
        cloud (numpy.ndarry): A Numpy array representing the word cloud image
        generate from the input sentence
    """
    final_text = []
    the_dictionary = {}

    argument1 = sentence.split()
    
    punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~--'''
    uninteresting_words = ["the", "a", "to", "if", "is", "it", "of","us","so","and", "or", "an", "as", "i", "me", "my", "up", "oh" \
    "we", "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", "hers", "its", "they", "them", \
    "their", "what", "which", "who", "whom", "this", "that", "am", "are", "was","we","were", "be", "been","for", "being", \
    "have", "has", "had", "do", "does", "did", "but", "at", "by", "with", "from", "here", "when", "where", "how", \
    "all", "any", "both", "each", "few", "more", "some", "such", "no", "nor", "too", "very", "can", "will", "just","in","on","you"]
    
    # Running through the text and processing it
    # to remove punctuations and uninteresting words
    for word in argument1:

        text = ""
        word_in_lower = word.lower()
        
        for letter in word_in_lower:

            if letter not in punctuations and letter.isalpha():
                text = text + letter
            else:
                text = text + ""
        
        final_text.append(text)

    # The processed text has been stored in
    # final_text (list)


    # Running through the final_text list and
    # updating the dictionary with word frequency
    for word in final_text:
        if word not in uninteresting_words:
            
            if word not in the_dictionary:
                count = 1
                the_dictionary[word] = count
            
            else:
                freq = the_dictionary[word]
                the_dictionary[word] = freq + 1

    # Producing Numpy Array
    cloud = wordcloud.WordCloud(width=1000,height=1000,collocations=False)
    cloud.generate_from_frequencies(the_dictionary)
    return cloud.to_array()

4. Generating the Word Cloud#

The code in the cell below generates the word cloud image from the text that is stored in the file_contents variable using the generate_from_frequencies() function.

The resulting image is then displayed using the plt.show() and plt.imshow() functions with the addition of setting the figure size and turning off the axis label.

Finally, a title is printed above the image.

_upload()
Uploaded `lucy.txt` (1.17 kB)
myimage = generate_from_frequencies(file_contents)
plt.figure(figsize=(20,10))
plt.imshow(myimage, interpolation = 'nearest')
plt.axis('off')

print("Run 1")
print("The Beatles - Lucy in the Sky With Diamonds")

plt.show()
Run 1
The Beatles - Lucy in the Sky With Diamonds
_images/final_project-2_11_1.png
_upload()
Uploaded `russell.txt` (32.02 kB)
newimage = generate_from_frequencies(file_contents)
plt.figure(figsize=(20,10))
plt.imshow(newimage, interpolation = 'nearest')
plt.axis('off')

print("Run 2")
print("Chapter 1 - Problems of Philosophy by Bertrand Russell")

plt.show()
Run 2
Chapter 1 - Problems of Philosophy by Bertrand Russell
_images/final_project-2_13_1.png
_upload()
Uploaded `do_not_gentle.txt` (0.85 kB)
another_image = generate_from_frequencies(file_contents)
plt.figure(figsize=(20,10))
plt.imshow(another_image, interpolation = 'nearest')
plt.axis('off')

print("Run 3")
print("Do not go gentle into that good night - Dylan Thomas")

plt.show()
Run 3
Do not go gentle into that good night - Dylan Thomas
_images/final_project-2_15_1.png