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

Source for file Identifier.php

Documentation is available at Identifier.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: Identify Arabic Text Segments
  31.  *  
  32.  * Filename:   Identifier.php
  33.  *  
  34.  * Original    Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose:    This class will identify Arabic text in a given UTF-8 multi
  37.  *             language document, it will return array of start and end
  38.  *             positions for Arabic text segments.
  39.  *              
  40.  * ----------------------------------------------------------------------
  41.  *  
  42.  * Identify Arabic Text Segments
  43.  *
  44.  * Using this PHP Class you can fully automated approach to processing
  45.  * Arabic text by quickly and accurately determining Arabic text segments within
  46.  * multiple languages documents.
  47.  * 
  48.  * Understanding the language and encoding of a given document is an essential step
  49.  * in working with unstructured multilingual text. Without this basic knowledge,
  50.  * applications such as information retrieval and text mining cannot accurately
  51.  * process data and important information may be completely missed or mis-routed.
  52.  * 
  53.  * Any application that works with Arabic in multiple languages documents can
  54.  * benefit from the ArIdentifier class. Using this class, applications can take a
  55.  * fully automated approach to processing Arabic text by quickly and accurately
  56.  * determining Arabic text segments within multiple languages document.
  57.  *
  58.  * Example:
  59.  * <code>
  60.  *     include('./I18N/Arabic.php');
  61.  *     $obj = new I18N_Arabic('Identifier');
  62.  *     
  63.  *     $hStr=$obj->highlightText($str, '#80B020');
  64.  * 
  65.  *     echo $str . '<hr />' . $hStr . '<hr />';
  66.  *     
  67.  *     $taggedText = $obj->tagText($str);
  68.  * 
  69.  *     foreach($taggedText as $wordTag) {
  70.  *         list($word, $tag) = $wordTag;
  71.  *     
  72.  *         if ($tag == 1) {
  73.  *             echo "$word is Noun, ";
  74.  *         }
  75.  *     
  76.  *         if ($tag == 0) {
  77.  *             echo "$word is not Noun, ";
  78.  *         }
  79.  *     }
  80.  * </code>
  81.  *             
  82.  * @category  I18N
  83.  * @package   I18N_Arabic
  84.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  85.  * @copyright 2006-2013 Khaled Al-Sham'aa
  86.  *    
  87.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  88.  * @link      http://www.ar-php.org
  89.  */
  90.  
  91. // New in PHP V5.3: Namespaces
  92. // namespace I18N\Arabic;
  93. // 
  94. // $obj = new I18N\Arabic\Identifier();
  95. // 
  96. // use I18N\Arabic;
  97. // $obj = new Arabic\Identifier();
  98. //
  99. // use I18N\Arabic\Identifier as Identifier;
  100. // $obj = new Identifier();
  101.  
  102. /**
  103.  * This PHP class identify Arabic text segments
  104.  *  
  105.  * @category  I18N
  106.  * @package   I18N_Arabic
  107.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  108.  * @copyright 2006-2013 Khaled Al-Sham'aa
  109.  *    
  110.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  111.  * @link      http://www.ar-php.org
  112.  */ 
  113. {
  114.     /**
  115.      * Loads initialize values
  116.      *
  117.      * @ignore
  118.      */         
  119.     public function __construct()
  120.     {
  121.     }
  122.  
  123.     /**
  124.      * Identify Arabic text in a given UTF-8 multi language string
  125.      *          
  126.      * @param string $str UTF-8 multi language string
  127.      *      
  128.      * @return array Offset of the beginning and end of each Arabic segment in
  129.      *                sequence in the given UTF-8 multi language string
  130.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  131.      */
  132.     public static function identify($str)
  133.     {
  134.         $minAr  55436;
  135.         $maxAr  55698;
  136.         $probAr false;
  137.         $arFlag false;
  138.         $arRef  array();
  139.         $max    strlen($str);
  140.         
  141.         $i = -1;
  142.         while (++$i $max{
  143.             $cDec ord($str[$i]);
  144.             
  145.             // ignore ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 :
  146.             // If it come in the Arabic context   
  147.             if ($cDec >= 33 && $cDec <= 58
  148.                 continue
  149.             }
  150.             
  151.             if (!$probAr && ($cDec == 216 || $cDec == 217)) {
  152.                 $probAr true;
  153.                 continue;
  154.             }
  155.             
  156.             if ($i 0{
  157.                 $pDec ord($str[$i 1]);
  158.             else {
  159.                 $pDec null;
  160.             }
  161.             
  162.             if ($probAr{
  163.                 $utfDecCode ($pDec << 8$cDec;
  164.  
  165.                 if ($utfDecCode >= $minAr && $utfDecCode <= $maxAr{
  166.                     if (!$arFlag{
  167.                         $arFlag  true;
  168.                         $arRef[$i 1;
  169.                     }
  170.                 else {
  171.                     if ($arFlag{
  172.                         $arFlag  false;
  173.                         $arRef[$i 1;
  174.                     }
  175.                 }
  176.                 
  177.                 $probAr false;
  178.                 continue;
  179.             }
  180.             
  181.             if ($arFlag && !preg_match("/^\s$/"$str[$i])) {
  182.                 $arFlag  false;
  183.                 $arRef[$i;
  184.             }
  185.         }
  186.         
  187.         return $arRef;
  188.     }
  189.     
  190.     /**
  191.      * Find out if given string is Arabic text or not
  192.      *          
  193.      * @param string $str String
  194.      *                    
  195.      * @return boolean True if given string is UTF-8 Arabic, else will return False
  196.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  197.      */
  198.     public static function isArabic($str)
  199.     {
  200.         $isArabic false;
  201.         $arr      self::identify($str);
  202.         
  203.         if (count($arr== && $arr[0== 0{
  204.             $isArabic true;
  205.         }
  206.         
  207.         return $isArabic;
  208.     }
  209. }

Documentation generated on Mon, 14 Jan 2013 17:48:50 +0100 by phpDocumentor 1.4.0