13
votes

In the word cloud I have repetitive words and I do not understand why they are not counted together and are shown then as one word.

from wordcloud import WordCloud
word_string = 'oh oh oh oh oh oh verse wrote book stand title book would life superman thats make feel count privilege love ideal honored know feel see everyday things things say rock baby truth rock love rock rock everything need rock baby rock wanna kiss ya feel ya please ya right wanna touch ya love ya baby night reward ya things rock love rock love rock oh oh oh verse try count ways make smile id run fingers run timeless things talk sugar keeps going make wanna keep lovin strong make wanna try best give want need give whole heart little piece minimum talking everything single wish talking every dream rock baby truth rock love rock rock everything need rock baby rock wanna kiss ya feel ya please ya right wanna touch ya love ya baby night reward ya things rock love rock wanna rock bridge theres options dont want theyre worth time cause oh thank like us fine rock sand smile cry joy pain truth lies matter know count oh oh oh oh oh oh rock baby truth rock love rock rock everything need rock baby rock wanna kiss ya feel ya please ya right wanna touch ya love ya baby night reward ya things rock love rock love rock oh oh oh oh oh oh wanna kiss ya feel ya please ya right wanna touch ya love ya baby night reward ya things rock love rock wanna rock party people people party popping sitting around see looking looking see look started lets hook little one one come give stuff let freshin ruff lets go lets hook start wont stop baby baby dont stop come give stuff lets go black culture black culture black culture black culture party people people party popping sitting around see looking looking see look started lets hook come one give stuff let freshin little one one ruff lets go lets hook start wont stop baby baby dont stop come give stuff lets go black culture black culture black culture black culture lets hook come give stuff let freshin little one one ruff lets go lets hook start wont stop baby baby dont stop come give stuff lets go lets hook come give stuff let freshin little one one ruff lets go lets hook start wont stop baby baby dont stop come give stuff lets go black culture black culture black culture black culture black culture black culture black culture black culture'
wordcloud = WordCloud(background_color="white",
                          width=1200, height=1000,
                          stopwords=STOPWORDS
                         ).generate(word_string)
plt.imshow(wordcloud)

As you see words like love, oh, rock, black, culture appear several times and it seems that they are not counted together. What am I doing wrong?

enter image description here

2
So you just want to remove duplicates from the input string? Like this?Olian04
I do not want to remove duplicates. The idea if the word cloud is to see which words are in the text and what is the occurrence of them. Word cloud will show the most frequent words in bigger size, whereas the less frequent words will be written with smaller size. So you see that the word "ya" is pretty frequent. But I do not understand why it shows repetitive words.Alina
aha, then i cant help you. Good luckOlian04

2 Answers

10
votes

If you look at wordcloud.words_ you will see the frequency table includes some two-word phrases like 'oh oh', 'hook start', 'lets go', 'lets hook'.

You would need to dig into the code behind .process_text() to see exactly why it does this.

As a work-around you could split word_string yourself to build a word-frequency table, then use .generate_from_frequencies() to create the image.

59
votes

That is a feature called 'collocations' in the word_cloud project. You can turn it off by setting collocations=False, like this:

    wordcloud = WordCloud(collocations=False).generate(word_string)

This will get rid of words that are frequently grouped together in your text. It will get rid of some things you probably don't like, for instance, "oh oh" and it will get rid of some others that you may like, for instance, "black culture"