def generate_from_frequencies(self, frequencies, max_font_size=None):  
<EOL>frequencies = sorted(frequencies.items(), key=itemgetter(<NUM_LIT:1>), reverse=True)<EOL>if len(frequencies) <= <NUM_LIT:0>:<EOL><INDENT>raise ValueError("<STR_LIT>"<EOL>"<STR_LIT>" % len(frequencies))<EOL><DEDENT>frequencies = frequencies[:self.max_words]<EOL>max_frequency = float(frequencies[<NUM_LIT:0>][<NUM_LIT:1>])<EOL>frequencies = [(word, freq / max_frequency)<EOL>for word, freq in frequencies]<EOL>if self.random_state is not None:<EOL><INDENT>random_state = self.random_state<EOL><DEDENT>else:<EOL><INDENT>random_state = Random()<EOL><DEDENT>if self.mask is not None:<EOL><INDENT>boolean_mask = self._get_bolean_mask(self.mask)<EOL>width = self.mask.shape[<NUM_LIT:1>]<EOL>height = self.mask.shape[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>boolean_mask = None<EOL>height, width = self.height, self.width<EOL><DEDENT>occupancy = IntegralOccupancyMap(height, width, boolean_mask)<EOL>img_grey = Image.new("<STR_LIT:L>", (width, height))<EOL>draw = ImageDraw.Draw(img_grey)<EOL>img_array = np.asarray(img_grey)<EOL>font_sizes, positions, orientations, colors = [], [], [], []<EOL>last_freq = <NUM_LIT:1.><EOL>if max_font_size is None:<EOL><INDENT>max_font_size = self.max_font_size<EOL><DEDENT>if max_font_size is None:<EOL><INDENT>if len(frequencies) == <NUM_LIT:1>:<EOL><INDENT>font_size = self.height<EOL><DEDENT>else:<EOL><INDENT>self.generate_from_frequencies(dict(frequencies[:<NUM_LIT:2>]),<EOL>max_font_size=self.height)<EOL>sizes = [x[<NUM_LIT:1>] for x in self.layout_]<EOL>try:<EOL><INDENT>font_size = int(<NUM_LIT:2> * sizes[<NUM_LIT:0>] * sizes[<NUM_LIT:1>]<EOL>/ (sizes[<NUM_LIT:0>] + sizes[<NUM_LIT:1>]))<EOL><DEDENT>except IndexError:<EOL><INDENT>try:<EOL><INDENT>font_size = sizes[<NUM_LIT:0>]<EOL><DEDENT>except IndexError:<EOL><INDENT>raise ValueError(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>"<STR_LIT>")<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>font_size = max_font_size<EOL><DEDENT>self.words_ = dict(frequencies)<EOL>if self.repeat and len(frequencies) < self.max_words:<EOL><INDENT>times_extend = int(np.ceil(self.max_words / len(frequencies))) - <NUM_LIT:1><EOL>frequencies_org = list(frequencies)<EOL>downweight = frequencies[-<NUM_LIT:1>][<NUM_LIT:1>]<EOL>for i in range(times_extend):<EOL><INDENT>frequencies.extend([(word, freq * downweight ** (i + <NUM_LIT:1>))<EOL>for word, freq in frequencies_org])<EOL><DEDENT><DEDENT>for word, freq in frequencies:<EOL><INDENT>rs = self.relative_scaling<EOL>if rs != <NUM_LIT:0>:<EOL><INDENT>font_size = int(round((rs * (freq / float(last_freq))<EOL>+ (<NUM_LIT:1> - rs)) * font_size))<EOL><DEDENT>if random_state.random() < self.prefer_horizontal:<EOL><INDENT>orientation = None<EOL><DEDENT>else:<EOL><INDENT>orientation = Image.ROTATE_90<EOL><DEDENT>tried_other_orientation = False<EOL>while True:<EOL><INDENT>font = ImageFont.truetype(self.font_path, font_size)<EOL>transposed_font = ImageFont.TransposedFont(<EOL>font, orientation=orientation)<EOL>box_size = draw.textsize(word, font=transposed_font)<EOL>result = occupancy.sample_position(box_size[<NUM_LIT:1>] + self.margin,<EOL>box_size[<NUM_LIT:0>] + self.margin,<EOL>random_state)<EOL>if result is not None or font_size < self.min_font_size:<EOL><INDENT>break<EOL><DEDENT>if not tried_other_orientation and self.prefer_horizontal < <NUM_LIT:1>:<EOL><INDENT>orientation = (Image.ROTATE_90 if orientation is None else<EOL>Image.ROTATE_90)<EOL>tried_other_orientation = True<EOL><DEDENT>else:<EOL><INDENT>font_size -= self.font_step<EOL>orientation = None<EOL><DEDENT><DEDENT>if font_size < self.min_font_size:<EOL><INDENT>break<EOL><DEDENT>x, y = np.array(result) + self.margin // <NUM_LIT:2><EOL>draw.text((y, x), word, fill="<STR_LIT>", font=transposed_font)<EOL>positions.append((x, y))<EOL>orientations.append(orientation)<EOL>font_sizes.append(font_size)<EOL>colors.append(self.color_func(word, font_size=font_size,<EOL>position=(x, y),<EOL>orientation=orientation,<EOL>random_state=random_state,<EOL>font_path=self.font_path))<EOL>if self.mask is None:<EOL><INDENT>img_array = np.asarray(img_grey)<EOL><DEDENT>else:<EOL><INDENT>img_array = np.asarray(img_grey) + boolean_mask<EOL><DEDENT>occupancy.update(img_array, x, y)<EOL>last_freq = freq<EOL><DEDENT>self.layout_ = list(zip(frequencies, font_sizes, positions,<EOL>orientations, colors))<EOL>return self<EOL>
Create a word_cloud from words and frequencies.

        Parameters
        ----------
        frequencies : dict from string to float
            A contains words and associated frequency.

        max_font_size : int
            Use this font-size instead of self.max_font_size

        Returns
        -------
        self