riff

Lingua_Stem_Ru

Apr 2nd, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.06 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. /**
  5.  * Class Lingua_Stem_Ru
  6.  * Стеммер Портера.
  7.  * взято от сюда: http://forum.dklab.ru/php/advises/HeuristicWithoutTheDictionaryExtractionOfARootFromRussianWord.html
  8.  */
  9. class Lingua_Stem_Ru
  10. {
  11.     var $VERSION = "0.02";
  12.     var $Stem_Caching = 0;
  13.     var $Stem_Cache = array();
  14.     var $VOWEL = '/аеиоуыэюя/ui';
  15.     var $PERFECTIVEGROUND = '/((ив|ивши|ившись|ыв|ывши|ывшись)|((?<=[ая])(в|вши|вшись)))$/ui';
  16.     var $REFLEXIVE = '/(с[яь])$/ui';
  17.     var $ADJECTIVE = '/(ее|ие|ые|ое|ими|ыми|ей|ий|ый|ой|ем|им|ым|ом|его|ого|еых|ую|юю|ая|яя|ою|ею)$/ui';
  18.     var $PARTICIPLE = '/((ивш|ывш|ующ)|((?<=[ая])(ем|нн|вш|ющ|щ)))$/ui';
  19.     var $VERB = '/((ила|ыла|ена|ейте|уйте|ите|или|ыли|ей|уй|ил|ыл|им|ым|ены|ить|ыть|ишь|ую|ю)|((?<=[ая])(ла|на|ете|йте|ли|й|л|ем|н|ло|но|ет|ют|ны|ть|ешь|нно)))$/ui';
  20.     var $NOUN = '/(а|ев|ов|ие|ье|е|иями|ями|ами|еи|ии|и|ией|ей|ой|ий|й|и|ы|ь|ию|ью|ю|ия|ья|я)$/ui';
  21.     var $RVRE = '/^(.*?[аеиоуыэюя])(.*)$/ui';
  22.     var $DERIVATIONAL = '/[^аеиоуыэюя][аеиоуыэюя]+[^аеиоуыэюя]+[аеиоуыэюя].*(?<=о)сть?$/ui';
  23.  
  24.     function s(&$s, $re, $to)
  25.     {
  26.         $orig = $s;
  27.         $s = preg_replace($re, $to, $s);
  28.  
  29.         return $orig !== $s;
  30.     }
  31.  
  32.     function m($s, $re)
  33.     {
  34.         return preg_match($re, $s);
  35.     }
  36.  
  37.     function stem_word($word)
  38.     {
  39.         //$word = strtolower($word);
  40.         $word = strtr($word, array('ё' => 'е'));
  41.         # Check against cache of stemmed words
  42.         if ($this->Stem_Caching && isset($this->Stem_Cache[$word]))
  43.         {
  44.             return $this->Stem_Cache[$word];
  45.         }
  46.         $stem = $word;
  47.         do
  48.         {
  49.             if (!preg_match($this->RVRE, $word, $p)) break;
  50.             $start = $p[1];
  51.             $RV = $p[2];
  52.             if (!$RV) break;
  53.  
  54.             # Step 1
  55.             if (!$this->s($RV, $this->PERFECTIVEGROUND, ''))
  56.             {
  57.                 $this->s($RV, $this->REFLEXIVE, '');
  58.  
  59.                 if ($this->s($RV, $this->ADJECTIVE, ''))
  60.                 {
  61.                     $this->s($RV, $this->PARTICIPLE, '');
  62.                 }
  63.                 else
  64.                 {
  65.                     if (!$this->s($RV, $this->VERB, '')) $this->s($RV, $this->NOUN, '');
  66.                 }
  67.             }
  68.  
  69.             # Step 2
  70.             $this->s($RV, '/и$/ui', '');
  71.  
  72.             # Step 3
  73.             if ($this->m($RV, $this->DERIVATIONAL)) $this->s($RV, '/ость?$/ui', '');
  74.  
  75.             # Step 4
  76.             if (!$this->s($RV, '/ь$/ui', ''))
  77.             {
  78.                 $this->s($RV, '/ейше?/ui', '');
  79.                 $this->s($RV, '/нн$/ui', 'н');
  80.             }
  81.  
  82.             $stem = $start . $RV;
  83.         } while (false);
  84.         if ($this->Stem_Caching) $this->Stem_Cache[$word] = $stem;
  85.  
  86.         return $stem;
  87.     }
  88.  
  89.     function stem_caching($parm_ref)
  90.     {
  91.         $caching_level = @$parm_ref['-level'];
  92.         if ($caching_level)
  93.         {
  94.             if (!$this->m($caching_level, '/^[012]$/'))
  95.             {
  96.                 die(__CLASS__ . "::stem_caching() - Legal values are '0','1' or '2'. '$caching_level' is not a legal value");
  97.             }
  98.             $this->Stem_Caching = $caching_level;
  99.         }
  100.  
  101.         return $this->Stem_Caching;
  102.     }
  103.  
  104.     function clear_stem_cache()
  105.     {
  106.         $this->Stem_Cache = array();
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment