Skip to content

Instantly share code, notes, and snippets.

@kururu-abdo
Last active July 26, 2023 09:48
Show Gist options
  • Select an option

  • Save kururu-abdo/bf36ade2435872a013596345e956021f to your computer and use it in GitHub Desktop.

Select an option

Save kururu-abdo/bf36ade2435872a013596345e956021f to your computer and use it in GitHub Desktop.
from wordcloud import WordCloud, STOPWORDS
print ('Wordcloud imported!')
import urllib
# # open the file and read it into a variable alice_novel
alice_novel = urllib.request.urlopen('https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/Data%20Files/alice_novel.txt').read().decode("utf-8")
stopwords = set(STOPWORDS)
# instantiate a word cloud object
alice_wc = WordCloud()
# generate the word cloud
alice_wc.generate(alice_novel)
# display the word cloud
plt.imshow(alice_wc, interpolation='bilinear')
plt.axis('off')
plt.show()
#save mask to alice_mask
alice_mask = np.array(Image.open(urllib.request.urlopen('https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-DV0101EN-SkillsNetwork/labs/Module%204/images/alice_mask.png')))
fig = plt.figure(figsize=(14, 18))
plt.imshow(alice_mask, cmap=plt.cm.gray, interpolation='bilinear')
plt.axis('off')
plt.show()
# instantiate a word cloud object
alice_wc = WordCloud(background_color='white', max_words=2000, mask=alice_mask, stopwords=stopwords)
# generate the word cloud
alice_wc.generate(alice_novel)
# display the word cloud
fig = plt.figure(figsize=(14, 18))
plt.imshow(alice_wc, interpolation='bilinear')
plt.axis('off')
plt.show()
#from data frame
total_immigration
max_words = 90
word_string = ''
for country in df_can.index.values:
# check if country's name is a single-word name
if country.count(" ") == 0:
repeat_num_times = int(df_can.loc[country, 'Total'] / total_immigration * max_words)
word_string = word_string + ((country + ' ') * repeat_num_times)
# display the generated text
word_string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment