<commit_before># Author: <NAME> <<EMAIL>>
# (c) 2012
# Modified by: <NAME> <<EMAIL>>
#
# License: MIT

from random import Random
import os
import re
import numpy as np
from operator import itemgetter

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from query_integral_image import query_integral_image

item1 = itemgetter(1)

FONT_PATH = "/usr/share/fonts/truetype/droid/DroidSansMono.ttf"
STOPWORDS = set([x.strip() for x in open(os.path.join(os.path.dirname(__file__),
                                                      'stopwords')).read().split('\n')])


def random_color_func(word, font_size, position, orientation, random_state=None):
    if random_state is None:
        random_state = Random()
    return "hsl(%d, 80%%, 50%%)" % random_state.randint(0, 255)


class WordCloud(object):
    """Word cloud object for generating and drawing.

    Parameters
    ----------
    font_path : string
        Font path to the font that will be used (OTF or TTF).
        Defaults to DroidSansMono path, but you might not have it.

    width : int (default=400)
        Width of the canvas.

    height : int (default=200)
        Height of the canvas.

    ranks_only : boolean (default=False)
        Only use the rank of the words, not the actual counts.

    prefer_horizontal : float (default=0.90)
        The ratio of times to try horizontal fitting as opposed to vertical.

    mask : nd-array or None (default=None)
        If not None, gives a binary mask on where to draw words. All zero
        entries will be considered "free" to draw on, while all non-zero
        entries will be deemed occupied. If mask is not None, width and height will be
        ignored and the shape of mask will be used instead.

    max_words : number (default=200)
        The maximum number of words.

    stopwords : set of strings
        The words that will be eliminated.

    background_color : color value (default="black")
        Background color for the word cloud image.

    Attributes
    ----------
    words_ : list of tuples (string, float)
        Word tokens with associated frequency.

    layout_ : list of tuples (string, int, (int, int), int, color))
        Encodes the fitted word cloud. Encodes for each word the string, font
        size, position, orientation and color.
    """

    def __init__(self, font_path=None, width=400, height=200, margin=5,
                 ranks_only=False, prefer_horizontal=0.9, mask=None, scale=1,
                 color_func=random_color_func, max_words=200, stopwords=None,
                 random_state=None, background_color='black'):
        if stopwords is None:
            stopwords = STOPWORDS
        if font_path is None:
            font_path = FONT_PATH
        self.font_path = font_path
        self.width = width
        self.height = height
        self.margin = margin
        self.ranks_only = ranks_only
        self.prefer_horizontal = prefer_horizontal
        self.mask = mask
        self.scale = scale
        self.color_func = color_func
        self.max_words = max_words
        self.stopwords = stopwords
        if isinstance(random_state, int):
            random_state = Random(random_state)
        self.random_state = random_state
        self.background_color = background_color

    def _fit_words(self, words):
        """Generate the positions for words.

        Parameters
        ----------
        words : array of tuples
            A tuple contains the word and its frequency.

        Returns
        -------
        layout_ : list of tuples (string, int, (int, int), int, color))
            Encodes the fitted word cloud. Encodes for each word the string, font
            size, position, orientation and color.

        Notes
        -----
        Larger canvases with make the code significantly slower. If you need a large
        word cloud, run this function with a lower canvas size, and draw it with a
        larger scale.

        In the current form it actually just uses the rank of the counts, i.e. the
        relative differences don't matter. Play with setting the font_size in the
        main loop for different styles.
        """
        if self.random_state is not None:
            random_state = self.random_state
        else:
            random_state = Random()

        if len(words) <= 0:
            print("We need at least 1 word to plot a word cloud, got %d."
                  % len(words))

        if self.mask is not None:
            width = self.mask.shape[1]
            height = self.mask.shape[0]
            # the order of the cumsum's is important for speed ?!
            integral = np.cumsum(np.cumsum(self.mask, axis=1), axis=0).astype(np.uint32)
        else:
            height, width = self.height, self.width
            integral = np.zeros((height, width), dtype=np.uint32)

        # create image
        img_grey = Image.new("L", (width, height))
        draw = ImageDraw.Draw(img_grey)
        img_array = np.asarray(img_grey)
        font_sizes, positions, orientations, colors = [], [], [], []

        # intitiallize font size "large enough"
        font_size = height

        # start drawing grey image
        for word, count in words:
            # alternative way to set the font size
            if not self.ranks_only:
                font_size = min(font_size, int(100 * np.log(count + 100)))
            while True:
                # try to find a position
                font = ImageFont.truetype(self.font_path, font_size)
                # transpose font optionally<commit_msg>add max font size argument<commit_after># Author: <NAME> <<EMAIL>>
# (c) 2012
# Modified by: <NAME> <<EMAIL>>
#
# License: MIT

from random import Random
import os
import re
import numpy as np
from operator import itemgetter

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
from query_integral_image import query_integral_image

