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

Source for file Normalise.php

Documentation is available at Normalise.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: Functions to normalise Arabic text.
  31.  *  
  32.  * Filename:   Normalise.php
  33.  *  
  34.  * Original    Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose:   Text normalisation through various stages. Also: unshaping.
  37.  *  
  38.  * ----------------------------------------------------------------------
  39.  *  
  40.  *  This class provides various functions to manipulate arabic text and
  41.  *  normalise it by applying filters, for example, to strip tatweel and
  42.  *  tashkeel, to normalise hamza and lamalephs, and to unshape
  43.  *  a joined Arabic text back into its normalised form.
  44.  *
  45.  *  There is also a function to reverse a utf8 string.
  46.  *
  47.  *  The functions are helpful for searching, indexing and similar
  48.  *  functions.
  49.  *
  50.  * Note that this class can only deal with UTF8 strings. You can use functions
  51.  * from the other classes to convert between encodings if necessary.
  52.  *
  53.  * Example:
  54.  * <code>
  55.  *     include('./I18N/Arabic.php');
  56.  *     $obj = new I18N_Arabic('Normalise');
  57.  * 
  58.  *     $str = "Arabic text with tatweel, tashkeel...";
  59.  * 
  60.  *     echo "<p><u><i>Before:</i></u><br />$str<br /><br />";
  61.  *     
  62.  *     $text = $obj->stripTatweel($str);
  63.  *        
  64.  *     echo "<u><i>After:</i></u><br />$text<br /><br />";
  65.  * </code>
  66.  *
  67.  * @category  I18N
  68.  * @package   I18N_Arabic
  69.  * @author    Djihed Afifi <djihed@gmail.com>
  70.  * @copyright 2006-2012 Khaled Al-Sham'aa
  71.  *    
  72.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  73.  * @link      http://www.ar-php.org
  74.  */
  75.  
  76. // New in PHP V5.3: Namespaces
  77. // namespace I18N\Arabic;
  78. // 
  79. // $obj = new I18N\Arabic\Normalise();
  80. // 
  81. // use I18N\Arabic;
  82. // $obj = new Arabic\Normalise();
  83. //
  84. // use I18N\Arabic\Normalise as Normalise;
  85. // $obj = new Normalise();
  86.  
  87. /**
  88.  *  This class provides various functions to manipulate arabic text and
  89.  *  normalise it by applying filters, for example, to strip tatweel and
  90.  *  tashkeel, to normalise hamza and lamalephs, and to unshape
  91.  *  a joined Arabic text back into its normalised form.
  92.  *
  93.  *  The functions are helpful for searching, indexing and similar
  94.  *  functions.
  95.  *  
  96.  * @category  I18N
  97.  * @package   I18N_Arabic
  98.  * @author    Djihed Afifi <djihed@gmail.com>
  99.  * @copyright 2006-2012 Khaled Al-Sham'aa
  100.  *    
  101.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  102.  * @link      http://www.ar-php.org
  103.  */ 
  104. {
  105.     private $_unshapeMap    array();
  106.     private $_unshapeKeys   array();
  107.     private $_unshapeValues array();
  108.     private $_chars         array();
  109.     private $_charGroups    array();
  110.     private $_charArNames   array();
  111.  
  112.      /**
  113.       * Load the Unicode constants that will be used ibn substitutions
  114.       * and normalisations.
  115.       *
  116.       * @ignore
  117.       */
  118.     public function __construct(
  119.     {
  120.         include dirname(__FILE__'/data/charset/ArUnicode.constants.php';
  121.  
  122.         $this->_unshapeMap    $ligature_map;
  123.         $this->_unshapeKeys   array_keys($this->_unshapeMap);
  124.         $this->_unshapeValues array_values($this->_unshapeMap);
  125.         $this->_chars         $char_names;
  126.         $this->_charGroups    $char_groups;
  127.         $this->_charArNames   $char_ar_names;
  128.     }    
  129.  
  130.     /**
  131.      * Strip all tatweel characters from an Arabic text.
  132.      * 
  133.      * @param string $text The text to be stripped.
  134.      *      
  135.      * @return string the stripped text.
  136.      * @author Djihed Afifi <djihed@gmail.com>
  137.      */ 
  138.     public function stripTatweel($text
  139.     {
  140.         return str_replace($this->_chars['TATWEEL']''$text)
  141.     }
  142.  
  143.     /**
  144.      * Strip all tashkeel characters from an Arabic text.
  145.      * 
  146.      * @param string $text The text to be stripped.
  147.      *      
  148.      * @return string the stripped text.
  149.      * @author Djihed Afifi <djihed@gmail.com>
  150.      */ 
  151.     public function stripTashkeel($text
  152.     {
  153.         $tashkeel array(
  154.              $this->_chars['FATHATAN']
  155.              $this->_chars['DAMMATAN']
  156.              $this->_chars['KASRATAN']
  157.              $this->_chars['FATHA']
  158.              $this->_chars['DAMMA']
  159.              $this->_chars['KASRA'],
  160.              $this->_chars['SUKUN'],
  161.              $this->_chars['SHADDA']
  162.         );
  163.         return str_replace($tashkeel""$text);
  164.     }
  165.  
  166.     /**
  167.      * Normalise all Hamza characters to their corresponding aleph
  168.      * character in an Arabic text.
  169.      *
  170.      * @param string $text The text to be normalised.
  171.      *      
  172.      * @return string the normalised text.
  173.      * @author Djihed Afifi <djihed@gmail.com>
  174.      */ 
  175.     public function normaliseHamza($text
  176.     {
  177.         $replace array(
  178.              $this->_chars['WAW_HAMZA'$this->_chars['WAW'],
  179.              $this->_chars['YEH_HAMZA'$this->_chars['YEH'],
  180.         );
  181.         $alephs array(
  182.              $this->_chars['ALEF_MADDA'],
  183.              $this->_chars['ALEF_HAMZA_ABOVE'],
  184.              $this->_chars['ALEF_HAMZA_BELOW'],
  185.              $this->_chars['HAMZA_ABOVE,HAMZA_BELOW']
  186.         );
  187.  
  188.         $text str_replace(array_keys($replace)array_values($replace)$text);
  189.         $text str_replace($alephs$this->_chars['ALEF']$text);
  190.         return $text;
  191.     }
  192.  
  193.     /**
  194.      * Unicode uses some special characters where the lamaleph and any
  195.      * hamza above them are combined into one code point. Some input
  196.      * system use them. This function expands these characters.
  197.      *
  198.      * @param string $text The text to be normalised.
  199.      *      
  200.      * @return string the normalised text.
  201.      * @author Djihed Afifi <djihed@gmail.com>
  202.      */ 
  203.     public function normaliseLamaleph ($text
  204.     {
  205.         $text str_replace($this->_chars['LAM_ALEPH']$simple_LAM_ALEPH$text);
  206.         $text str_replace($this->_chars['LAM_ALEPH_HAMZA_ABOVE']
  207.                             $simple_LAM_ALEPH_HAMZA_ABOVE$text);
  208.         $text str_replace($this->_chars['LAM_ALEPH_HAMZA_BELOW']
  209.                             $simple_LAM_ALEPH_HAMZA_BELOW$text);
  210.         $text str_replace($this->_chars['LAM_ALEPH_MADDA_ABOVE']
  211.                             $simple_LAM_ALEPH_MADDA_ABOVE$text);
  212.         return $text;
  213.     }
  214.  
  215.     /**
  216.      * Return unicode char by its code point.
  217.      *
  218.      * @param char $u code point
  219.      *      
  220.      * @return string the result character.
  221.      * @author Djihed Afifi <djihed@gmail.com>
  222.      */
  223.     public function unichr($u
  224.     {
  225.         return mb_convert_encoding('&#'.intval($u).';''UTF-8''HTML-ENTITIES');
  226.     }
  227.  
  228.     /**
  229.      * Takes a string, it applies the various filters in this class
  230.      * to return a unicode normalised string suitable for activities
  231.      * such as searching, indexing, etc.
  232.      *
  233.      * @param string $text the text to be normalised.
  234.      *      
  235.      * @return string the result normalised string.
  236.      * @author Djihed Afifi <djihed@gmail.com>
  237.      */ 
  238.     public function normalise($text)
  239.     {
  240.         $text $this->stripTashkeel($text);
  241.         $text $this->stripTatweel($text);
  242.         $text $this->normaliseHamza($text);
  243.         $text $this->normaliseLamaleph($text);
  244.  
  245.         return $text;
  246.     
  247.  
  248.     /**
  249.      * Takes Arabic text in its joined form, it untangles the characters
  250.      * and  unshapes them.
  251.      *
  252.      * This can be used to process text that was processed through OCR
  253.      * or by extracting text from a PDF document.
  254.      *
  255.      * Note that the result text may need further processing. In most
  256.      * cases, you will want to use the utf8Strrev function from
  257.      * this class to reverse the string.
  258.      *  
  259.      * Most of the work of setting up the characters for this function
  260.      * is done through the ArUnicode.constants.php constants and
  261.      * the constructor loading.
  262.      *
  263.      * @param string $text the text to be unshaped.
  264.      *      
  265.      * @return string the result normalised string.
  266.      * @author Djihed Afifi <djihed@gmail.com>
  267.      */
  268.     public function unshape($text)
  269.     {
  270.           return str_replace($this->_unshapeKeys$this->_unshapeValues$text);
  271.     }
  272.  
  273.     /**
  274.      * Take a UTF8 string and reverse it.
  275.      *
  276.      * @param string  $str             the string to be reversed.
  277.      * @param boolean $reverse_numbers whether to reverse numbers.
  278.      *      
  279.      * @return string The reversed string.
  280.      */
  281.     public function utf8Strrev($str$reverse_numbers false
  282.     {
  283.         preg_match_all('/./us'$str$ar);
  284.         if ($reverse_numbers{
  285.             return join(''array_reverse($ar[0]));
  286.         else {
  287.             $temp array();
  288.             foreach ($ar[0as $value{
  289.                 if (is_numeric($value&& !empty($temp[0]&& is_numeric($temp[0])) {
  290.                     foreach ($temp as $key => $value2{
  291.                         if (is_numeric($value2)) {
  292.                             $pos ($key 1);
  293.                         else {
  294.                             break;
  295.                         }
  296.                     }
  297.                     $temp2 array_splice($temp$pos);
  298.                     $temp  array_merge($temparray($value)$temp2);
  299.                 else {
  300.                     array_unshift($temp$value);
  301.                 }
  302.             }
  303.             return implode(''$temp);
  304.         }
  305.     }
  306.     
  307.     /**
  308.      * Checks for Arabic Tashkeel marks (i.e. FATHA, DAMMA, KASRA, SUKUN, SHADDA, FATHATAN, DAMMATAN, KASRATAN).
  309.      *
  310.      * @param string $archar Arabic unicode char
  311.      *      
  312.      * @return boolean True if it is Arabic Tashkeel mark
  313.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  314.      */
  315.     public function isTashkeel($archar)
  316.     {
  317.         $key array_search($archar$this->_chars);
  318.  
  319.         if (in_array($key$this->_charGroups['TASHKEEL'])) {
  320.             $value true;
  321.         else {
  322.             $value false;
  323.         }
  324.         
  325.         return $value;
  326.     }
  327.     
  328.     /**
  329.      * Checks for Arabic Harakat marks (i.e. FATHA, DAMMA, KASRA, SUKUN, TANWIN).
  330.      *
  331.      * @param string $archar Arabic unicode char
  332.      *      
  333.      * @return boolean True if it is Arabic Harakat mark
  334.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  335.      */
  336.     public function isHaraka($archar)
  337.     {
  338.         $key array_search($archar$this->_chars);
  339.  
  340.         if (in_array($key$this->_charGroups['HARAKAT'])) {
  341.             $value true;
  342.         else {
  343.             $value false;
  344.         }
  345.         
  346.         return $value;
  347.     }
  348.     
  349.     /**
  350.      * Checks for Arabic short Harakat marks (i.e. FATHA, DAMMA, KASRA, SUKUN).
  351.      *
  352.      * @param string $archar Arabic unicode char
  353.      *      
  354.      * @return boolean True if it is Arabic short Harakat mark
  355.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  356.      */
  357.     public function isShortharaka($archar)
  358.     {
  359.         $key array_search($archar$this->_chars);
  360.  
  361.         if (in_array($key$this->_charGroups['SHORTHARAKAT'])) {
  362.             $value true;
  363.         else {
  364.             $value false;
  365.         }
  366.         
  367.         return $value;
  368.     }
  369.     
  370.     /**
  371.      * Checks for Arabic Tanwin marks (i.e. FATHATAN, DAMMATAN, KASRATAN).
  372.      *
  373.      * @param string $archar Arabic unicode char
  374.      *      
  375.      * @return boolean True if it is Arabic Tanwin mark
  376.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  377.      */
  378.     public function isTanwin($archar)
  379.     {
  380.         $key array_search($archar$this->_chars);
  381.  
  382.         if (in_array($key$this->_charGroups['TANWIN'])) {
  383.             $value true;
  384.         else {
  385.             $value false;
  386.         }
  387.         
  388.         return $value;
  389.     }
  390.     
  391.     /**
  392.      * Checks for Arabic Ligatures like LamAlef (i.e. LAM ALEF, LAM ALEF HAMZA ABOVE, LAM ALEF HAMZA BELOW, LAM ALEF MADDA ABOVE).
  393.      *
  394.      * @param string $archar Arabic unicode char
  395.      *      
  396.      * @return boolean True if it is Arabic Ligatures like LamAlef
  397.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  398.      */
  399.     public function isLigature($archar)
  400.     {
  401.         $key array_search($archar$this->_chars);
  402.  
  403.         if (in_array($key$this->_charGroups['LIGUATURES'])) {
  404.             $value true;
  405.         else {
  406.             $value false;
  407.         }
  408.         
  409.         return $value;
  410.     }
  411.     
  412.     /**
  413.      * Checks for Arabic Hamza forms (i.e. HAMZA, WAW HAMZA, YEH HAMZA, HAMZA ABOVE, HAMZA BELOW, ALEF HAMZA BELOW, ALEF HAMZA ABOVE).
  414.      *
  415.      * @param string $archar Arabic unicode char
  416.      *      
  417.      * @return boolean True if it is Arabic Hamza form
  418.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  419.      */
  420.     public function isHamza($archar)
  421.     {
  422.         $key array_search($archar$this->_chars);
  423.  
  424.         if (in_array($key$this->_charGroups['HAMZAT'])) {
  425.             $value true;
  426.         else {
  427.             $value false;
  428.         }
  429.         
  430.         return $value;
  431.     }
  432.     
  433.     /**
  434.      * Checks for Arabic Alef forms (i.e. ALEF, ALEF MADDA, ALEF HAMZA ABOVE, ALEF HAMZA BELOW,ALEF WASLA, ALEF MAKSURA).
  435.      *
  436.      * @param string $archar Arabic unicode char
  437.      *      
  438.      * @return boolean True if it is Arabic Alef form
  439.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  440.      */
  441.     public function isAlef($archar)
  442.     {
  443.         $key array_search($archar$this->_chars);
  444.  
  445.         if (in_array($key$this->_charGroups['ALEFAT'])) {
  446.             $value true;
  447.         else {
  448.             $value false;
  449.         }
  450.         
  451.         return $value;
  452.     }
  453.     
  454.     /**
  455.      * Checks for Arabic Weak letters (i.e. ALEF, WAW, YEH, ALEF_MAKSURA).
  456.      *
  457.      * @param string $archar Arabic unicode char
  458.      *      
  459.      * @return boolean True if it is Arabic Weak letter
  460.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  461.      */
  462.     public function isWeak($archar)
  463.     {
  464.         $key array_search($archar$this->_chars);
  465.  
  466.         if (in_array($key$this->_charGroups['WEAK'])) {
  467.             $value true;
  468.         else {
  469.             $value false;
  470.         }
  471.         
  472.         return $value;
  473.     }
  474.     
  475.     /**
  476.      * Checks for Arabic Yeh forms (i.e. YEH, YEH HAMZA, SMALL YEH, ALEF MAKSURA).
  477.      *
  478.      * @param string $archar Arabic unicode char
  479.      *      
  480.      * @return boolean True if it is Arabic Yeh form
  481.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  482.      */
  483.     public function isYehlike($archar)
  484.     {
  485.         $key array_search($archar$this->_chars);
  486.  
  487.         if (in_array($key$this->_charGroups['YEHLIKE'])) {
  488.             $value true;
  489.         else {
  490.             $value false;
  491.         }
  492.         
  493.         return $value;
  494.     }
  495.     
  496.     /**
  497.      * Checks for Arabic Waw like forms (i.e. WAW, WAW HAMZA, SMALL WAW).
  498.      *
  499.      * @param string $archar Arabic unicode char
  500.      *      
  501.      * @return boolean True if it is Arabic Waw like form
  502.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  503.      */
  504.     public function isWawlike($archar)
  505.     {
  506.         $key array_search($archar$this->_chars);
  507.  
  508.         if (in_array($key$this->_charGroups['WAWLIKE'])) {
  509.             $value true;
  510.         else {
  511.             $value false;
  512.         }
  513.         
  514.         return $value;
  515.     }
  516.     
  517.     /**
  518.      * Checks for Arabic Teh forms (i.e. TEH, TEH MARBUTA).
  519.      *
  520.      * @param string $archar Arabic unicode char
  521.      *      
  522.      * @return boolean True if it is Arabic Teh form
  523.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  524.      */
  525.     public function isTehlike($archar)
  526.     {
  527.         $key array_search($archar$this->_chars);
  528.  
  529.         if (in_array($key$this->_charGroups['TEHLIKE'])) {
  530.             $value true;
  531.         else {
  532.             $value false;
  533.         }
  534.         
  535.         return $value;
  536.     }
  537.     
  538.     /**
  539.      * Checks for Arabic Small letters (i.e. SMALL ALEF, SMALL WAW, SMALL YEH).
  540.      *
  541.      * @param string $archar Arabic unicode char
  542.      *      
  543.      * @return boolean True if it is Arabic Small letter
  544.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  545.      */
  546.     public function isSmall($archar)
  547.     {
  548.         $key array_search($archar$this->_chars);
  549.  
  550.         if (in_array($key$this->_charGroups['SMALL'])) {
  551.             $value true;
  552.         else {
  553.             $value false;
  554.         }
  555.         
  556.         return $value;
  557.     }
  558.     
  559.     /**
  560.      * Checks for Arabic Moon letters.
  561.      *
  562.      * @param string $archar Arabic unicode char
  563.      *      
  564.      * @return boolean True if it is Arabic Moon letter
  565.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  566.      */
  567.     public function isMoon($archar)
  568.     {
  569.         $key array_search($archar$this->_chars);
  570.  
  571.         if (in_array($key$this->_charGroups['MOON'])) {
  572.             $value true;
  573.         else {
  574.             $value false;
  575.         }
  576.         
  577.         return $value;
  578.     }
  579.     
  580.     /**
  581.      * Checks for Arabic Sun letters.
  582.      *
  583.      * @param string $archar Arabic unicode char
  584.      *      
  585.      * @return boolean True if it is Arabic Sun letter
  586.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  587.      */
  588.     public function isSun($archar)
  589.     {
  590.         $key array_search($archar$this->_chars);
  591.  
  592.         if (in_array($key$this->_charGroups['SUN'])) {
  593.             $value true;
  594.         else {
  595.             $value false;
  596.         }
  597.         
  598.         return $value;
  599.     }
  600.     
  601.     /**
  602.      * Return Arabic letter name in arabic.
  603.      *
  604.      * @param string $archar Arabic unicode char
  605.      *      
  606.      * @return string Arabic letter name in arabic
  607.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  608.      */
  609.     public function charName($archar)
  610.     {
  611.         $key array_search($archar$this->_chars);
  612.  
  613.         $name $this->_charArNames["$key"];
  614.         
  615.         return $name;
  616.     }
  617. }

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