<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Xiaonan Zhang on Medium]]></title>
        <description><![CDATA[Stories by Xiaonan Zhang on Medium]]></description>
        <link>https://medium.com/@zxnmail?source=rss-19314fe2cced------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*g_0Ss3tuhM09ZUfT</url>
            <title>Stories by Xiaonan Zhang on Medium</title>
            <link>https://medium.com/@zxnmail?source=rss-19314fe2cced------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 21:29:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zxnmail/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Visualizing Your Data from MongoDB to Word Cloud with Python 3]]></title>
            <link>https://medium.com/analytics-vidhya/visualizing-your-data-from-mongodb-to-word-cloud-with-python-3-3e9c4b4f2743?source=rss-19314fe2cced------2</link>
            <guid isPermaLink="false">https://medium.com/p/3e9c4b4f2743</guid>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[naturallanguageprocessing]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[word-cloud]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[Xiaonan Zhang]]></dc:creator>
            <pubDate>Fri, 07 Aug 2020 12:26:17 GMT</pubDate>
            <atom:updated>2020-08-12T12:32:53.979Z</atom:updated>
            <content:encoded><![CDATA[<ul><li><a href="#0d55">Technology Stack</a></li><li><a href="#e758">Connecting to MongoDB</a></li><li><a href="#177b">Tokenizing Words</a></li><li><a href="#80da">Generating Wordcloud Images</a></li><li><a href="#f290">Translate to Another Language</a></li></ul><p>Word clouds have become more and more popular nowadays as it is a straightforward and efficient way to communicate data. The main advantage is that it helps to simplify the process of understanding complex diagrams which usually need some prerequisite knowledge to assist with digestion.</p><p>One rule represents the word cloud’s simplicity — the text size, i.e. the bigger the text the more important it is. This obvious feature assures that people from a variety of backgrounds understand the content easily.</p><p>In this article, we introduce an example of converting data collected from a toddler’s daily activities into a word cloud. The word cloud’s design helps to visualize the toddler’s activities into a way that parents and carers can easily see and then utilize to foster the way they educate the children.</p><p>Before diving into the technical details, let us check out the result first.</p><figure><img alt="A sample of Wordcloud showing nursery activities for a 3 years old toddler" src="https://cdn-images-1.medium.com/max/1024/1*ScME2rZkDsDLzFa-4IyLbQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*weFAMEASePthflRjfcSJ0w.png" /></figure><p>The above word clouds are based on teachers’ comments for a kid throughout a fixed period of time. The left cloud is the original language used in comments and the right word cloud is its Chinese translation. The words in bigger font size than others highlight the kid’s focus and sentiment communication with others.</p><h3>Technology Stack</h3><p><strong>MongoDB</strong>: Store collected data<br><strong>Python 3</strong>: Process data and generate word cloud images<br><strong>Libraries</strong>: <a href="https://spacy.io/">spaCy</a>, <a href="https://numpy.org/">Numpy</a>, <a href="https://amueller.github.io/word_cloud/">wordcloud</a>, <a href="https://pypi.org/project/googletrans/">googletrans</a></p><h3>Connecting to MongoDB</h3><p><a href="https://www.mongodb.com/cloud/atlas">MongoDB Atlas</a>, a managed MongoDB cloud service, is chosen because the M0 Sandbox is free of charge and it saves time for deploying and maintaining a local database.</p><p>The connection details can be put into environment variables and read from Python as follows. Using environment variables is good practice in case if the application needs to be deployed to clouds as a service with Docker and Kubernetes.</p><pre>MONGO_USERNAME = os.getenv(&#39;MONGO_USERNAME&#39;)<br>MONGO_PASSWORD = os.getenv(&#39;MONGO_PASSWORD&#39;)<br>MONGO_URL = os.getenv(&#39;MONGO_URL&#39;)</pre><p>Then the MongoDB client needs to be initialized. All the MongoDB related code can be put into a separate Python file for organization and potential future reuse purposes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1bb1e08e70c3a87d4683d9a5da853646/href">https://medium.com/media/1bb1e08e70c3a87d4683d9a5da853646/href</a></iframe><p>In this example the db will be initialized from the command line so it is left empty here. A simple function for reading the collections is added as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2c347deeb1ee9762e64ae93a25be5fa/href">https://medium.com/media/a2c347deeb1ee9762e64ae93a25be5fa/href</a></iframe><p>Retrieve the data from the main script.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/627dda3e43cf46488f127ed725d19b3c/href">https://medium.com/media/627dda3e43cf46488f127ed725d19b3c/href</a></iframe><p>The collectionis the MongoDB collection name and the data_entry is the key name of the values. Here is a piece of sample data. The data_entry used here is the detail below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c902d49b1bd1c6748060e788682a14e2/href">https://medium.com/media/c902d49b1bd1c6748060e788682a14e2/href</a></iframe><h3>Tokenizing Words</h3><p>In the previous section, the paragraphs are loaded into an array. Streaming can be considered if the data size is huge.</p><p>The paragraphs need to be broken down into words. It sounds like a straightforward job however lots of effort is needed to ensure the accuracy of the final result. Processing natural language is a challenge to computers as they can never understand the words directly.</p><p>Natural Language Processing and Machine Learning are big topics. As this article focuses on the word cloud, only a few steps are considered to process these texts.</p><ol><li>Discard unwanted symbols</li><li>Remove the organization and person names</li><li>Normalize words (stemming)</li><li>Filter stop-words</li></ol><p><a href="https://spacy.io/">spaCy</a> is a powerful tool for Natural Language Processing. We use it for tagging names and stemming which are just the tip of the iceberg.</p><p>The first step is to initialize spaCy and load data without unwanted symbols. The data is loaded to an array named content and the spaCy is initialized by en_core_web_sm.load()</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f5c1ff259c3690094c4ad7cd2688dc65/href">https://medium.com/media/f5c1ff259c3690094c4ad7cd2688dc65/href</a></iframe><p>Secondly, we use spaCy to filter organization and person names. They are classified with label_ equals to PERSON and ORG. A complete list of the named entities can be found <a href="https://spacy.io/api/annotation#named-entities">here</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72fa8dcc587f2e3ff572f5572edd7174/href">https://medium.com/media/72fa8dcc587f2e3ff572f5572edd7174/href</a></iframe><p>Then we do stemming, punctuation removal, and stop word removal in a loop. Extraction of the base form of a token can be done by getting the lemma_ attribute.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bba67d136fecd407a80c1946e8758394/href">https://medium.com/media/bba67d136fecd407a80c1946e8758394/href</a></iframe><p>Based on the study of the data we decide to also have a customized list of stop words. A file named stop-words.txt is introduced for the script to load dynamically.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c763ea643795eb9f880e569dc199489/href">https://medium.com/media/7c763ea643795eb9f880e569dc199489/href</a></iframe><p>Here is a sample of the tokenized words.</p><pre>[&#39;play&#39;, &#39;dough&#39;, &#39;disco&#39;, &#39;time&#39;, &#39;construction&#39;, &#39;activity&#39;, &#39;afternoon&#39;, &#39;engaged&#39;, &#39;stamp&#39;, &#39;pat&#39;, &#39;play&#39;, &#39;piano&#39;, &#39;play&#39;, &#39;dough&#39;, &#39;piece&#39;, &#39;spend&#39;, &#39;lot&#39;, &#39;morning&#39;, &#39;explore&#39;, &#39;painting&#39;, &#39;equipment&#39;, &#39;stamp&#39;, &#39;roll&#39;, &#39;create&#39;, &#39;different&#39;, &#39;print&#39;, &#39;friend&#39;, &#39;time&#39;, &#39;decorate&#39;, &#39;carefully&#39;, &#39;gingerbread&#39;, &#39;biscuit&#39;, &#39;colourful&#39;, &#39;flake&#39;, &#39;explore&#39;, &#39;conker&#39;, &#39;paint&#39;, &#39;roll&#39;, &#39;shake&#39;, &#39;box&#39;, &#39;show&#39;, &#39;able&#39;, &#39;independently&#39;, &#39;find&#39;, &#39;way&#39;, &#39;suit&#39;, &#39;conker&#39;, &#39;create&#39;, &#39;picture&#39;, &#39;watch&#39;, &#39;closely&#39;, &#39;see&#39;, &#39;different&#39;, &#39;mark&#39;, &#39;conker&#39;, &#39;make&#39;, &#39;move&#39;, &#39;keen&#39;, &#39;help&#39;, &#39;teacher&#39;, &#39;create&#39;, &#39;gloop&#39;, &#39;morning&#39;, &#39;spoon&#39;, &#39;mix&#39;, &#39;water&#39;, &#39;corn&#39;, &#39;flour&#39;]</pre><h3>Generating Wordcloud Images</h3><p>As the data is collected and processed, the next step is to get it visualized as word cloud images. The <a href="https://numpy.org/">Numpy</a> and <a href="https://amueller.github.io/word_cloud/">wordcloud</a> make this step straightforward and simple.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80f5df11bf9db2e6ea737b35bdbe3514/href">https://medium.com/media/80f5df11bf9db2e6ea737b35bdbe3514/href</a></iframe><p>Similar to other online word cloud sites, a mask image can be chosen as the base image. Then quite a few parameters such as font_path and background_color can be tweaked for producing an image to suit your specific needs. Examples can be found on the official wordcloud library site <a href="https://amueller.github.io/word_cloud/auto_examples/index.html">here</a>.</p><h3>Translate to Another Language</h3><p>As the data may be from various sources and the word cloud audience may be from different countries, it may be necessary to translate the words into another language for the best understanding.</p><p><a href="https://pypi.org/project/googletrans/">googletrans</a> is an unofficial Python library that implemented Google Translate. We initialize the library in our code and send a group of words at one time to Google Translate.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ace2e2b3d57232039fe85a206245acd/href">https://medium.com/media/6ace2e2b3d57232039fe85a206245acd/href</a></iframe><p>It seems simple to do the translation however there are some concerns that need to be taken care of.</p><ol><li>The library is not from Google so the stability is not guaranteed.</li><li>Maximum text size in a single submission is 15k</li><li>Words may have different translations in various contexts</li><li>Some translations need to be corrected</li></ol><p>If stability is a concern, the official <a href="https://cloud.google.com/translate/docs">Google Translate API</a> is recommended.</p><p>Based on our tokenized data, the limit of size can be simply done by segmenting the array for multiple submissions.</p><p>The most difficult issue is the 3rd point above. If the translation of the words needs to be based on context, other complex processing needs to be in place.</p><p>In this article, we take a simple approach to translate words without contexts. As the remote Google Translate API call is expensive in timing, we cannot submit one word at a time. The easiest and most straightforward solution is to find a good separator and add it to the list of words which makes the translation neutral to the contexts. In the below code snippet the tokens are submitted in a group of 500 with &gt; as separators.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2475dce21c095512ece3ba438e58b49/href">https://medium.com/media/d2475dce21c095512ece3ba438e58b49/href</a></iframe><p>The final step is to check your translations and correct those ones which have irrelevant meanings to the overall context or the translation is simply not good enough. A new translation mapping file translation-map-zh-cn.csv is used for correction as follows. Words on the left side are to be replaced by the words on the right side.</p><pre>股票,分享<br>铅,带头<br>匙,勺子</pre><p>The code for reading the file and making the correction is as follows.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b6792671a0a27a17bb5f8490333beac/href">https://medium.com/media/7b6792671a0a27a17bb5f8490333beac/href</a></iframe><p>In conclusion, 4 topics are covered in this article. Connecting to a remote MongoDB, tokenizing words, generating word cloud images, and finally translating the words to other languages. The goal is to produce a word cloud with the most meaningful words. Depending on the collected data and the purpose of the word cloud, an in-depth study of the raw data is crucial to draw your own plan of tokenizing words.</p><p><a href="https://gitlab.com/conanzxn/mongodb-wordcloud">Here is the GitLab link to this showcase project.</a> Please see the <a href="https://gitlab.com/conanzxn/mongodb-wordcloud/-/blob/master/README.md">README</a> for how to generate a word cloud for your own data. PRs are welcome!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3e9c4b4f2743" width="1" height="1" alt=""><hr><p><a href="https://medium.com/analytics-vidhya/visualizing-your-data-from-mongodb-to-word-cloud-with-python-3-3e9c4b4f2743">Visualizing Your Data from MongoDB to Word Cloud with Python 3</a> was originally published in <a href="https://medium.com/analytics-vidhya">Analytics Vidhya</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>