<commit_before><?php
/**
 * Tag model.
 *
 * This handles adding, removing and modifying tags.
 * @author <NAME> <<EMAIL>>
 */
class Tag
{
  /**
    * Updates count values in tags when an object is updates.
    * Keeps track of # of objects tagged in the Tag object itself.
    * Both params are a full set of tags before and after the update.
    *
    * @param array $existingTags The tags previously on the object.
    * @param array $updatedTags The tags currently being updated on the object.
    * @return boolean
    */
  public static function updateTagCounts($existingTags, $updatedTags)
  {
    $tagsToDecrement = array_diff($existingTags, $updatedTags);
    $tagsToIncrement = array_diff($updatedTags, $existingTags);
    $tagsToUpdate = array();
    foreach($tagsToDecrement as $tg)
      $tagsToUpdate[self::sanitize($tg)] = -1;
    foreach($tagsToIncrement as $tg)
      $tagsToUpdate[self::sanitize($tg)] = 1;
    return getDb()->postTagsCounter($tagsToUpdate);
  }

  /**
    * Groups tags by weight for tag cloud generation
    * Weights are 1-10.
    *
    * @param array $tags An array of Tag objects optionally passed in else queried from the database.
    * @return array Tag object augmented with a "weight" property.
    */
  public static function groupByWeight($tags = null)
  {
    if($tags === null)
    {
      $tags = getApi()->invoke("/tags.json");
      $tags = $tags['result'];
    }

    $maxTags = 0;
    $minTags = PHP_INT_MAX;
    if(count($tags) > 0)
    {
      foreach($tags as $tag)
      {
        if($tag['count'] < $minTags)
          $minTags = $tag['count'];
        if($tag['count'] > $maxTags)
          $maxTags = $tag['count'];
      }

      // we create 10 groups based on count using %s
      $range = $maxTags - $minTags;
      // step needs to be float so we don't divide by zero
      $step = floatval($range / 9);
      foreach($tags as $key => $tag)
        $tags[$key]['weight'] = intval(($tag['count']-$minTags) / $step)+1;
    }
    return $tags;
  }

  public static function sanitize($tag)
  {
    return preg_replace('/[^a-zA-Z0-9]/', '', $tag);
  }

  public static function sanitizeTagsAsString($tags)
  {
    $tagsArray = (array)explode(',', $tags);
    foreach($tagsArray as $key => $val)
      $tagsArray[$key] = self::sanitize($val);

    return implode(',', $tagsArray);
  }
}
<commit_msg>Making sure that the step value is at least 1. Closes gh-46<commit_after><?php
/**
 * Tag model.
 *
 * This handles adding, removing and modifying tags.
 * @author <NAME> <<EMAIL>>
 */
class Tag
{
  /**
    * Updates count values in tags when an object is updates.
    * Keeps track of # of objects tagged in the Tag object itself.
    * Both params are a full set of tags before and after the update.
    *
    * @param array $existingTags The tags previously on the object.
    * @param array $updatedTags The tags currently being updated on the object.
    * @return boolean
    */
  public static function updateTagCounts($existingTags, $updatedTags)
  {
    $tagsToDecrement = array_diff($existingTags, $updatedTags);
    $tagsToIncrement = array_diff($updatedTags, $existingTags);
    $tagsToUpdate = array();
    foreach($tagsToDecrement as $tg)
      $tagsToUpdate[self::sanitize($tg)] = -1;
    foreach($tagsToIncrement as $tg)
      $tagsToUpdate[self::sanitize($tg)] = 1;
    return getDb()->postTagsCounter($tagsToUpdate);
  }

  /**
    * Groups tags by weight for tag cloud generation
    * Weights are 1-10.
    *
    * @param array $tags An array of Tag objects optionally passed in else queried from the database.
    * @return array Tag object augmented with a "weight" property.
    */
  public static function groupByWeight($tags = null)
  {
    if($tags === null)
    {
      $tags = getApi()->invoke("/tags.json");
      $tags = $tags['result'];
    }

    $maxTags = 0;
    $minTags = PHP_INT_MAX;
    if(count($tags) > 0)
    {
      foreach($tags as $tag)
      {
        if($tag['count'] < $minTags)
          $minTags = $tag['count'];
        if($tag['count'] > $maxTags)
          $maxTags = $tag['count'];
      }

      // we create 10 groups based on count using %s
      $range = max(($maxTags - $minTags), 1);
      // step needs to be float so we don't divide by zero
      $step = floatval($range / 9);
      foreach($tags as $key => $tag)
        $tags[$key]['weight'] = intval(($tag['count']-$minTags) / $step)+1;
    }
    return $tags;
  }

  public static function sanitize($tag)
  {
    return preg_replace('/[^a-zA-Z0-9]/', '', $tag);
  }

  public static function sanitizeTagsAsString($tags)
  {
    $tagsArray = (array)explode(',', $tags);
    foreach($tagsArray as $key => $val)
      $tagsArray[$key] = self::sanitize($val);

    return implode(',', $tagsArray);
  }
}

{'fraction_non_alphanumeric': 0.14390448261416003, 'fraction_numerical': 0.006284038542103058, 'mean_word_length': 2.841512469831054, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1978946', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1493, 'n_words': 448}