<?php
/*
 * The implementation of the Porter Stemmer is free software,
 * and is © 2005 Richard Heyes (http://www.phpguru.org/). Certain elements
 * were borrowed from the (broken) implementation by Jon Abernathy.
 *
 * It was modified by Steven Wittens for PHP4 compatibility and Drupal integration.
 */

/**
* Implementation of hook_search_preprocess
*/
function porterstemmer_search_preprocess(&$text) {
  // Split words from noise and remove apostrophes
  $words = preg_split('/([^a-zA-Z]+)/', str_replace("'", '', $text), -1, PREG_SPLIT_DELIM_CAPTURE);

  // Process each word
  $odd = true;
  foreach ($words as $k => $word) {
    if ($odd) {
      $words[$k] = Stem($word);
    }
    $odd = !$odd;
  }

  // Put it all back together
  return implode('', $words);
}

/**
* Regex for matching a consonant
*/
define('regex_consonant', '(?:[bcdfghjklmnpqrstvwxz]|(?<=[aeiou])y|^y)');

/**
* Regex for matching a vowel
*/
define('regex_vowel', '(?:[aeiou]|(?<![aeiou])y)');

/**
* Stems a word
*
* @param  string $word Word to stem
* @return string       Stemmed word
*/
function stem($word) {
  if (strlen($word) <= 2) {
      return $word;
  }

  $word = step1ab($word);
  $word = step1c($word);
  $word = step2($word);
  $word = step3($word);
  $word = step4($word);
  $word = step5($word);

  return $word;
}


/**
* Step 1
*/
function step1ab($word) {
  // Part a
  if (substr($word, -1) == 's') {
       replace($word, 'sses', 'ss')
    OR replace($word, 'ies', 'i')
    OR replace($word, 'ss', 'ss')
    OR replace($word, 's', '');
  }

  // Part b
  if (substr($word, -2, 1) != 'e' OR !replace($word, 'eed', 'ee', 0)) { // First rule
    $v = regex_vowel;

    // ing and ed
    if (   preg_match("#$v+#", substr($word, 0, -3)) && replace($word, 'ing', '')
        OR preg_match("#$v+#", substr($word, 0, -2)) && replace($word, 'ed', '')) { // Note use of && and OR, for precedence reasons

        // If one of above two test successful
        if (    !replace($word, 'at', 'ate')
            AND !replace($word, 'bl', 'ble')
            AND !replace($word, 'iz', 'ize')) {

            // Double consonant ending
            if (    doubleConsonant($word)
                AND substr($word, -2) != 'll'
                AND substr($word, -2) != 'ss'
                AND substr($word, -2) != 'zz') {

                $word = substr($word, 0, -1);

              } else if (m($word) == 1 AND cvc($word)) {
                  $word .= 'e';
              }
          }
      }
  }

  return $word;
}

/**
* Step 1c
*
* @param string $word Word to stem
*/
function step1c($word) {
  $v = regex_vowel;

  if (substr($word, -1) == 'y' && preg_match("#$v+#", substr($word, 0, -1))) {
    replace($word, 'y', 'i');
  }

  return $word;
}

/**
* Step 2
*
* @param string $word Word to stem
*/
function step2($word) {
  switch (substr($word, -2, 1)) {
      case 'a':
             replace($word, 'ational', 'ate', 0)
          OR replace($word, 'tional', 'tion', 0);
          break;

      case 'c':
             replace($word, 'enci', 'ence', 0)
          OR replace($word, 'anci', 'ance', 0);
          break;

      case 'e':
          replace($word, 'izer', 'ize', 0);
          break;

      case 'g':
          replace($word, 'logi', 'log', 0);
          break;

      case 'l':
             replace($word, 'entli', 'ent', 0)
          OR replace($word, 'ousli', 'ous', 0)
          OR replace($word, 'alli', 'al', 0)
          OR replace($word, 'bli', 'ble', 0)
          OR replace($word, 'eli', 'e', 0);
          break;

      case 'o':
             replace($word, 'ization', 'ize', 0)
          OR replace($word, 'ation', 'ate', 0)
          OR replace($word, 'ator', 'ate', 0);
          break;

      case 's':
             replace($word, 'iveness', 'ive', 0)
          OR replace($word, 'fulness', 'ful', 0)
          OR replace($word, 'ousness', 'ous', 0)
          OR replace($word, 'alism', 'al', 0);
          break;

      case 't':
             replace($word, 'biliti', 'ble', 0)
          OR replace($word, 'aliti', 'al', 0)
          OR replace($word, 'iviti', 'ive', 0);
          break;
  }

  return $word;
}

