<jupyter_start><jupyter_text>Kobe Bryant Tribute Word Cloud @Author: <NAME><jupyter_code>import pandas as pd 
import numpy as np
from os import path, getcwd
import matplotlib.pyplot as plt 
from wordcloud import WordCloud, ImageColorGenerator, STOPWORDS
from PIL import Image
from twitterscraper import query_tweets 
import datetime as dt
begin_date = dt.date(2020,1,26)
end_date = dt.date(2020,1,27)
lang = 'english'
limit = 10000
tweets = query_tweets("#Kobe", begindate = begin_date, enddate=end_date, limit=limit, lang=lang)
df = pd.DataFrame(i.__dict__ for i in tweets)
df.head()
df.shape
df.to_csv('tweets.csv')
user_tweets = pd.read_csv('tweets.csv')
user_tweets.shape
tweet_words = " ".join(user_tweets.text.drop_duplicates())
#removing punchuation
from nltk.tokenize import RegexpTokenizer
tokenizer = RegexpTokenizer(r'\w+')
words = tokenizer.tokenize(tweet_words)
import nltk
nltk.download('stopwords')
nltk.download('punkt')
words_1 = ' '.join(words)
#removing stop words
from nltk.corpus import stopwords
from nltk import word_tokenize
stop_words = set(stopwords.words('english'))
text = word_tokenize(words_1)
text=[x.lower() for x in text]
unwanted = ['kobe','bryant','kobebryant','https','helicopter', 'crash','today','rip','accident','daughter','death','black','pic','com']
text = " ".join([w for w in text if w not in unwanted])
no_digits=[]
for i in text:
    if not i.isdigit():
        no_digits.append(i)
result = ''.join(no_digits)
import nltk
nltk.download('words')
words = set(nltk.corpus.words.words())
sent = result
final =" ".join(w for w in nltk.wordpunct_tokenize(sent) \
         if w.lower() in words or not w.isalpha())
final
import os
p = os.getcwd()
mask_logo = np.array(Image.open(path.join(p,'kobe.png')))
stopwords = set(STOPWORDS)
stopwords.add('www')
stopwords.add('instagram')
stopwords.add('twitter')
stopwords.add('igshid')
stopwords.add('status')
wc = WordCloud(background_color="white", max_words=2000, mask=mask_logo,stopwords=stopwords)
# generate word cloud
wc.generate(text)
# store to file
# wc.to_file(path.join(p, "kobe.png"))
# show
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
image_colors = ImageColorGenerator(mask_logo)
wc.recolor(color_func=image_colors).to_file('kobe.png')
plt.figure(figsize=[10,10])
plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis("off")
plt.show()<jupyter_output><empty_output>