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

Source for file WordTag.php

Documentation is available at WordTag.php

  1. <?php
  2. /**
  3.  * ----------------------------------------------------------------------
  4.  *  
  5.  * Copyright (c) 2006-2012 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: Tagging Arabic Word Class
  31.  *  
  32.  * Filename: WordTag.php
  33.  *  
  34.  * Original  Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose:  Arabic grammarians describe Arabic as being derived from
  37.  *           three main categories: noun, verb and particle. This class
  38.  *           built to recognize the class of a given Arabic word.
  39.  *            
  40.  * ----------------------------------------------------------------------
  41.  *  
  42.  * Tagging Arabic Word
  43.  *
  44.  * This PHP Class can identifying names, places, dates, and other noun
  45.  * words and phrases in Arabic language that establish the meaning of a body
  46.  * of text.
  47.  * 
  48.  * This process of identifying names, places, dates, and other noun words and
  49.  * phrases that establish the meaning of a body of text-is critical to software
  50.  * systems that process large amounts of unstructured data coming from sources such
  51.  * as email, document files, and the Web.
  52.  * 
  53.  * Arabic words are classifies into three main classes, namely, verb, noun and
  54.  * particle. Verbs are sub classified into three subclasses (Past verbs, Present
  55.  * Verbs, etc.); nouns into forty six subclasses (e.g. Active participle, Passive
  56.  * participle, Exaggeration pattern, Adjectival noun, Adverbial noun, Infinitive
  57.  * noun, Common noun, Pronoun, Quantifier, etc.) and particles into twenty three
  58.  * subclasses (e.g. additional, resumption, Indefinite, Conditional, Conformational,
  59.  * Prohibition, Imperative, Optative, Reasonal, Dubious, etc.), and from these three
  60.  * main classes that the rest of the language is derived.
  61.  * 
  62.  * The most important aspect of this system of describing Arabic is that all the
  63.  * subclasses of these three main classes inherit properties from the parent
  64.  * classes.
  65.  * 
  66.  * Arabic is very rich in categorising words, and contains classes for almost every
  67.  * form of word imaginable. For example, there are classes for nouns of instruments,
  68.  * nouns of place and time, nouns of activity and so on. If we tried to use all the
  69.  * subclasses described by Arabic grammarians, the size of the tagset would soon
  70.  * reach more than two or three hundred tags. For this reason, we have chosen only
  71.  * the main classes. But because of the way all the classes inherit from others, it
  72.  * would be quite simple to extend this tagset to include more subclasses.
  73.  *
  74.  * Example:
  75.  * <code>
  76.  *     include('./I18N/Arabic.php');
  77.  *     $obj = new I18N_Arabic('WordTag');
  78.  * 
  79.  *     $hStr=$obj->highlightText($str,'#80B020');
  80.  * 
  81.  *     echo $str . '<hr />' . $hStr . '<hr />';
  82.  *     
  83.  *     $taggedText = $obj->tagText($str);
  84.  * 
  85.  *     foreach($taggedText as $wordTag) {
  86.  *         list($word, $tag) = $wordTag;
  87.  *     
  88.  *         if ($tag == 1) {
  89.  *             echo "<font color=#DBEC21>$word is Noun</font>, ";
  90.  *         }
  91.  *     
  92.  *         if ($tag == 0) {
  93.  *             echo "$word is not Noun, ";
  94.  *         }
  95.  *     }
  96.  * </code>
  97.  *    
  98.  * @category  I18N
  99.  * @package   I18N_Arabic
  100.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  101.  * @copyright 2006-2012 Khaled Al-Sham'aa
  102.  *    
  103.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  104.  * @link      http://www.ar-php.org
  105.  */
  106.  
  107. // New in PHP V5.3: Namespaces
  108. // namespace I18N\Arabic;
  109. // 
  110. // $obj = new I18N\Arabic\WordTag();
  111. // 
  112. // use I18N\Arabic;
  113. // $obj = new Arabic\WordTag();
  114. //
  115. // use I18N\Arabic\WordTag as WordTag;
  116. // $obj = new WordTag();
  117.  
  118. /**
  119.  * This PHP class to tagging Arabic Word
  120.  *  
  121.  * @category  I18N
  122.  * @package   I18N_Arabic
  123.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  124.  * @copyright 2006-2012 Khaled Al-Sham'aa
  125.  *    
  126.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  127.  * @link      http://www.ar-php.org
  128.  */ 
  129. {
  130.     private static $_particlePreNouns    array('عن''في''مذ''منذ''من'
  131.                                                   'الى''على''حتى''الا'
  132.                                                   'غير''سوى''خلا''عدا'
  133.                                                   'حاشا''ليس');
  134.     private static $_normalizeAlef       array('أ','إ','آ');
  135.     private static $_normalizeDiacritics array('َ','ً','ُ','ٌ','ِ','ٍ','ْ','ّ');
  136.  
  137.     /**
  138.      * Loads initialize values
  139.      *
  140.      * @ignore
  141.      */         
  142.     public function __construct()
  143.     {
  144.     }
  145.     
  146.     /**
  147.      * Check if given rabic word is noun or not
  148.      *      
  149.      * @param string $word       Word you want to check if it is
  150.      *                            noun (utf-8)
  151.      * @param string $word_befor The word before word you want to check
  152.      *                    
  153.      * @return boolean TRUE if given word is Arabic noun
  154.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  155.      */
  156.     public static function isNoun($word$word_befor)
  157.     {
  158.         $word       trim($word);
  159.         $word_befor trim($word_befor);
  160.  
  161.         $word       str_replace(self::$_normalizeAlef'ا'$word);
  162.         $word_befor str_replace(self::$_normalizeAlef'ا'$word_befor);
  163.         $wordLen    strlen($word);
  164.         
  165.         // إذا سبق بحرف جر فهو اسم مجرور
  166.         if (in_array($word_beforself::$_particlePreNouns)) {
  167.             return true;
  168.         }
  169.         
  170.         // إذا سبق بعدد فهو معدود
  171.         if (is_numeric($word|| is_numeric($word_befor)) {
  172.             return true;
  173.         }
  174.         
  175.         // إذا كان منون
  176.         if (mb_substr($word-11== 'ً' ||
  177.             mb_substr($word-11== 'ٌ' || 
  178.             mb_substr($word-11== 'ٍ'{
  179.             return true;
  180.         }
  181.         
  182.         $word    str_replace(self::$_normalizeDiacritics''$word);
  183.         $wordLen mb_strlen($word);
  184.         
  185.         // إن كان معرف بأل التعريف
  186.         if (mb_substr($word01== 'ا' && mb_substr($word11== 'ل' && $wordLen >= 5{
  187.             return true;
  188.         }
  189.         
  190.         // إذا كان في الكلمة  ثلاث ألفات
  191.         // إن لم تكن الألف الثالثة متطرفة
  192.         if (mb_substr_count($word'ا'>= 3{
  193.             return true;
  194.         }
  195.  
  196.         // إن كان مؤنث تأنيث لفظي، منتهي بتاء مربوطة
  197.         // أو همزة أو ألف مقصورة
  198.         if ((mb_substr($word-11== 'ة' || mb_substr($word-11== 'ء' || 
  199.              mb_substr($word-11== 'ى'&& $wordLen >= 4{
  200.             return true;
  201.         }
  202.  
  203.         // مؤنث تأنيث لفظي،
  204.         // منتهي بألف وتاء مفتوحة - جمع مؤنث سالم
  205.         if (mb_substr($word-11== 'ت' && mb_substr($word-21== 'ا' && 
  206.             $wordLen >= 5{
  207.             return true;
  208.         }
  209.  
  210.         // started by Noon, before REH or LAM, or Noon, is a verb and not a noun
  211.         if (mb_substr($word01== 'ن' && (mb_substr($word11== 'ر' || 
  212.             mb_substr($word11== 'ل' || mb_substr($word11== 'ن')
  213.             && $wordLen 3{
  214.             return false;
  215.         }
  216.         
  217.         // started by YEH, before some letters is a verb and not a noun
  218.         // YEH,THAL,JEEM,HAH,KHAH,ZAIN,SHEEN,SAD,DAD,TAH,ZAH,GHAIN,KAF
  219.         if (mb_substr($word01== 'ي' && (mb_strpos('يذجهخزشصضطظغك'mb_substr($word11)) !== false&& 
  220.             $wordLen 3{
  221.             return false;
  222.         }
  223.         
  224.         // started by beh or meem, before BEH,FEH,MEEM is a noun and not a verb
  225.         if ((mb_substr($word01== 'ب' || mb_substr($word01== 'م'&& 
  226.             (mb_substr($word11== 'ب' || mb_substr($word11== 'ف' || mb_substr($word11== 'م'&& 
  227.              $wordLen 3{
  228.             return true;
  229.         }
  230.         
  231.         // الكلمات التي  تنتهي بياء ونون
  232.         // أو ألف ونون أو ياء ونون
  233.         // تكون أسماء ما لم تبدأ بأحد حروف المضارعة 
  234.         if (preg_match('/^[^ايتن]\S{2}[اوي]ن$/u'$word)) {
  235.             return true;
  236.         }
  237.  
  238.         // إن كان على وزن اسم الآلة
  239.         // أو اسم المكان أو اسم الزمان
  240.         if (preg_match('/^م\S{3}$/u'$word|| 
  241.         preg_match('/^م\S{2}ا\S$/u'$word|| 
  242.             preg_match('/^م\S{3}ة$/u'$word|| 
  243.             preg_match('/^\S{2}ا\S$/u'$word|| 
  244.             preg_match('/^\Sا\Sو\S$/u'$word|| 
  245.             preg_match('/^\S{2}و\S$/u'$word|| 
  246.             preg_match('/^\S{2}ي\S$/u'$word|| 
  247.             preg_match('/^م\S{2}و\S$/u'$word|| 
  248.             preg_match('/^م\S{2}ي\S$/u'$word|| 
  249.             preg_match('/^\S{3}ة$/u'$word|| 
  250.             preg_match('/^\S{2}ا\Sة$/u'$word|| 
  251.             preg_match('/^\Sا\S{2}ة$/u'$word|| 
  252.             preg_match('/^\Sا\Sو\Sة$/u'$word|| 
  253.             preg_match('/^ا\S{2}و\Sة$/u'$word|| 
  254.             preg_match('/^ا\S{2}ي\S$/u'$word|| 
  255.             preg_match('/^ا\S{3}$/u'$word|| 
  256.             preg_match('/^\S{3}ى$/u'$word|| 
  257.             preg_match('/^\S{3}اء$/u'$word|| 
  258.             preg_match('/^\S{3}ان$/u'$word|| 
  259.             preg_match('/^م\Sا\S{2}$/u'$word|| 
  260.             preg_match('/^من\S{3}$/u'$word|| 
  261.             preg_match('/^مت\S{3}$/u'$word|| 
  262.             preg_match('/^مست\S{3}$/u'$word|| 
  263.             preg_match('/^م\Sت\S{2}$/u'$word|| 
  264.             preg_match('/^مت\Sا\S{2}$/u'$word|| 
  265.             preg_match('/^\Sا\S{2}$/u'$word)) {
  266.             return true;
  267.         }
  268.  
  269.         return false;
  270.     }
  271.     
  272.     /**
  273.      * Tag all words in a given Arabic string if they are nouns or not
  274.      *      
  275.      * @param string $str Arabic string you want to tag all its words
  276.      *                    
  277.      * @return array Two dimension array where item[i][0] represent the word i
  278.      *                in the given string, and item[i][1] is 1 if that word is
  279.      *                noun and 0 if it is not
  280.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  281.      */
  282.     public static function tagText($str)
  283.     {
  284.         $text     array();
  285.         $words    explode(' '$str);
  286.         $prevWord '';
  287.         
  288.         foreach ($words as $word{
  289.             if ($word == ''{
  290.                 continue;
  291.             }
  292.  
  293.             if (self::isNoun($word$prevWord)) {
  294.                 $text[array($word1);
  295.             else {
  296.                 $text[array($word0);
  297.             }
  298.             
  299.             $prevWord $word;
  300.         }
  301.  
  302.         return $text;
  303.     }
  304.     
  305.     /**
  306.      * Highlighted all nouns in a given Arabic string
  307.      *      
  308.      * @param string $str   Arabic string you want to highlighted
  309.      *                       all its nouns
  310.      * @param string $style Name of the CSS class you would like to apply
  311.      *                    
  312.      * @return string Arabic string in HTML format where all nouns highlighted
  313.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  314.      */
  315.     public static function highlightText($str$style null)
  316.     {
  317.         $html     '';
  318.         $prevTag  0;
  319.         $prevWord '';
  320.         
  321.         $taggedText self::tagText($str);
  322.         
  323.         foreach ($taggedText as $wordTag{
  324.             list($word$tag$wordTag;
  325.             
  326.             if ($prevTag == 1{
  327.                 if (in_array($wordself::$_particlePreNouns)) {
  328.                     $prevWord $word;
  329.                     continue;
  330.                 }
  331.                 
  332.                 if ($tag == 0{
  333.                     $html .= "</span> \r\n";
  334.                 }
  335.             else {
  336.                 if ($tag == 1{
  337.                     $html .= " \r\n<span class=\"" $style ."\">";
  338.                 }
  339.             }
  340.             
  341.             $html .= ' ' $prevWord ' ' $word;
  342.             
  343.             if ($prevWord != ''{
  344.                 $prevWord '';
  345.             }
  346.             $prevTag $tag;
  347.         }
  348.         
  349.         if ($prevTag == 1{
  350.             $html .= "</span> \r\n";
  351.         }
  352.         
  353.         return $html;
  354.     }
  355. }

Documentation generated on Wed, 29 Aug 2012 08:33:19 +0200 by phpDocumentor 1.4.0