/**
* Step 3
*
* @param string $word String to stem
*/
function step3($word) {
  switch (substr($word, -2, 1)) {
      case 'a':
          replace($word, 'ical', 'ic', 0);
          break;

      case 's':
          replace($word, 'ness', '', 0);
          break;

      case 't':
            replace($word, 'icate', 'ic', 0)
          OR replace($word, 'iciti', 'ic', 0);
          break;

      case 'u':
          replace($word, 'ful', '', 0);
          break;

      case 'v':
          replace($word, 'ative', '', 0);
          break;

      case 'z':
          replace($word, 'alize', 'al', 0);
          break;
  }

  return $word;
}

/**
* Step 4
*
* @param string $word Word to stem
*/
function step4($word) {
  switch (substr($word, -2, 1)) {
      case 'a':
          replace($word, 'al', '', 1);
          break;

      case 'c':
             replace($word, 'ance', '', 1)
          OR replace($word, 'ence', '', 1);
          break;

      case 'e':
          replace($word, 'er', '', 1);
          break;

      case 'i':
          replace($word, 'ic', '', 1);
          break;

      case 'l':
             replace($word, 'able', '', 1)
          OR replace($word, 'ible', '', 1);
          break;

      case 'n':
             replace($word, 'ant', '', 1)
          OR replace($word, 'ement', '', 1)
          OR replace($word, 'ment', '', 1)
          OR replace($word, 'ent', '', 1);
          break;

      case 'o':
          if (substr($word, -4) == 'tion' OR substr($word, -4) == 'sion') {
             replace($word, 'ion', '', 1);
          } else {
              replace($word, 'ou', '', 1);
          }
          break;

      case 's':
          replace($word, 'ism', '', 1);
          break;

      case 't':
             replace($word, 'ate', '', 1)
          OR replace($word, 'iti', '', 1);
          break;

      case 'u':
          replace($word, 'ous', '', 1);
          break;

      case 'v':
          replace($word, 'ive', '', 1);
          break;

      case 'z':
          replace($word, 'ize', '', 1);
          break;
  }

  return $word;
}

/**
* Step 5
*
* @param string $word Word to stem
*/
function step5($word) {
  // Part a
  if (substr($word, -1) == 'e') {
      if (m(substr($word, 0, -1)) > 1) {
          replace($word, 'e', '');

      } else if (m(substr($word, 0, -1)) == 1) {

          if (!cvc(substr($word, 0, -1))) {
              replace($word, 'e', '');
          }
      }
  }

  // Part b
  if (m($word) > 1 AND doubleConsonant($word) AND substr($word, -1) == 'l') {
      $word = substr($word, 0, -1);
  }

  return $word;
}

/**
* Replaces the first string with the second, at the end of the string. If third
* arg is given, then the preceding string must match that m count at least.
*
* @param  string $str   String to check
* @param  string $check Ending to check for
* @param  string $repl  Replacement string
* @param  int    $m     Optional minimum number of m() to meet
* @return bool          Whether the $check string was at the end
*                       of the $str string. True does not necessarily mean
*                       that it was replaced.
*/
function replace(&$str, $check, $repl, $m = null) {
  $len = 0 - strlen($check);

  if (substr($str, $len) == $check) {
    $substr = substr($str, 0, $len);
    if (is_null($m) OR m($substr) > $m) {
      $str = $substr . $repl;
    }

    return true;
  }

  return false;
}

/**
* m() measures the number of consonant sequences in $str. if c is
* a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
* presence,
*
* <c><v>       gives 0
* <c>vc<v>     gives 1
* <c>vcvc<v>   gives 2
* <c>vcvcvc<v> gives 3
*
* @param  string $str The string to return the m count for
* @return int         The m count
*/
function m($str) {
  $c = regex_consonant;
  $v = regex_vowel;

  $str = preg_replace("#^$c+#", '', $str);
  $str = preg_replace("#$v+$#", '', $str);

  preg_match_all("#($v+$c+)#", $str, $matches);

  return count($matches[1]);
}

/**
* Returns true/false as to whether the given string contains two
* of the same consonant next to each other at the end of the string.
*
* @param  string $str String to check
* @return bool        Result
*/
function doubleConsonant($str) {
  $c = regex_consonant;

  return preg_match("#$c{2}$#", $str, $matches) AND $matches[0]{0} == $matches[0]{1};
}

/**
* Checks for ending CVC sequence where second C is not W, X or Y
*
* @param  string $str String to check
* @return bool        Result
*/
function cvc($str) {
  $c = regex_consonant;
  $v = regex_vowel;

  return     preg_match("#($c$v$c)$#", $str, $matches)
         AND strlen($matches[1]) == 3
         AND $matches[1]{2} != 'w'
         AND $matches[1]{2} != 'x'
         AND $matches[1]{2} != 'y';
}

?>
