📜 ⬆️ ⬇️

Determining the part of the speech of a word in PHP with a single function

After reading the post http://toster.ru/2410/ , I wrote a function that determines their parts of speech from a string of words. Definition, of course not 100%, but you can easily modify.

The function returns an array of group values:


Example of function call:
print_r(chastrechiRUS('     ')); 

')
The result of the function (array):
 Array ( [0] => 8 [1] => 4 [2] => 3 [3] => 1 [4] => 2 [5] => 4 ) 



Function code:
 function chastrechiRUS($string){ /*  : 1.  2.  3.  4.  5.  6.  7.  8.  */ $groups = array( 1 => array ('','','','','','','','','','','','','','', '','','','','','','','','','','',''), 2 => array ('','','','','','','','','','','','',''), 3 => array ('','','','','','','','','','','','','','','', '','','','','','','','','','','','','','','','','','', '','','','','','','','','','','',''), 4 => array ('','','','','','','','','','','','','','','','','','','', '','','','','','','','','','','','','','','','', '', '', '','','','','',''), 5 => array ('', '', '', '', '', '', '', '', '','','','','','','','',''), 6 => array ('','','','','','','','','','','','','','','', '','','','','','','','','','','','','','',''), 7 => array ('','','','','','','','','','','', '','','','','','','','','','','','','','','', '','','','',''), 8 => array ('','','','') ); $res=array(); $string=mb_strtolower($string); $words=explode(' ',$string); //print_r($words); foreach ($words as $wk=>$w){ $len_w=mb_strlen($w); foreach ($groups as $gk=>$g){ foreach ($g as $part){ $len_part=mb_strlen($part); if ( mb_substr($w,-$len_part)==$part && $res[$wk][$gk]<$len_part //  ,  || mb_strpos($w,$part)>=(round(2*$len_w)/5) && $gk==2 //,  40%      || mb_substr($w,0,$len_part)==$part && $res[$wk][$gk]<$len_part && $gk==7 //,   || $w==$part //  ) { //echo $w.':'.$part."(".$gk.")<br>"; if ($w!=$part) $res[$wk][$gk]=mb_strlen($part); else $res[$wk][$gk]=99; } } } if (!isset($res[$wk][$gk])) $res[$wk][$gk]=0; //echo "<hr>"; } $result=array(); foreach($res as $r) { arsort($r); array_push($result,key($r)); } return $result; } 


UPDATED : 09/27/2012 - The function code has been updated. Transition to multibyte strings (mb_ *). Added new parts of speech. Added a significant part of the words in the array.

UPDATED : 09/29/2012 - Useful information on the Russian language for those who themselves write like this: here

Write your comments - we will refine together.

Source: https://habr.com/ru/post/152389/


All Articles