DEV Community

Kevin Mack
Kevin Mack

Posted on • Originally published at welldocumentednerd.com on

Cool Nerdy Gift Idea – Word Cloud

The holidays are fast approaching, and this year I had a really cool idea for a gift that turned out well, and I thought I would share it. For the past year and a half, I’ve had this thing going with my wife where every day I’ve sent her a “Reason X, that I love you…” and it’s been a thing of ours that’s been going for a long time (up to 462 at the time of this post).

But what was really cool was this year for our anniversary I decided to take a nerdy approach to making something very sentimental but easy to make. Needless to say, it was very well-received, and I thought I would share.

What I did was used Microsoft Cognitive Services and Power BI to build a Word Cloud based on the key words extracted from the text messages I’ve sent her. Microsoft provides a cognitive service that does text analytics, and if you’re like me you’ve seen sentiment analysis and other bots before. But one of the capabilities, is Key Word Extraction, which is discussed here.

So given this, I wrote a simple python script to pull in all the text messages that I exported to csv, and run them through cognitive services.

from collections import Counter
import json 

key = "..."
endpoint = "..."

run_text_analytics = True
run_summarize = True 

from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential

class KeywordResult():
    def __init__ (self, keyword, count):
        self.keyword = keyword
        self.count = count 

# Authenticate the client using your key and endpoint 
def authenticate_client():
    ta_credential = AzureKeyCredential(key)
    text_analytics_client = TextAnalyticsClient(
            endpoint=endpoint, 
            credential=ta_credential)
    return text_analytics_client

client = authenticate_client()

def key_phrase_extraction(client):

    try:
        if (run_text_analytics == True):
            print("Running Text Analytics")
            with open("./data/reasons.txt") as f:
                lines = f.readlines()

                responses = []
                for i in range(0, len(lines),10):
                    documents = lines[i:i+10]
                    response = client.extract_key_phrases(documents = documents)[0]

                    if not response.is_error:
                        for phrase in response.key_phrases:
                            #print("\t\t", phrase)
                            responses += [phrase]
                    else:
                        print(response.id, response.error)
                # for line in lines:
                # documents = [line]


                with open("./data/output.txt", 'w') as o:
                    for respone_line in responses:
                        o.write(f"{respone_line}\n")
            print("Running Text Analytics - Complete")

        if (run_summarize == True):
            print("Running Summary Statistics")
            print("Getting output values")
            with open("./data/output.txt") as reason_keywords:
                keywords = reason_keywords.readlines()
                keyword_counts = Counter(keywords)
                print("Counts retrieved")

                print("Building Keyword objects")
                keyword_list = []
                for key, value in keyword_counts.items():
                    result = KeywordResult(key,value)
                    keyword_list.append(result)
                print("Keyword objects built")

                print("Writing output files")
                with open("./data/keyword_counts.csv","w") as keyword_count_output:
                    for k in keyword_list:
                        print(f"Key = {k.keyword} Value = {k.count}")
                        print()
                        key_value = k.keyword.replace("\n","")
                        result_line = f"{key_value},{k.count}\n"
                        keyword_count_output.write(result_line)
                print("Finished writing output files")

    except Exception as err:
        print("Encountered exception. {}".format(err))

key_phrase_extraction(client)
Enter fullscreen mode Exit fullscreen mode

Now with the above code, you will need to create a text analytics cognitive service, and then populate the endpoint and the key provided. But the code will take each row of the document and run it through cognitive services (in batches of 10) and then output the results.

From there, you can open up Power BI and point it at the text document provided, and connect the Word Cloud visual, and you’re done. There are great instructions found hereif it helps.

It’s a pretty easy gift that can be really amazing. And Happy Holidays!

Top comments (0)