In [1]:
import os
os.getcwd()
os.chdir("F:\\")

from wordcloud import WordCloud, STOPWORDS
import matplotlib.pyplot as plt
import pandas as pd


data=pd.read_csv("dataset.csv")
data0=data['motive']
data1=data0.dropna()

words=''
for e in data1:
    words+=e

    
    
    stopwords = set(STOPWORDS)
stopwords.add("attack")
stopwords.add("attacks")
stopwords.add("motive")
stopwords.add("unknown")
stopwords.add("specific")
stopwords.add("however")

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
from os import path
from PIL import Image
import numpy as np

d = path.dirname('.')

mask = np.array(Image.open(path.join(d, "mask1.png")))

In [18]:
plt.imshow(wordcloud)

plt.axis('off')
plt.show()

In [20]:
wordcloud = WordCloud(#font_path='/Users/kunal/Library/Fonts/sans-serif.ttf',
                  #        stopwords=STOPWORDS, background_color='white'
    mask=mask,
                         stopwords=stopwords,max_font_size=40,scale=1,max_words=1000
                         
                        )
wordcloud.generate(words)


Out[20]:
<wordcloud.wordcloud.WordCloud at 0x69d6750>

In [ ]:
import random
def grey_color_func(word, font_size, position, orientation, random_state=None, **kwargs):
    return "hsl(0, 0%%, %d%%)" % random.randint(60, 100)


image_colors = ImageColorGenerator(mask)
plt.imshow(wordcloud.recolor(color_func=grey_color_func, random_state=3))

#plt.imshow(wordcloud.recolor(color_func=image_colors))
plt.axis("off")
plt.figure()
plt.show()

In [12]:


In [6]:
color=np.array(Image.open(path.join(d, "mask1.png")))
image_colors=ImageColorGenerator(color)
plt.imshow(wordcloud.recolor(color_func=image_colors))
plt.axis("off")
plt.figure()


Out[6]:
<matplotlib.figure.Figure at 0x9d2ba90>