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-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: 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-2013 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-2013 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== 'ً' || mb_substr($word-11== 'ٌ' 
  177.             || mb_substr($word-11== 'ٍ'
  178.         {
  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== 'ل' 
  187.             && $wordLen >= 5
  188.         {
  189.             return true;
  190.         }
  191.         
  192.         // إذا كان في الكلمة  ثلاث ألفات
  193.         // إن لم تكن الألف الثالثة متطرفة
  194.         if (mb_substr_count($word'ا'>= 3{
  195.             return true;
  196.         }
  197.  
  198.         //إن كان مؤنث تأنيث لفظي، منتهي بتاء مربوطة
  199.         // أو همزة أو ألف مقصورة
  200.         if ((mb_substr($word-11== 'ة' || mb_substr($word-11== 'ء' 
  201.             || mb_substr($word-11== 'ى'&& $wordLen >= 4
  202.         {
  203.             return true;
  204.         }
  205.  
  206.         // مؤنث تأنيث لفظي،
  207.         // منتهي بألف وتاء مفتوحة - جمع مؤنث سالم
  208.         if (mb_substr($word-11== 'ت' && mb_substr($word-21== 'ا' 
  209.             && $wordLen >= 5
  210.         {
  211.             return true;
  212.         }
  213.  
  214.         // started by Noon, before REH or LAM, or Noon, is a verb and not a noun
  215.         if (mb_substr($word01== 'ن' && (mb_substr($word11== 'ر' 
  216.             || mb_substr($word11== 'ل' || mb_substr($word11== 'ن'
  217.             && $wordLen 3
  218.         {
  219.             return false;
  220.         }
  221.         
  222.         // started by YEH, before some letters is a verb and not a noun
  223.         // YEH,THAL,JEEM,HAH,KHAH,ZAIN,SHEEN,SAD,DAD,TAH,ZAH,GHAIN,KAF
  224.         if (mb_substr($word01== 'ي' 
  225.             && (mb_strpos('يذجهخزشصضطظغك'mb_substr($word11)) !== false
  226.             && $wordLen 3
  227.         {
  228.             return false;
  229.         }
  230.         
  231.         // started by beh or meem, before BEH,FEH,MEEM is a noun and not a verb
  232.         if ((mb_substr($word01== 'ب' || mb_substr($word01== 'م'
  233.             && (mb_substr($word11== 'ب' || mb_substr($word11== 'ف' 
  234.             || mb_substr($word11== 'م'&& $wordLen 3
  235.         {
  236.             return true;
  237.         }
  238.         
  239.         // الكلمات التي  تنتهي بياء ونون
  240.         // أو ألف ونون أو ياء ونون
  241.         // تكون أسماء ما لم تبدأ بأحد حروف المضارعة 
  242.         if (preg_match('/^[^ايتن]\S{2}[اوي]ن$/u'$word)) {
  243.             return true;
  244.         }
  245.  
  246.         // إن كان على وزن اسم الآلة
  247.         // أو اسم المكان أو اسم الزمان
  248.         if (preg_match('/^م\S{3}$/u'$word
  249.             || preg_match('/^م\S{2}ا\S$/u'$word)  
  250.             || preg_match('/^م\S{3}ة$/u'$word)  
  251.             || preg_match('/^\S{2}ا\S$/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{2}و\S$/u'$word)  
  256.             || preg_match('/^م\S{2}ي\S$/u'$word)  
  257.             || preg_match('/^\S{3}ة$/u'$word
  258.             || preg_match('/^\S{2}ا\Sة$/u'$word)  
  259.             || preg_match('/^\Sا\S{2}ة$/u'$word)  
  260.             || preg_match('/^\Sا\Sو\Sة$/u'$word)  
  261.             || preg_match('/^ا\S{2}و\Sة$/u'$word)  
  262.             || preg_match('/^ا\S{2}ي\S$/u'$word
  263.             || preg_match('/^ا\S{3}$/u'$word)  
  264.             || preg_match('/^\S{3}ى$/u'$word)  
  265.             || preg_match('/^\S{3}اء$/u'$word)  
  266.             || preg_match('/^\S{3}ان$/u'$word)  
  267.             || preg_match('/^م\Sا\S{2}$/u'$word)  
  268.             || preg_match('/^من\S{3}$/u'$word)  
  269.             || preg_match('/^مت\S{3}$/u'$word)  
  270.             || preg_match('/^مست\S{3}$/u'$word)  
  271.             || preg_match('/^م\Sت\S{2}$/u'$word)  
  272.             || preg_match('/^مت\Sا\S{2}$/u'$word
  273.             || preg_match('/^\Sا\S{2}$/u'$word)
  274.         {
  275.             return true;
  276.         }
  277.  
  278.         return false;
  279.     }
  280.     
  281.     /**
  282.      * Tag all words in a given Arabic string if they are nouns or not
  283.      *      
  284.      * @param string $str Arabic string you want to tag all its words
  285.      *                    
  286.      * @return array Two dimension array where item[i][0] represent the word i
  287.      *                in the given string, and item[i][1] is 1 if that word is
  288.      *                noun and 0 if it is not
  289.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  290.      */
  291.     public static function tagText($str)
  292.     {
  293.         $text     array();
  294.         $words    explode(' '$str);
  295.         $prevWord '';
  296.         
  297.         foreach ($words as $word{
  298.             if ($word == ''{
  299.                 continue;
  300.             }
  301.  
  302.             if (self::isNoun($word$prevWord)) {
  303.                 $text[array($word1);
  304.             else {
  305.                 $text[array($word0);
  306.             }
  307.             
  308.             $prevWord $word;
  309.         }
  310.  
  311.         return $text;
  312.     }
  313.     
  314.     /**
  315.      * Highlighted all nouns in a given Arabic string
  316.      *      
  317.      * @param string $str   Arabic string you want to highlighted
  318.      *                       all its nouns
  319.      * @param string $style Name of the CSS class you would like to apply
  320.      *                    
  321.      * @return string Arabic string in HTML format where all nouns highlighted
  322.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  323.      */
  324.     public static function highlightText($str$style null)
  325.     {
  326.         $html     '';
  327.         $prevTag  0;
  328.         $prevWord '';
  329.         
  330.         $taggedText self::tagText($str);
  331.         
  332.         foreach ($taggedText as $wordTag{
  333.             list($word$tag$wordTag;
  334.             
  335.             if ($prevTag == 1{
  336.                 if (in_array($wordself::$_particlePreNouns)) {
  337.                     $prevWord $word;
  338.                     continue;
  339.                 }
  340.                 
  341.                 if ($tag == 0{
  342.                     $html .= "</span> \r\n";
  343.                 }
  344.             else {
  345.                 if ($tag == 1{
  346.                     $html .= " \r\n<span class=\"" $style ."\">";
  347.                 }
  348.             }
  349.             
  350.             $html .= ' ' $prevWord ' ' $word;
  351.             
  352.             if ($prevWord != ''{
  353.                 $prevWord '';
  354.             }
  355.             $prevTag $tag;
  356.         }
  357.         
  358.         if ($prevTag == 1{
  359.             $html .= "</span> \r\n";
  360.         }
  361.         
  362.         return $html;
  363.     }
  364. }

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