Advertisement
Guest User

Zend_Search_Lucene russian stemmer

a guest
Feb 14th, 2011
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. <?php
  2.  
  3. class Application_Search_Stemmer_Russian extends Zend_Search_Lucene_Analysis_TokenFilter
  4. {
  5.     protected $_stemCaching = 0;
  6.     protected $_stemCache = array();
  7.     const VOWEL = '/аеиоуыэюя/u';
  8.     const PERFECTIVEGROUND = '/((ив|ивши|ившись|ыв|ывши|ывшись)|((?<=[ая])(в|вши|вшись)))$/u';
  9.     const REFLEXIVE = '/(с[яь])$/u';
  10.     const ADJECTIVE = '/(ее|ие|ые|ое|ими|ыми|ей|ий|ый|ой|ем|им|ым|ом|его|ого|еых|ую|юю|ая|яя|ою|ею)$/u';
  11.     const PARTICIPLE = '/((ивш|ывш|ующ)|((?<=[ая])(ем|нн|вш|ющ|щ)))$/u';
  12.     const VERB = '/((ила|ыла|ена|ейте|уйте|ите|или|ыли|ей|уй|ил|ыл|им|ым|ены|ить|ыть|ишь|ую|ю)|((?<=[ая])(ла|на|ете|йте|ли|й|л|ем|н|ло|но|ет|ют|ны|ть|ешь|нно)))$/u';
  13.     const NOUN = '/(а|ев|ов|ие|ье|е|иями|ями|ами|еи|ии|и|ией|ей|ой|ий|й|и|ы|ь|ию|ью|ю|ия|ья|я)$/u';
  14.     const RVRE = '/^(.*?[аеиоуыэюя])(.*)$/u';
  15.     const DERIVATIONAL = '/[^аеиоуыэюя][аеиоуыэюя]+[^аеиоуыэюя]+[аеиоуыэюя].*(?<=о)сть?$/u';
  16.  
  17.     protected function _s(&$s, $re, $to)
  18.     {
  19.         $orig = $s;
  20.         $s = preg_replace($re, $to, $s);
  21.         return $orig !== $s;
  22.     }
  23.  
  24.     protected function _m($s, $re)
  25.     {
  26.         return preg_match($re, $s);
  27.     }
  28.  
  29.     public function stem_word($word)
  30.     {
  31.         $word = mb_strtolower($word);
  32.         $word = str_replace('ё', 'е', $word);
  33.         # Check against cache of stemmed words
  34.        if ($this->_stemCaching && isset($this->_stemCache[$word])) {
  35.             return $this->_stemCache[$word];
  36.         }
  37.         $stem = $word;
  38.         do {
  39.           if (!preg_match(self::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, self::PERFECTIVEGROUND, '')) {
  46.               $this->_s($RV, self::REFLEXIVE, '');
  47.  
  48.               if ($this->_s($RV, self::ADJECTIVE, '')) {
  49.                   $this->_s($RV, self::PARTICIPLE, '');
  50.               } else {
  51.                   if (!$this->_s($RV, self::VERB, ''))
  52.                       $this->_s($RV, self::NOUN, '');
  53.               }
  54.           }
  55.  
  56.           # Step 2
  57.          $this->_s($RV, '/и$/u', '');
  58.  
  59.           # Step 3
  60.          if ($this->_m($RV, self::DERIVATIONAL))
  61.               $this->_s($RV, '/ость?$/u', '');
  62.  
  63.           # Step 4
  64.          if (!$this->_s($RV, '/ь$/u', '')) {
  65.               $this->_s($RV, '/ейше?/u', '');
  66.               $this->_s($RV, '/нн$/u', 'н');
  67.           }
  68.  
  69.           $stem = $start.$RV;
  70.         } while(false);
  71.         if ($this->_stemCaching) $this->_stemCache[$word] = $stem;
  72.         return $stem;
  73.     }
  74.  
  75.     public function stemCaching($parm_ref)
  76.     {
  77.         $caching_level = @$parm_ref['-level'];
  78.         if ($caching_level) {
  79.             if (!$this->_m($caching_level, '/^[012]$/u')) {
  80.                 die(__CLASS__ . "::stemCaching() - Legal values are '0','1' or '2'. '$caching_level' is not a legal value");
  81.             }
  82.             $this->_stemCaching = $caching_level;
  83.         }
  84.         return $this->_stemCaching;
  85.     }
  86.  
  87.     public function clearStemCache()
  88.     {
  89.         $this->_stemCache = array();
  90.     }
  91.  
  92.     public function normalize(Zend_Search_Lucene_Analysis_Token $srcToken)
  93.     {
  94.         $word = $srcToken->getTermText();
  95.         $result = $this->stem_word($word);
  96.         return new Zend_Search_Lucene_Analysis_Token(
  97.                 $result,
  98.                 $srcToken->getStartOffset(),
  99.                 $srcToken->getEndOffset());
  100.     }
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement