Source for file porter_en.php

Documentation is available at porter_en.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Administrator
  4.  * @subpackage  com_finder
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. JLoader::register('FinderIndexerStemmer'dirname(__DIR__'/stemmer.php');
  13.  
  14. /**
  15.  * Porter English stemmer class for the Finder indexer package.
  16.  *
  17.  * This class was adapted from one written by Richard Heyes.
  18.  * See copyright and link information above.
  19.  *
  20.  * @package     Joomla.Administrator
  21.  * @subpackage  com_finder
  22.  * @since       2.5
  23.  */
  24. {
  25.     /**
  26.      * Regex for matching a consonant.
  27.      *
  28.      * @var    string 
  29.      * @since  2.5
  30.      */
  31.     private static $_regex_consonant '(?:[bcdfghjklmnpqrstvwxz]|(?<=[aeiou])y|^y)';
  32.  
  33.     /**
  34.      * Regex for matching a vowel
  35.      *
  36.      * @var    string 
  37.      * @since  2.5
  38.      */
  39.     private static $_regex_vowel '(?:[aeiou]|(?<![aeiou])y)';
  40.  
  41.     /**
  42.      * Method to stem a token and return the root.
  43.      *
  44.      * @param   string  $token  The token to stem.
  45.      * @param   string  $lang   The language of the token.
  46.      *
  47.      * @return  string  The root token.
  48.      *
  49.      * @since   2.5
  50.      */
  51.     public function stem($token$lang)
  52.     {
  53.         // Check if the token is long enough to merit stemming.
  54.         if (strlen($token<= 2)
  55.         {
  56.             return $token;
  57.         }
  58.  
  59.         // Check if the language is English or All.
  60.         if ($lang !== 'en' && $lang != '*')
  61.         {
  62.             return $token;
  63.         }
  64.  
  65.         // Stem the token if it is not in the cache.
  66.         if (!isset($this->cache[$lang][$token]))
  67.         {
  68.             // Stem the token.
  69.             $result $token;
  70.             $result self::_step1ab($result);
  71.             $result self::_step1c($result);
  72.             $result self::_step2($result);
  73.             $result self::_step3($result);
  74.             $result self::_step4($result);
  75.             $result self::_step5($result);
  76.  
  77.             // Add the token to the cache.
  78.             $this->cache[$lang][$token$result;
  79.         }
  80.  
  81.         return $this->cache[$lang][$token];
  82.     }
  83.  
  84.     /**
  85.      * Step 1
  86.      *
  87.      * @param   string  $word  The token to stem.
  88.      *
  89.      * @return  string 
  90.      *
  91.      * @since   2.5
  92.      */
  93.     private static function _step1ab($word)
  94.     {
  95.         // Part a
  96.         if (substr($word-1== 's')
  97.         {
  98.             self::_replace($word'sses''ss')
  99.             or self::_replace($word'ies''i')
  100.             or self::_replace($word'ss''ss')
  101.             or self::_replace($word's''');
  102.         }
  103.  
  104.         // Part b
  105.         if (substr($word-21!= 'e' or !self::_replace($word'eed''ee'0))
  106.         {
  107.             // First rule
  108.             $v self::$_regex_vowel;
  109.  
  110.             // Words ending with ing and ed
  111.             // Note use of && and OR, for precedence reasons
  112.             if (preg_match("#$v+#"substr($word0-3)) && self::_replace($word'ing''')
  113.                 or preg_match("#$v+#"substr($word0-2)) && self::_replace($word'ed'''))
  114.             {
  115.                 // If one of above two test successful
  116.                 if (!self::_replace($word'at''ate'and !self::_replace($word'bl''ble'and !self::_replace($word'iz''ize'))
  117.                 {
  118.                     // Double consonant ending
  119.                     if (self::_doubleConsonant($wordand substr($word-2!= 'll' and substr($word-2!= 'ss' and substr($word-2!= 'zz')
  120.                     {
  121.                         $word substr($word0-1);
  122.                     }
  123.                     elseif (self::_m($word== and self::_cvc($word))
  124.                     {
  125.                         $word .= 'e';
  126.                     }
  127.                 }
  128.             }
  129.         }
  130.  
  131.         return $word;
  132.     }
  133.  
  134.     /**
  135.      * Step 1c
  136.      *
  137.      * @param   string  $word  The token to stem.
  138.      *
  139.      * @return  string 
  140.      *
  141.      * @since   2.5
  142.      */
  143.     private static function _step1c($word)
  144.     {
  145.         $v self::$_regex_vowel;
  146.  
  147.         if (substr($word-1== 'y' && preg_match("#$v+#"substr($word0-1)))
  148.         {
  149.             self::_replace($word'y''i');
  150.         }
  151.  
  152.         return $word;
  153.     }
  154.  
  155.     /**
  156.      * Step 2
  157.      *
  158.      * @param   string  $word  The token to stem.
  159.      *
  160.      * @return  string 
  161.      *
  162.      * @since   2.5
  163.      */
  164.     private static function _step2($word)
  165.     {
  166.         switch (substr($word-21))
  167.         {
  168.             case 'a':
  169.                 self::_replace($word'ational''ate'0)
  170.                 or self::_replace($word'tional''tion'0);
  171.                 break;
  172.             case 'c':
  173.                 self::_replace($word'enci''ence'0)
  174.                 or self::_replace($word'anci''ance'0);
  175.                 break;
  176.             case 'e':
  177.                 self::_replace($word'izer''ize'0);
  178.                 break;
  179.             case 'g':
  180.                 self::_replace($word'logi''log'0);
  181.                 break;
  182.             case 'l':
  183.                 self::_replace($word'entli''ent'0)
  184.                 or self::_replace($word'ousli''ous'0)
  185.                 or self::_replace($word'alli''al'0)
  186.                 or self::_replace($word'bli''ble'0)
  187.                 or self::_replace($word'eli''e'0);
  188.                 break;
  189.             case 'o':
  190.                 self::_replace($word'ization''ize'0)
  191.                 or self::_replace($word'ation''ate'0)
  192.                 or self::_replace($word'ator''ate'0);
  193.                 break;
  194.             case 's':
  195.                 self::_replace($word'iveness''ive'0)
  196.                 or self::_replace($word'fulness''ful'0)
  197.                 or self::_replace($word'ousness''ous'0)
  198.                 or self::_replace($word'alism''al'0);
  199.                 break;
  200.             case 't':
  201.                 self::_replace($word'biliti''ble'0)
  202.                 or self::_replace($word'aliti''al'0)
  203.                 or self::_replace($word'iviti''ive'0);
  204.                 break;
  205.         }
  206.  
  207.         return $word;
  208.     }
  209.  
  210.     /**
  211.      * Step 3
  212.      *
  213.      * @param   string  $word  The token to stem.
  214.      *
  215.      * @return  string 
  216.      *
  217.      * @since   2.5
  218.      */
  219.     private static function _step3($word)
  220.     {
  221.         switch (substr($word-21))
  222.         {
  223.             case 'a':
  224.                 self::_replace($word'ical''ic'0);
  225.                 break;
  226.             case 's':
  227.                 self::_replace($word'ness'''0);
  228.                 break;
  229.             case 't':
  230.                 self::_replace($word'icate''ic'0)
  231.                 or self::_replace($word'iciti''ic'0);
  232.                 break;
  233.             case 'u':
  234.                 self::_replace($word'ful'''0);
  235.                 break;
  236.             case 'v':
  237.                 self::_replace($word'ative'''0);
  238.                 break;
  239.             case 'z':
  240.                 self::_replace($word'alize''al'0);
  241.                 break;
  242.         }
  243.  
  244.         return $word;
  245.     }
  246.  
  247.     /**
  248.      * Step 4
  249.      *
  250.      * @param   string  $word  The token to stem.
  251.      *
  252.      * @return  string 
  253.      *
  254.      * @since   2.5
  255.      */
  256.     private static function _step4($word)
  257.     {
  258.         switch (substr($word-21))
  259.         {
  260.             case 'a':
  261.                 self::_replace($word'al'''1);
  262.                 break;
  263.             case 'c':
  264.                     self::_replace($word'ance'''1)
  265.                 or self::_replace($word'ence'''1);
  266.                 break;
  267.             case 'e':
  268.                 self::_replace($word'er'''1);
  269.                 break;
  270.             case 'i':
  271.                 self::_replace($word'ic'''1);
  272.                 break;
  273.             case 'l':
  274.                 self::_replace($word'able'''1)
  275.                 or self::_replace($word'ible'''1);
  276.                 break;
  277.             case 'n':
  278.                 self::_replace($word'ant'''1)
  279.                 or self::_replace($word'ement'''1)
  280.                 or self::_replace($word'ment'''1)
  281.                 or self::_replace($word'ent'''1);
  282.                 break;
  283.             case 'o':
  284.                 if (substr($word-4== 'tion' or substr($word-4== 'sion')
  285.                 {
  286.                     self::_replace($word'ion'''1);
  287.                 }
  288.                 else
  289.                 {
  290.                     self::_replace($word'ou'''1);
  291.                 }
  292.                 break;
  293.             case 's':
  294.                 self::_replace($word'ism'''1);
  295.                 break;
  296.             case 't':
  297.                     self::_replace($word'ate'''1)
  298.                 or self::_replace($word'iti'''1);
  299.                 break;
  300.             case 'u':
  301.                 self::_replace($word'ous'''1);
  302.                 break;
  303.             case 'v':
  304.                 self::_replace($word'ive'''1);
  305.                 break;
  306.             case 'z':
  307.                 self::_replace($word'ize'''1);
  308.                 break;
  309.         }
  310.  
  311.         return $word;
  312.     }
  313.  
  314.     /**
  315.      * Step 5
  316.      *
  317.      * @param   string  $word  The token to stem.
  318.      *
  319.      * @return  string 
  320.      *
  321.      * @since   2.5
  322.      */
  323.     private static function _step5($word)
  324.     {
  325.         // Part a
  326.         if (substr($word-1== 'e')
  327.         {
  328.             if (self::_m(substr($word0-1)) 1)
  329.             {
  330.                 self::_replace($word'e''');
  331.             }
  332.             elseif (self::_m(substr($word0-1)) == 1)
  333.             {
  334.                 if (!self::_cvc(substr($word0-1)))
  335.                 {
  336.                     self::_replace($word'e''');
  337.                 }
  338.             }
  339.         }
  340.  
  341.         // Part b
  342.         if (self::_m($wordand self::_doubleConsonant($wordand substr($word-1== 'l')
  343.         {
  344.             $word substr($word0-1);
  345.         }
  346.  
  347.         return $word;
  348.     }
  349.  
  350.     /**
  351.      * Replaces the first string with the second, at the end of the string. If third
  352.      * arg is given, then the preceding string must match that m count at least.
  353.      *
  354.      * @param   string   &$str   String to check
  355.      * @param   string   $check  Ending to check for
  356.      * @param   string   $repl   Replacement string
  357.      * @param   integer  $m      Optional minimum number of m() to meet
  358.      *
  359.      * @return  boolean  Whether the $check string was at the end
  360.      *                    of the $str string. True does not necessarily mean
  361.      *                    that it was replaced.
  362.      *
  363.      * @since   2.5
  364.      */
  365.     private static function _replace(&$str$check$repl$m null)
  366.     {
  367.         $len strlen($check);
  368.  
  369.         if (substr($str$len== $check)
  370.         {
  371.             $substr substr($str0$len);
  372.  
  373.             if (is_null($mor self::_m($substr$m)
  374.             {
  375.                 $str $substr $repl;
  376.             }
  377.  
  378.             return true;
  379.         }
  380.  
  381.         return false;
  382.     }
  383.  
  384.     /**
  385.      * m() measures the number of consonant sequences in $str. if c is
  386.      * a consonant sequence and v a vowel sequence, and <..> indicates arbitrary
  387.      * presence,
  388.      *
  389.      * <c><v>       gives 0
  390.      * <c>vc<v>     gives 1
  391.      * <c>vcvc<v>   gives 2
  392.      * <c>vcvcvc<v> gives 3
  393.      *
  394.      * @param   string  $str  The string to return the m count for
  395.      *
  396.      * @return  integer  The m count
  397.      *
  398.      * @since   2.5
  399.      */
  400.     private static function _m($str)
  401.     {
  402.         $c self::$_regex_consonant;
  403.         $v self::$_regex_vowel;
  404.  
  405.         $str preg_replace("#^$c+#"''$str);
  406.         $str preg_replace("#$v+$#"''$str);
  407.  
  408.         preg_match_all("#($v+$c+)#"$str$matches);
  409.  
  410.         return count($matches[1]);
  411.     }
  412.  
  413.     /**
  414.      * Returns true/false as to whether the given string contains two
  415.      * of the same consonant next to each other at the end of the string.
  416.      *
  417.      * @param   string  $str  String to check
  418.      *
  419.      * @return  boolean  Result
  420.      *
  421.      * @since   2.5
  422.      */
  423.     private static function _doubleConsonant($str)
  424.     {
  425.         $c self::$_regex_consonant;
  426.  
  427.         return preg_match("#$c{2}$#"$str$matchesand $matches[0]{0== $matches[0]{1};
  428.     }
  429.  
  430.     /**
  431.      * Checks for ending CVC sequence where second C is not W, X or Y
  432.      *
  433.      * @param   string  $str  String to check
  434.      *
  435.      * @return  boolean  Result
  436.      *
  437.      * @since   2.5
  438.      */
  439.     private static function _cvc($str)
  440.     {
  441.         $c self::$_regex_consonant;
  442.         $v self::$_regex_vowel;
  443.  
  444.         return preg_match("#($c$v$c)$#"$str$matchesand strlen($matches[1]== and $matches[1]{2!= 'w' and $matches[1]{2!= 'x'
  445.             and $matches[1]{2!= 'y';
  446.     }
  447. }

Documentation generated on Tue, 19 Nov 2013 15:10:58 +0100 by phpDocumentor 1.4.3