Ejemplo n.º 1
0
 def directory_cloud(self,directory,max_text_size=72,min_text_size=12,expand_width=50,expand_height=50,max_count=100000):
     '''Creates a word cloud using files from a directory.
     The color of the words correspond to the amount of documents the word occurs in.'''
     worddict = assign_fonts(tuplecount(read_dir(directory)),max_text_size,min_text_size,self.exclude_words)
     sorted_worddict = list(reversed(sorted(worddict.keys(), key=lambda x: worddict[x])))
     colordict = assign_colors(dir_freq(directory))
     num_words = 0
     for word in sorted_worddict:
         self.render_word(word,worddict[word],colordict[word])
         if self.width < self.word_size[0]:
             #If the word is bigger than the surface, expand the surface.
             self.expand(self.word_size[0]-self.width,0)
         elif self.height < self.word_size[1]:
             self.expand(0,self.word_size[1]-self.height)
         position = [randint(0,self.width-self.word_size[0]),randint(0,self.height-self.word_size[1])]
         #the initial position is determined
         loopcount = 0
         while self.collides(position,self.word_size):
             if loopcount > max_count:
             #If it can't find a position for the word, create a bigger cloud.
                 self.expand(expand_width,expand_height)      
                 loopcount = 0
             position = [randint(0,self.width-self.word_size[0]),randint(0,self.height-self.word_size[1])]
             loopcount += 1
         self.plot_word(position)
         num_words += 1
Ejemplo n.º 2
0
 def text_cloud(self,text,max_text_size=72,min_text_size=12,expand_width=50,expand_height=50,max_count=100000):
     '''Creates a word cloud using plain text.'''
     worddict = assign_fonts(tuplecount(text),max_text_size,min_text_size,self.exclude_words)
     sorted_worddict = list(reversed(sorted(worddict.keys(), key=lambda x: worddict[x])))
     for word in sorted_worddict:
         self.render_word(word,worddict[word],(randint(0,255),randint(0,255),randint(0,255)))
         if self.width < self.word_size[0]:
             #If the word is bigger than the surface, expand the surface.
             self.expand(self.word_size[0]-self.width,0)
         elif self.height < self.word_size[1]:
             self.expand(0,self.word_size[1]-self.height)
         position = [randint(0,self.width-self.word_size[0]),randint(0,self.height-self.word_size[1])]
         loopcount = 0
         while self.collides(position,self.word_size):
             if loopcount > max_count:
             #If it can't find a position for the word, expand the cloud.
                 self.expand(expand_width,expand_height)
                 loopcount = 0
             position = [randint(0,self.width-self.word_size[0]),randint(0,self.height-self.word_size[1])]
             loopcount += 1
         self.plot_word(position)