{"id":141,"date":"2022-05-08T00:46:44","date_gmt":"2022-05-08T00:46:44","guid":{"rendered":"https:\/\/codecrypt76.com\/?p=141"},"modified":"2022-11-25T16:54:37","modified_gmt":"2022-11-25T16:54:37","slug":"create-a-wordcloud-using-python","status":"publish","type":"post","link":"https:\/\/codecrypt76.com\/index.php\/2022\/05\/08\/create-a-wordcloud-using-python\/","title":{"rendered":"Create a WordCloud using Python"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"529\" src=\"https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/05\/EDGARALLANPOEBYSAMSHEARON2017-LowResolution-1024x529.jpg\" alt=\"The Raven, by Edgar Allan Poe\" class=\"wp-image-146\" srcset=\"https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/05\/EDGARALLANPOEBYSAMSHEARON2017-LowResolution-1024x529.jpg 1024w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/05\/EDGARALLANPOEBYSAMSHEARON2017-LowResolution-300x155.jpg 300w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/05\/EDGARALLANPOEBYSAMSHEARON2017-LowResolution-768x396.jpg 768w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/05\/EDGARALLANPOEBYSAMSHEARON2017-LowResolution-1536x793.jpg 1536w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/05\/EDGARALLANPOEBYSAMSHEARON2017-LowResolution-2048x1057.jpg 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>The Raven, by Edgar Allan Poe<\/figcaption><\/figure>\n\n\n\n<p>Here are the instructions I took to create a <strong>WordCloud<\/strong> project for the <a href=\"https:\/\/www.coursera.org\/professional-certificates\/google-it-support\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Google IT Automation with Python Course\">Google IT Automation with Python Course<\/a>. A word cloud is <strong>a simple yet powerful visual representation object for text processing<\/strong>, which shows the most frequent word with bigger and bolder letters, and with different colors. The smaller the size of the word the less important it is. The text I used for this project was <em><a href=\"https:\/\/en.wikipedia.org\/wiki\/The_Raven\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"The Raven\">The Raven<\/a> <\/em>, by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Edgar_Allan_Poe\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"Edgar Allan Poe\">Edgar Allan Poe<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">The Raven <br><br>by<br><br>Edgar Allan Poe<br><br><br>  Once upon a midnight dreary, while I pondered, weak and weary,<br>  Over many a quaint and curious volume of forgotten lore\u2014<br>  While I nodded, nearly napping, suddenly there came a tapping,<br>  As of some one gently rapping, rapping at my chamber door.<br>  \u201c\u2019Tis some visiter,\u201d I muttered, \u201ctapping at my chamber door\u2014<br>                                     Only this and nothing more.\u201d<br><br>  Ah, distinctly I remember it was in the bleak December,<br>  And each separate dying ember wrought its ghost upon the floor.<br>  Eagerly I wished the morrow;\u2014vainly I had sought to borrow<br>  From my books surcease of sorrow\u2014sorrow for the lost Lenore\u2014<br>  For the rare and radiant maiden whom the angels name Lenore\u2014<br>                                     Nameless here for evermore.<br><br>  And the silken sad uncertain rustling of each purple curtain<br>  Thrilled me\u2014filled me with fantastic terrors never felt before;<br>  So that now, to still the beating of my heart, I stood repeating<br>  \u201c\u2019Tis some visiter entreating entrance at my chamber door\u2014<br>  Some late visiter entreating entrance at my chamber door;<br>                                     This it is and nothing more.\u201d<br><br>  Presently my soul grew stronger; hesitating then no longer,<br>  \u201cSir,\u201d said I, \u201cor Madam, truly your forgiveness I implore;<br>  But the fact is I was napping, and so gently you came rapping,<br>  And so faintly you came tapping, tapping at my chamber door,<br>  That I scarce was sure I heard you\u201d\u2014here I opened wide the door\u2014<br>                                     Darkness there and nothing more.<br><br>  Deep into that darkness peering, long I stood there wondering, fearing,<br>  Doubting, dreaming dreams no mortals ever dared to dream before;<br>  But the silence was unbroken, and the stillness gave no token,<br>  And the only word there spoken was the whispered word, \u201cLenore?\u201d<br>  This I whispered, and an echo murmured back the word, \u201cLenore!\u201d\u2014<br>                                     Merely this and nothing more.<br><br>  Back into the chamber turning, all my soul within me burning,<br>  Soon again I heard a tapping something louder than before.<br>  \u201cSurely,\u201d said I, \u201csurely that is something at my window lattice;<br>  Let me see, then, what thereat is and this mystery explore\u2014<br>  Let my heart be still a moment and this mystery explore;\u2014<br>                                     \u2019Tis the wind and nothing more.\u201d<br><br>  Open here I flung the shutter, when, with many a flirt and flutter,<br>  In there stepped a stately Raven of the saintly days of yore.<br>  Not the least obeisance made he; not a minute stopped or stayed he,<br>  But, with mien of lord or lady, perched above my chamber door\u2014<br>  Perched upon a bust of Pallas just above my chamber door\u2014<br>                                     Perched, and sat, and nothing more.<br><br>  Then the ebony bird beguiling my sad fancy into smiling,<br>  By the grave and stern decorum of the countenance it wore,<br>  \u201cThough thy crest be shorn and shaven, thou,\u201d I said, \u201cart sure no craven,<br>  Ghastly grim and ancient Raven wandering from the Nightly shore\u2014<br>  Tell me what thy lordly name is on the Night\u2019s Plutonian shore!\u201d<br>                                     Quoth the Raven, \u201cNevermore.\u201d<br><br>  Much I marvelled this ungainly fowl to hear discourse so plainly,<br>  Though its answer little meaning\u2014little relevancy bore;<br>  For we cannot help agreeing that no living human being<br>  Ever yet was blessed with seeing bird above his chamber door\u2014<br>  Bird or beast upon the sculptured bust above his chamber door,<br>                                     With such name as \u201cNevermore.\u201d<br><br>  But the Raven, sitting lonely on that placid bust, spoke only<br>  That one word, as if its soul in that one word he did outpour<br>  Nothing farther then he uttered; not a feather then he fluttered\u2014<br>  Till I scarcely more than muttered: \u201cOther friends have flown before\u2014<br>  On the morrow _he_ will leave me, as my Hopes have flown before.\u201d<br>                                     Then the bird said \u201cNevermore.\u201d<br><br>  Startled at the stillness broken by reply so aptly spoken,<br>  \u201cDoubtless,\u201d said I, \u201cwhat it utters is its only stock and store,<br>  Caught from some unhappy master whom unmerciful Disaster<br>  Followed fast and followed faster till his songs one burden bore\u2014<br>  Till the dirges of his Hope that melancholy burden bore<br>                                     Of \u2018Never\u2014nevermore.\u2019\u201d<br><br>  But the Raven still beguiling all my sad soul into smiling,<br>  Straight I wheeled a cushioned seat in front of bird and bust and door;<br>  Then, upon the velvet sinking, I betook myself to linking<br>  Fancy unto fancy, thinking what this ominous bird of yore\u2014<br>  What this grim, ungainly, ghastly, gaunt, and ominous bird of yore<br>                                     Meant in croaking \u201cNevermore.\u201d<br><br>  This I sat engaged in guessing, but no syllable expressing<br>  To the fowl whose fiery eyes now burned into my bosom\u2019s core;<br>  This and more I sat divining, with my head at ease reclining<br>  On the cushion\u2019s velvet lining that the lamp-light gloated o\u2019er,<br>  But whose velvet violet lining with the lamp-light gloating o\u2019er<br>                                     _She_ shall press, ah, nevermore!<br><br>  Then, methought, the air grew denser, perfumed from an unseen censer<br>  Swung by Seraphim whose foot-falls tinkled on the tufted floor.<br>  \u201cWretch,\u201d I cried, \u201cthy God hath lent thee\u2014by these angels he hath sent thee<br>  Respite\u2014respite and nepenthe from thy memories of Lenore!<br>  Quaff, oh quaff this kind nepenthe and forget this lost Lenore!\u201d<br>                                     Quoth the Raven, \u201cNevermore.\u201d<br><br>  \u201cProphet!\u201d said I, \u201cthing of evil!\u2014prophet still, if bird or devil!\u2014<br>  Whether Tempter sent, or whether tempest tossed thee here ashore,<br>  Desolate, yet all undaunted, on this desert land enchanted\u2014<br>  On this home by Horror haunted\u2014tell me truly, I implore\u2014<br>  Is there\u2014_is_ there balm in Gilead?\u2014tell me\u2014tell me, I implore!\u201d<br>                                     Quoth the Raven, \u201cNevermore.\u201d<br><br>  \u201cProphet!\u201d said I, \u201cthing of evil!\u2014prophet still, if bird or devil!<br>  By that Heaven that bends above us\u2014by that God we both adore\u2014<br>  Tell this soul with sorrow laden if, within the distant Aidenn,<br>  It shall clasp a sainted maiden whom the angels name Lenore\u2014<br>  Clasp a rare and radiant maiden whom the angels name Lenore.\u201d<br>                                     Quoth the Raven, \u201cNevermore.\u201d<br><br>  \u201cBe that our sign of parting, bird or fiend!\u201d I shrieked, upstarting\u2014<br>  \u201cGet thee back into the tempest and the Night\u2019s Plutonian shore!<br>  Leave no black plume as a token of that lie thy soul has spoken!<br>  Leave my loneliness unbroken!\u2014quit the bust above my door!<br>  Take thy beak from out my heart, and take thy form from off my door!\u201d<br>                                     Quoth the Raven, \u201cNevermore.\u201d<br><br>  And the Raven, never flitting, still is sitting, still is sitting<br>  On the pallid bust of Pallas just above my chamber door;<br>  And his eyes have all the seeming of a demon\u2019s that is dreaming<br>  And the lamp-light o\u2019er him streaming throws his shadows on the floor;<br>  And my soul from out that shadow that lies floating on the floor<br>                                     Shall be lifted\u2014nevermore!<\/pre>\n\n\n\n<h2>Step 1: Import the necessary libraries <\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>\nimport os\nimport re\nfrom collections import Counter\nfrom wordcloud import WordCloud<\/code><\/pre><\/div>\n\n\n\n<p>The <code>os <\/code>module will be used for setting the <code>path <\/code>of the text file. The <code>re <\/code>module is used for removing punctuations, the  <code>Counter <\/code>module is used for creating a frequency map of the text file, and the <code>wordcloud<\/code> module is used for creating the actual WordCloud Image.<\/p>\n\n\n\n<h2>Step 2: Open the File<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def get_file(filename):\n    with open(filename, encoding=&#39;utf-8&#39;) as fo:\n        content = [i.lower().strip() for i in fo]\n    return &#39; &#39;.join(content)<\/code><\/pre><\/div>\n\n\n\n<h2>Step 3: Clean the Data<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def clean_file(data):\n    data = re.sub(r&#39;[^\\w\\s]&#39;, &#39;&#39;, data)\n    stopwords = (&#39;a&#39;, &#39;an&#39;, &#39;and&#39;, &#39;as&#39;, &#39;at&#39;, &#39;but&#39;, &#39;by&#39;, &#39;from&#39;, &#39;he&#39;, &#39;him&#39;, &#39;i&#39;, &#39;is&#39;, &#39;my&#39;, &#39;of&#39;, &#39;or&#39;,\n                 &#39;on&#39;, &#39;said&#39;, &#39;that&#39;, &#39;the&#39;, &#39;there&#39;, &#39;this&#39;, &#39;to&#39;, &#39;with&#39;)\n    return Counter([word for word in data.split() if word not in stopwords])<\/code><\/pre><\/div>\n\n\n\n<p>We are using the <code>re <\/code>module to remove all punctuations from the text. Next, we remove all non-interesting words as seen in the tuple <code>stopwords <\/code>in the code above. Finally, we use the <code>Counter <\/code>module  to create a <code>frequency map<\/code> of the new list and return it. <\/p>\n\n\n\n<h2>Step 4: Generate the WordCloud <\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def generate_wordcloud(data):\n    return WordCloud(height=800, width=1200).generate_from_frequencies(data)<\/code><\/pre><\/div>\n\n\n\n<p>Now we generate the WordCloud. Here I am creating an WordCloud image that is 800&#215;1200 pixels in dimension. <\/p>\n\n\n\n<h2>Step 5: Save the Image  File<\/h2>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def save_wordcloud(data, filename):\n    data.to_file(os.path.join(filename))\n    print(f&#39;{filename} has been successfully saved.&#39;)<\/code><\/pre><\/div>\n\n\n\n<p>Finally, we save the WordCloud image. Here the <code>os <\/code>module is used to create a complete path.<\/p>\n\n\n\n<h2>The Complete Code<\/h2>\n\n\n\n<p>Now lets take a look at the complete code.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>#!\/usr\/bin\/env python3\n\nimport os\nimport re\nfrom collections import Counter\nfrom wordcloud import WordCloud\n\n\ndef get_file(filename):\n    with open(filename, encoding=&#39;utf-8&#39;) as fo:\n        content = [i.lower().strip() for i in fo]\n    return &#39; &#39;.join(content)\n\n\ndef clean_file(data):\n    data = re.sub(r&#39;[^\\w\\s]&#39;, &#39;&#39;, data)\n    stopwords = (&#39;a&#39;, &#39;an&#39;, &#39;and&#39;, &#39;as&#39;, &#39;at&#39;, &#39;but&#39;, &#39;by&#39;, &#39;from&#39;, &#39;he&#39;, &#39;him&#39;, &#39;i&#39;, &#39;is&#39;, &#39;my&#39;, &#39;of&#39;, &#39;or&#39;,\n                 &#39;on&#39;, &#39;said&#39;, &#39;that&#39;, &#39;the&#39;, &#39;there&#39;, &#39;this&#39;, &#39;to&#39;, &#39;with&#39;)\n    return Counter([word for word in data.split() if word not in stopwords])\n\n\ndef generate_wordcloud(data):\n    return WordCloud(height=800, width=1200).generate_from_frequencies(data)\n\n\ndef save_wordcloud(data, filename):\n    data.to_file(os.path.join(filename))\n    print(f&#39;{filename} has been successfully saved.&#39;)\n\n\ndef main():\n    raven_path = os.path.join(&#39;the_raven.txt&#39;)\n    raven_file = get_file(raven_path)\n\n    process_file = clean_file(raven_file)\n    raven_cloud = generate_wordcloud(process_file)\n    save_wordcloud(raven_cloud, &#39;raven_cloud.jpg&#39;)\n\n\nif __name__ == &#39;__main__&#39;:\n    main()<\/code><\/pre><\/div>\n\n\n\n<h2>Final Results<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"683\" src=\"https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/05\/raven_cloud-1024x683.jpg\" alt=\"The Raven, WordCloud. By Edgar Allen Poe\" class=\"wp-image-145\" srcset=\"https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/05\/raven_cloud-1024x683.jpg 1024w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/05\/raven_cloud-300x200.jpg 300w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/05\/raven_cloud-768x512.jpg 768w, https:\/\/codecrypt76.com\/wp-content\/uploads\/2022\/05\/raven_cloud.jpg 1200w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to create a WordCloud Image using Python and words from the poem &#8220;The Raven&#8221; by Edgar Allan Poe!<\/p>\n","protected":false},"author":1,"featured_media":146,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[36,7,11,35,12],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/141"}],"collection":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/comments?post=141"}],"version-history":[{"count":10,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/141\/revisions"}],"predecessor-version":[{"id":461,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/posts\/141\/revisions\/461"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/media\/146"}],"wp:attachment":[{"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/media?parent=141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/categories?post=141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codecrypt76.com\/index.php\/wp-json\/wp\/v2\/tags?post=141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}