You are here

porterstemmer.module in Porter-Stemmer 5

File

porterstemmer.module
View source
<?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);
}

/**
* Implementation of hook_help().
*/
function porterstemmer_help($section = 'admin/help#search') {
  switch ($section) {
    case 'admin/modules#description':
      return t('Implements the Porter-Stemmer algorithm to improve English searching.');
  }
}

/**
* 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. Simple huh?
*
* @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;
}

/**
* What, you mean it's not obvious from the name?
*
* 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';
}

Functions

Namesort descending Description
cvc Checks for ending CVC sequence where second C is not W, X or Y
doubleConsonant 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.
m What, you mean it's not obvious from the name?
porterstemmer_help Implementation of hook_help().
porterstemmer_search_preprocess Implementation of hook_search_preprocess
replace 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.
stem Stems a word. Simple huh?
step1ab Step 1
step1c Step 1c
step2 Step 2
step3 Step 3
step4 Step 4
step5 Step 5

Constants

Namesort descending Description
regex_consonant Regex for matching a consonant
regex_vowel Regex for matching a vowel