Python Forum
frequency list from wordcloud
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
frequency list from wordcloud
#1
I have written code to help me generate a WordCloud but my boss has asked me to include a frequency list for my data based on a short description. I am trying to get the numbers behind the wordcloud I have generated (ex. award 1957). Can someone help me with what code I would have to add to generate this in addition to the wordcloud.

My code:
# Python program to generate WordCloud

# importing all necessery modules
from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
import pandas as pd

# Reads 'short_description' file
df = open('PythonTestData.txt')

comment_words = ' '
stopwords = set(STOPWORDS)

# iterate through the csv file
for val in df:

    # typecaste each val to string
    val = str(val)

    # split the value
    tokens = val.split()

    # Converts each token into lowercase
    for i in range(len(tokens)):
        tokens[i] = tokens[i].lower()

    for words in tokens:
        comment_words = comment_words + words + ' '

wordcloud = WordCloud(width=800, height=800,
                      background_color='white',
                      stopwords=stopwords,
                      min_font_size=10).generate(comment_words)



# plot the WordCloud image
plt.figure(figsize=(8, 8), facecolor=None)
plt.imshow(wordcloud)
plt.axis("off")
plt.tight_layout(pad=0)

plt.show()
Reply
#2
From the documentation, it appears the WorldCloud object has a words_ attribute that has the frequencies.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Apologies. I will follow the procedures. New to this site. How would I call the attribute in my code to generate that? I'm very new to Python.
Reply
#4
That would be in wordcloud.words_. It appears to be a dictionary of word keys and float values. I would pull out the value, key tuples in a list comprehension or loop, sort that (probably with reverse = True), and then print those out in a loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Could you explain that as if I've never seen Python before (still pretty much at that stage).
Reply
#6
Could you try to write some code? If it doesn't work I can help you fix it, but I'm not big on just writing code for people.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Date_Range Frequency List sobrio1 1 3,392 Dec-03-2017, 09:54 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020