Advertisement
zero50x

Porter stemmer ru

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