Форум сетевых администраторов
Показаны 6 сообщений на этой странице

Форум сетевых администраторов (http://netadmin.ws/forum/index.php)
- PHP (http://netadmin.ws/forum/forumdisplay.php?forumid=6)
-- Умный поиск? (http://netadmin.ws/forum/showthread.php?threadid=5727)


Помещено Tagore 04-06-2003 в 15:34:

Arrow Умный поиск?

Kто знает что это? Взял из "Adrevenue" lib.php:
Может ли от этого быть толк в русском языку?


PHP:


// Created By: Joe Stump
// Created On: 2000-12-14
// Notes: This is the infamous "Porter Stemming Algorithm" - lots
// of serach engines use something similar to relate words. An example
// would be to turn "running" into "run" but "kissing" into "kiss".
// 
// Returns: string PorterStem(string word)
  
function stemmer($word)
  {
    
$vowels = array('a','e','i','o','u','y');
    
$o      = array('w','x','y');

    
$test_m = eregi_replace('^[a-z]','',$word);
    
$test_m = eregi_replace('[a-z]$','',$test_m); 
    for(
$i = 0 ; $i < strlen($test_m) ; ++$i)
    {
      if(
eregi('[b-df-hj-np-tw-z]',$test_m[$i]) && eregi('[aeiou]',$test_m[($i - 1)]))
      {
        ++
$total;
      }
    }

    if(
$total)
    {
      
$m = $total;
    }
    else
    {
      
$m = 0;
    }

    
// Step 1a
    
$replaces = array(
          
'/sses$/' => 'ss',
                  
'/ies$/' => 'i',
                  
'/ss$/' => 'ss',
                  
'/s$/' => '');

    while(list(
$search,$replace) = each($replaces))
    {
      
#$word = eregi_replace($search,$replace,$word);
      
$word = preg_replace($search,$replace,trim($word));
      
#print $word;
    
}

    
// Step 1b
    
if($m > 0)
    {
      
$word = eregi_replace('eed$','ee',$word);
    }

    
$do_more = 0;
     if(
eregi('[aeiou]',eregi_replace('ed$','',$word)))

    {
      
$word = eregi_replace('ed$','',$word);
      
$do_more = 1;
    }
   
     if(
eregi('[aeiou]',eregi_replace('ing$','',$word))
)
    {
      
$word = eregi_replace('ing$','',$word);
      
$do_more = 1;
    }

    if(
$do_more)
    {
      
$more_replace = array(
                        
'at$' => 'ate',
                        
'bl$' => 'ble',
                        
'iz$' => 'ize');
      while(list(
$s,$r) = each($more_replace))
      {
        
$word = eregi_replace($s,$r,$word);
      }

      
$length = strlen($word);
      
$last_letter = substr($word,($length - 1),$length);
      
$next_last_letter = substr($word,(strlen($word) - 2),-1);
      if((
$last_letter == $next_last_letter) && !in_array($last_letter,array('l','s','z')))
      {
        
$word = substr($word,0,(strlen($word) - 1));
      }

      if(
$m == 1 && eregi('[b-df-hj-np-tv-z][aeiou][b-df-hj-np-tv-z]$',$word) && !in_array($last_letter,$o))
      {
        
$word .= 'e';
      }
    }

  
    
// Step 1c
    
if(eregi('[aeiou]',$word) && eregi('y$',$word) && $m == 1 && !eregi('ly$',$word))
    {
      
$word = eregi_replace('y$','i',$word);
    }

    
// Step 2
    
$replaces = array(
                  
'ational$' => 'ate',
                  
'tional$' => 'tion',
                  
'enci$' => 'ence',
                  
'anci$' => 'ance',
                  
'izer$' => 'ize',
                  
'abli$' => 'able',
                  
'alli$' => 'al',
                  
'entli$' => 'ent',
                  
'eli$' => 'e',
                  
'ousli$' => 'ous',
                  
'ization$' => 'ize',
                  
'ation$' => 'ate',
                  
'ator$' => 'ate',
                  
'alism$' => 'al',
                  
'iveness$' => 'ive',
                  
'fulness$' => 'ful',
                  
'ousness$' => 'ous',
                  
'aliti$' => 'al',
                  
'iviti$' => 'ive',
                  
'biliti$' => 'ble');

    while(list(
$search,$replace) = each($replaces))
    {
      if(
$m > 0)
      {
        
$word = eregi_replace($search,$replace,$word);
      }
    }

    
// Step 3
    
$replaces = array(
                  
'icate$' => 'ic',
                  
'ative$' => '',
                  
'alize$' => 'al',
                  
'iciti$' => 'ic',
                  
'ical$' => 'ic',
                  
'ful$' => '',
                  
'ness$' => '');

    while(list(
$search,$replace) = each($replaces))
    {
      if(
$m > 0)
      {
        
$word = eregi_replace($search,$replace,$word);
      }
    }

    
// Step 4a
    
$replaces = array(
                  
'al$' => '',
                  
'ance$' => '',
                  
'ence$' => '',
                  
'er$' => '',
                  
'ic$' => '',
                  
'able$' => '',
                  
'ible$' => '',
                  
'ant$' => '',
                  
'ement$' => '',
                  
'ment$' => '',
                  
'ent$' => '',
                  
'tion$' => 't',
                  
'sion$' => 's',
                  
'ou$' => '',
                  
'ism$' => '',
                  
'ate$' => '',
                  
'iti$' => '',
                  
'ous$' => '',
                  
'ive$' => '',
                  
'ize$' => '');

    while(list(
$search,$replace) = each($replaces))
    {
      if(
$m > 1)
      {
        
$word = eregi_replace($search,$replace,$word);
      }
    }

    
// Step 5a
    
if($m > 1 && ereg('e$',$word))
    {
      
$word = eregi_replace('e$','',$word);
    }

    if(
$m == 1 && !(eregi('[b-df-hj-np-tv-z][aeiou][b-df-hj-np-tv-z]$',$word) && !in_array($last_letter,$o)))
    {
      
$word = eregi_replace('e$','',$word);
    }

    
// Step 5b
    
if($m > 1)
    {
      
$word = eregi_replace('ll$','l',$word); 
    }

    
// This is NOT part of the original Porter Stemming Algorithm
    
if($m > 0)
    {
      
$word = eregi_replace('ly$','',$word);
    }
    return 
$word;
  }

__________________
Международное брачное агенство в Сибири.


Помещено RaZEr 09-06-2003 в 09:23:

Нет, для русского это работать не будет, хотя бы потому что в патернах нет русских букв и setlocale не стоит.

__________________
The time was broken ...


Помещено Tagore 09-06-2003 в 12:07:

RaZEr
А другое решение? Что бы он был чуток к оканчаниям и тому-подобной дребедени?!

__________________
Международное брачное агенство в Сибири.


Помещено RaZEr 09-06-2003 в 12:16:

Ð_усский язык несколько сложнее и богаче чем английский. Максимум что можно сделать так это искать неточные совпадения.

__________________
The time was broken ...


Помещено Tagore 09-06-2003 в 16:03:

RaZEr
А про этоможно где-то нормально почитать?

__________________
Международное брачное агенство в Сибири.


Помещено RaZEr 10-06-2003 в 09:34:

Может и можно, но врядли.

__________________
The time was broken ...


Формат времени GMT. Текущее время 12:16.
Показаны 6 сообщений на этой странице

Powered by: vBulletin Version 2.2.4
Copyright © Jelsoft Enterprises Limited 2000, 2001.