item1 = itemgetter(1)

FONT_PATH = "/usr/share/fonts/truetype/droid/DroidSansMono.ttf"
STOPWORDS = set([x.strip() for x in open(os.path.join(os.path.dirname(__file__),
                                                      'stopwords')).read().split('\n')])


def random_color_func(word, font_size, position, orientation, random_state=None):
    if random_state is None:
        random_state = Random()
    return "hsl(%d, 80%%, 50%%)" % random_state.randint(0, 255)


class WordCloud(object):
    """Word cloud object for generating and drawing.

    Parameters
    ----------
    font_path : string
        Font path to the font that will be used (OTF or TTF).
        Defaults to DroidSansMono path, but you might not have it.

    width : int (default=400)
        Width of the canvas.

    height : int (default=200)
        Height of the canvas.

    ranks_only : boolean (default=False)
        Only use the rank of the words, not the actual counts.

    prefer_horizontal : float (default=0.90)
        The ratio of times to try horizontal fitting as opposed to vertical.

    mask : nd-array or None (default=None)
        If not None, gives a binary mask on where to draw words. All zero
        entries will be considered "free" to draw on, while all non-zero
        entries will be deemed occupied. If mask is not None, width and height will be
        ignored and the shape of mask will be used instead.

    max_words : number (default=200)
        The maximum number of words.

    stopwords : set of strings
        The words that will be eliminated.

    background_color : color value (default="black")
        Background color for the word cloud image.

    max_font_size : int or None (default=None)
        Maximum font size for the largest word. If None, height of the image is
        used.

    Attributes
    ----------
    words_ : list of tuples (string, float)
        Word tokens with associated frequency.

    layout_ : list of tuples (string, int, (int, int), int, color))
        Encodes the fitted word cloud. Encodes for each word the string, font
        size, position, orientation and color.
    """

    def __init__(self, font_path=None, width=400, height=200, margin=5,
                 ranks_only=False, prefer_horizontal=0.9, mask=None, scale=1,
                 color_func=random_color_func, max_words=200, stopwords=None,
                 random_state=None, background_color='black', max_font_size=None):
        if stopwords is None:
            stopwords = STOPWORDS
        if font_path is None:
            font_path = FONT_PATH
        self.font_path = font_path
        self.width = width
        self.height = height
        self.margin = margin
        self.ranks_only = ranks_only
        self.prefer_horizontal = prefer_horizontal
        self.mask = mask
        self.scale = scale
        self.color_func = color_func
        self.max_words = max_words
        self.stopwords = stopwords
        if isinstance(random_state, int):
            random_state = Random(random_state)
        self.random_state = random_state
        self.background_color = background_color
        if max_font_size is None:
            max_font_size = height
        self.max_font_size = max_font_size

    def _fit_words(self, words):
        """Generate the positions for words.

        Parameters
        ----------
        words : array of tuples
            A tuple contains the word and its frequency.

        Returns
        -------
        layout_ : list of tuples (string, int, (int, int), int, color))
            Encodes the fitted word cloud. Encodes for each word the string, font
            size, position, orientation and color.

        Notes
        -----
        Larger canvases with make the code significantly slower. If you need a large
        word cloud, run this function with a lower canvas size, and draw it with a
        larger scale.

        In the current form it actually just uses the rank of the counts, i.e. the
        relative differences don't matter. Play with setting the font_size in the
        main loop for different styles.
        """
        if self.random_state is not None:
            random_state = self.random_state
        else:
            random_state = Random()

        if len(words) <= 0:
            print("We need at least 1 word to plot a word cloud, got %d."
                  % len(words))

        if self.mask is not None:
            width = self.mask.shape[1]
            height = self.mask.shape[0]
            # the order of the cumsum's is important for speed ?!
            integral = np.cumsum(np.cumsum(self.mask, axis=1), axis=0).astype(np.uint32)
        else:
            height, width = self.height, self.width
            integral = np.zeros((height, width), dtype=np.uint32)

        # create image
        img_grey = Image.new("L", (width, height))
        draw = ImageDraw.Draw(img_grey)
        img_array = np.asarray(img_grey)
        font_sizes, positions, orientations, colors = [], [], [], []

        font_size = self.max_font_size

        # start drawing grey image
        for word, count in words:
            # alternative way to set the font size
            if not self.ranks_only:
                font_size = min(font_size, int(100 * np.log(count + 100)))
            while True:
                # try to find a position
                font = ImageFont.truetype(self.font_path, font_size)
                # transpose font optionally
{'fraction_non_alphanumeric': 0.08313580897199543, 'fraction_numerical': 0.009656746554297253, 'mean_word_length': 2.1365638766519823, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4475690', 'n_tokens_mistral': 3223, 'n_tokens_neox': 3073, 'n_words': 1143}