15 May 2019

And now for a little Cthulhu

I decided to have a little play with word clouds...

I found a nice wordcloud library (https://github.com/amueller/word_cloud), and a complete works of Lovecraft. What could be better :)


I took some liberties (i did some text replacing), so this is a biased wordcloud.

The code is mostly copied from the examples for the library itself.

 from wordcloud import WordCloud  
 import matplotlib.pyplot as plt  
 from PIL import Image  
 import numpy as np  
  
 sFile = 'The Complete Works of H.P. Lovecraft.txt'
 iMap = 'Cthulhu.jpg'  

 lDoc = []  
 with open(sFile,'r') as f:  
   sDoc = f.readlines()  
 for s in sDoc:  
   if s.strip() != '':  
     lDoc.append(s.lower())  
 sDoc = []  
 text = ' '.join(lDoc)  
 for l in lReplace:  
   text = text.replace(l[0],l[1])  
 mask = np.array(Image.open(iMap))  
 wc = WordCloud(background_color="white", max_words=2000, mask=mask,  
         contour_width=3, contour_color='steelblue')  
 # generate word cloud  
 wc.generate(text)  
 # store to file  
 wc.to_file("cthulhu.png")  
 # show  
 plt.imshow(wc, interpolation='bilinear')  
 plt.axis("off")  
 plt.figure()  
 plt.imshow(mask, cmap=plt.cm.gray, interpolation='bilinear')  
 plt.axis("off")  
 plt.show()  

No comments:

Post a Comment

And now for a little Cthulhu

I decided to have a little play with word clouds... I found a nice wordcloud library ( https://github.com/amueller/word_cloud ), and a com...