I18N_Arabic
[ class tree: I18N_Arabic ] [ index: I18N_Arabic ] [ all elements ]

Source for file Stemmer.php

Documentation is available at Stemmer.php

  1. <?php
  2. /**
  3.  * ----------------------------------------------------------------------
  4.  *  
  5.  * Copyright (c) 2006-2013 Khaled Al-Sham'aa.
  6.  *  
  7.  * http://www.ar-php.org
  8.  *  
  9.  * PHP Version 5
  10.  *  
  11.  * ----------------------------------------------------------------------
  12.  *  
  13.  * LICENSE
  14.  *
  15.  * This program is open source product; you can redistribute it and/or
  16.  * modify it under the terms of the GNU Lesser General Public License (LGPL)
  17.  * as published by the Free Software Foundation; either version 3
  18.  * of the License, or (at your option) any later version.
  19.  *  
  20.  * This program is distributed in the hope that it will be useful,
  21.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  * GNU Lesser General Public License for more details.
  24.  *  
  25.  * You should have received a copy of the GNU Lesser General Public License
  26.  * along with this program.  If not, see <http://www.gnu.org/licenses/lgpl.txt>.
  27.  *  
  28.  * ----------------------------------------------------------------------
  29.  *  
  30.  * Class Name: Arabic Text ArStemmer Class
  31.  *  
  32.  * Filename: Stemmer.php
  33.  *  
  34.  * Original  Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose:  Get stem of an Arabic word
  37.  *  
  38.  * ----------------------------------------------------------------------
  39.  *  
  40.  * Source: http://arabtechies.net/node/83
  41.  * By: Taha Zerrouki <taha.zerrouki@gmail.com>
  42.  *  
  43.  * ----------------------------------------------------------------------
  44.  *  
  45.  * Arabic Word Stemmer Class
  46.  *
  47.  * PHP class to get stem of an Arabic word
  48.  *
  49.  * A stemmer is an automatic process in which morphological variants of terms
  50.  * are mapped to a single representative string called a stem. Arabic belongs
  51.  * to the Semitic family of languages which also includes Hebrew and Aramaic.
  52.  * Since morphological change in Arabic results from the addition of prefixes
  53.  * and infixes as well as suffixes, simple removal of suffixes is not as
  54.  * effective for Arabic as it is for English.
  55.  * 
  56.  * Arabic has much richer morphology than English. Arabic has two genders,
  57.  * feminine and masculine; three numbers, singular, dual, and plural; and three
  58.  * grammatical cases, nominative, genitive, and accusative. A noun has the
  59.  * nominative case when it is a subject; accusative when it is the object of a
  60.  * verb; and genitive when it is the object of a preposition. The form of an
  61.  * Arabic noun is determined by its gender, number, and grammatical case. The
  62.  * definitive nouns are formed by attaching the Arabic article "AL" to the
  63.  * immediate front of the nouns. Besides prefixes, a noun can also carry a
  64.  * suffix which is often a possessive pronoun. In Arabic, the conjunction word
  65.  * "WA" (and) is often attached to the following word.
  66.  *  
  67.  * Like nouns, an Arabic adjective can also have many variants. When an
  68.  * adjective modifies a noun in a noun phrase, the adjective agrees with the
  69.  * noun in gender, number, case, and definiteness. Arabic verbs have two tenses:
  70.  * perfect and imperfect. Perfect tense denotes actions completed, while
  71.  * imperfect denotes uncompleted actions. The imperfect tense has four mood:
  72.  * indicative, subjective, jussive, and imperative. Arabic verbs in perfect
  73.  * tense consist of a stem and a subject marker. The subject marker indicates
  74.  * the person, gender, and number of the subject. The form of a verb in perfect
  75.  * tense can have subject marker and pronoun suffix. The form of a
  76.  * subject-marker is determined together by the person, gender, and number of
  77.  * the subject.
  78.  * Example:
  79.  * <code>
  80.  *     include('./I18N/Arabic.php');
  81.  *     $obj = new I18N_Arabic('Stemmer');
  82.  * 
  83.  *     echo $obj->stem($word);
  84.  * </code>
  85.  *  
  86.  * @category  I18N
  87.  * @package   I18N_Arabic
  88.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  89.  * @copyright 2006-2013 Khaled Al-Sham'aa
  90.  *    
  91.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  92.  * @link      http://www.ar-php.org
  93.  */
  94.  
  95. // New in PHP V5.3: Namespaces
  96. // namespace I18N\Arabic;
  97. // 
  98. // $obj = new I18N\Arabic\Stemmer();
  99. // 
  100. // use I18N\Arabic;
  101. // $obj = new Arabic\Stemmer();
  102. //
  103. // use I18N\Arabic\Stemmer as Stemmer;
  104. // $obj = new Stemmer();
  105.  
  106. /**
  107.  * This PHP class get stem of an Arabic word
  108.  *  
  109.  * @category  I18N
  110.  * @package   I18N_Arabic
  111.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  112.  * @copyright 2006-2013 Khaled Al-Sham'aa
  113.  *    
  114.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  115.  * @link      http://www.ar-php.org
  116.  */ 
  117. {
  118.     private static $_verbPre  'وأسفلي';
  119.     private static $_verbPost 'ومكانيه';
  120.     private static $_verbMay;
  121.  
  122.     private static $_verbMaxPre  4;
  123.     private static $_verbMaxPost 6;
  124.     private static $_verbMinStem 2;
  125.  
  126.     private static $_nounPre  'ابفكلوأ';
  127.     private static $_nounPost 'اتةكمنهوي';
  128.     private static $_nounMay;
  129.  
  130.     private static $_nounMaxPre  4;
  131.     private static $_nounMaxPost 6;
  132.     private static $_nounMinStem 2;
  133.     
  134.     /**
  135.      * Loads initialize values
  136.      *
  137.      * @ignore
  138.      */         
  139.     public function __construct()
  140.     {
  141.         self::$_verbMay self::$_verbPre self::$_verbPost;
  142.         self::$_nounMay self::$_nounPre self::$_nounPost;
  143.     }
  144.     
  145.     /**
  146.      * Get rough stem of the given Arabic word
  147.      *      
  148.      * @param string $word Arabic word you would like to get its stem
  149.      *                    
  150.      * @return string Arabic stem of the word
  151.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  152.      */
  153.     public static function stem($word)
  154.     {
  155.         $nounStem self::roughStem(
  156.             $wordself::$_nounMayself::$_nounPreself::$_nounPost
  157.             self::$_nounMaxPreself::$_nounMaxPostself::$_nounMinStem
  158.         );
  159.         $verbStem self::roughStem(
  160.             $wordself::$_verbMayself::$_verbPreself::$_verbPost
  161.             self::$_verbMaxPreself::$_verbMaxPostself::$_verbMinStem
  162.         );
  163.         
  164.         if (mb_strlen($nounStem'UTF-8'mb_strlen($verbStem'UTF-8')) {
  165.             $stem $nounStem;
  166.         else {
  167.             $stem $verbStem;
  168.         }
  169.         
  170.         return $stem;
  171.     }
  172.     
  173.     /**
  174.      * Get rough stem of the given Arabic word (under specific rules)
  175.      *      
  176.      * @param string  $word      Arabic word you would like to get its stem
  177.      * @param string  $notChars  Arabic chars those can't be in postfix or prefix
  178.      * @param string  $preChars  Arabic chars those may exists in the prefix
  179.      * @param string  $postChars Arabic chars those may exists in the postfix
  180.      * @param integer $maxPre    Max prefix length
  181.      * @param integer $maxPost   Max postfix length
  182.      * @param integer $minStem   Min stem length
  183.      *
  184.      * @return string Arabic stem of the word under giving rules
  185.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  186.      */
  187.     protected static function roughStem (
  188.         $word$notChars$preChars$postChars$maxPre$maxPost$minStem
  189.     {
  190.         $right = -1;
  191.         $left  = -1;
  192.         $max   mb_strlen($word'UTF-8');
  193.         
  194.         for ($i=0$i $max$i++{
  195.             if (mb_strpos($notCharsmb_substr($word$i1'UTF-8')0'UTF-8'=== false{
  196.                 if ($right == -1{
  197.                     $right $i;
  198.                 }
  199.                 $left $i;
  200.             }
  201.         }
  202.         
  203.         if ($right $maxPre{
  204.             $right $maxPre;
  205.         }
  206.         
  207.         if ($max $left $maxPost{
  208.             $left $max $maxPost -1;
  209.         }
  210.         
  211.         for ($i=0$i $right$i++{
  212.             if (mb_strpos($preCharsmb_substr($word$i1'UTF-8')0'UTF-8'=== false{
  213.                 $right $i;
  214.                 break;
  215.             }
  216.         }
  217.         
  218.         for ($i=$max-1$i>$left$i--{
  219.             if (mb_strpos($postCharsmb_substr($word$i1'UTF-8')0'UTF-8'=== false{
  220.                 $left $i;
  221.                 break;
  222.             }
  223.         }
  224.  
  225.         if ($left $right >= $minStem{
  226.             $stem mb_substr($word$right$left-$right+1'UTF-8');
  227.         else {
  228.             $stem null;
  229.         }
  230.  
  231.         return $stem;
  232.     }
  233. }

Documentation generated on Mon, 14 Jan 2013 17:49:07 +0100 by phpDocumentor 1.4.0