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-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: 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-2013 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-2013 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(
  206.             $this->_chars['LAM_ALEPH']
  207.             $simple_LAM_ALEPH
  208.             $text
  209.         );
  210.         $text str_replace(
  211.             $this->_chars['LAM_ALEPH_HAMZA_ABOVE']
  212.             $simple_LAM_ALEPH_HAMZA_ABOVE
  213.             $text
  214.         );
  215.         $text str_replace(
  216.             $this->_chars['LAM_ALEPH_HAMZA_BELOW']
  217.             $simple_LAM_ALEPH_HAMZA_BELOW
  218.             $text
  219.         );
  220.         $text str_replace(
  221.             $this->_chars['LAM_ALEPH_MADDA_ABOVE']
  222.             $simple_LAM_ALEPH_MADDA_ABOVE
  223.             $text
  224.         );
  225.         return $text;
  226.     }
  227.  
  228.     /**
  229.      * Return unicode char by its code point.
  230.      *
  231.      * @param char $u code point
  232.      *      
  233.      * @return string the result character.
  234.      * @author Djihed Afifi <djihed@gmail.com>
  235.      */
  236.     public function unichr($u
  237.     {
  238.         return mb_convert_encoding('&#'.intval($u).';''UTF-8''HTML-ENTITIES');
  239.     }
  240.  
  241.     /**
  242.      * Takes a string, it applies the various filters in this class
  243.      * to return a unicode normalised string suitable for activities
  244.      * such as searching, indexing, etc.
  245.      *
  246.      * @param string $text the text to be normalised.
  247.      *      
  248.      * @return string the result normalised string.
  249.      * @author Djihed Afifi <djihed@gmail.com>
  250.      */ 
  251.     public function normalise($text)
  252.     {
  253.         $text $this->stripTashkeel($text);
  254.         $text $this->stripTatweel($text);
  255.         $text $this->normaliseHamza($text);
  256.         $text $this->normaliseLamaleph($text);
  257.  
  258.         return $text;
  259.     
  260.  
  261.     /**
  262.      * Takes Arabic text in its joined form, it untangles the characters
  263.      * and  unshapes them.
  264.      *
  265.      * This can be used to process text that was processed through OCR
  266.      * or by extracting text from a PDF document.
  267.      *
  268.      * Note that the result text may need further processing. In most
  269.      * cases, you will want to use the utf8Strrev function from
  270.      * this class to reverse the string.
  271.      *  
  272.      * Most of the work of setting up the characters for this function
  273.      * is done through the ArUnicode.constants.php constants and
  274.      * the constructor loading.
  275.      *
  276.      * @param string $text the text to be unshaped.
  277.      *      
  278.      * @return string the result normalised string.
  279.      * @author Djihed Afifi <djihed@gmail.com>
  280.      */
  281.     public function unshape($text)
  282.     {
  283.           return str_replace($this->_unshapeKeys$this->_unshapeValues$text);
  284.     }
  285.  
  286.     /**
  287.      * Take a UTF8 string and reverse it.
  288.      *
  289.      * @param string  $str             the string to be reversed.
  290.      * @param boolean $reverse_numbers whether to reverse numbers.
  291.      *      
  292.      * @return string The reversed string.
  293.      */
  294.     public function utf8Strrev($str$reverse_numbers false
  295.     {
  296.         preg_match_all('/./us'$str$ar);
  297.         if ($reverse_numbers{
  298.             return join(''array_reverse($ar[0]));
  299.         else {
  300.             $temp array();
  301.             foreach ($ar[0as $value{
  302.                 if (is_numeric($value&& !empty($temp[0]&& is_numeric($temp[0])) {
  303.                     foreach ($temp as $key => $value2{
  304.                         if (is_numeric($value2)) {
  305.                             $pos ($key 1);
  306.                         else {
  307.                             break;
  308.                         }
  309.                     }
  310.                     $temp2 array_splice($temp$pos);
  311.                     $temp  array_merge($temparray($value)$temp2);
  312.                 else {
  313.                     array_unshift($temp$value);
  314.                 }
  315.             }
  316.             return implode(''$temp);
  317.         }
  318.     }
  319.     
  320.     /**
  321.      * Checks for Arabic Tashkeel marks (i.e. FATHA, DAMMA, KASRA, SUKUN,
  322.      * SHADDA, FATHATAN, DAMMATAN, KASRATAN).
  323.      *
  324.      * @param string $archar Arabic unicode char
  325.      *      
  326.      * @return boolean True if it is Arabic Tashkeel mark
  327.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  328.      */
  329.     public function isTashkeel($archar)
  330.     {
  331.         $key array_search($archar$this->_chars);
  332.  
  333.         if (in_array($key$this->_charGroups['TASHKEEL'])) {
  334.             $value true;
  335.         else {
  336.             $value false;
  337.         }
  338.         
  339.         return $value;
  340.     }
  341.     
  342.     /**
  343.      * Checks for Arabic Harakat marks (i.e. FATHA, DAMMA, KASRA, SUKUN, TANWIN).
  344.      *
  345.      * @param string $archar Arabic unicode char
  346.      *      
  347.      * @return boolean True if it is Arabic Harakat mark
  348.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  349.      */
  350.     public function isHaraka($archar)
  351.     {
  352.         $key array_search($archar$this->_chars);
  353.  
  354.         if (in_array($key$this->_charGroups['HARAKAT'])) {
  355.             $value true;
  356.         else {
  357.             $value false;
  358.         }
  359.         
  360.         return $value;
  361.     }
  362.     
  363.     /**
  364.      * Checks for Arabic short Harakat marks (i.e. FATHA, DAMMA, KASRA, SUKUN).
  365.      *
  366.      * @param string $archar Arabic unicode char
  367.      *      
  368.      * @return boolean True if it is Arabic short Harakat mark
  369.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  370.      */
  371.     public function isShortharaka($archar)
  372.     {
  373.         $key array_search($archar$this->_chars);
  374.  
  375.         if (in_array($key$this->_charGroups['SHORTHARAKAT'])) {
  376.             $value true;
  377.         else {
  378.             $value false;
  379.         }
  380.         
  381.         return $value;
  382.     }
  383.     
  384.     /**
  385.      * Checks for Arabic Tanwin marks (i.e. FATHATAN, DAMMATAN, KASRATAN).
  386.      *
  387.      * @param string $archar Arabic unicode char
  388.      *      
  389.      * @return boolean True if it is Arabic Tanwin mark
  390.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  391.      */
  392.     public function isTanwin($archar)
  393.     {
  394.         $key array_search($archar$this->_chars);
  395.  
  396.         if (in_array($key$this->_charGroups['TANWIN'])) {
  397.             $value true;
  398.         else {
  399.             $value false;
  400.         }
  401.         
  402.         return $value;
  403.     }
  404.     
  405.     /**
  406.      * Checks for Arabic Ligatures like LamAlef (i.e. LAM ALEF, LAM ALEF HAMZA
  407.      * ABOVE, LAM ALEF HAMZA BELOW, LAM ALEF MADDA ABOVE).
  408.      *
  409.      * @param string $archar Arabic unicode char
  410.      *      
  411.      * @return boolean True if it is Arabic Ligatures like LamAlef
  412.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  413.      */
  414.     public function isLigature($archar)
  415.     {
  416.         $key array_search($archar$this->_chars);
  417.  
  418.         if (in_array($key$this->_charGroups['LIGUATURES'])) {
  419.             $value true;
  420.         else {
  421.             $value false;
  422.         }
  423.         
  424.         return $value;
  425.     }
  426.     
  427.     /**
  428.      * Checks for Arabic Hamza forms (i.e. HAMZA, WAW HAMZA, YEH HAMZA, HAMZA ABOVE,
  429.      * HAMZA BELOW, ALEF HAMZA BELOW, ALEF HAMZA ABOVE).
  430.      *
  431.      * @param string $archar Arabic unicode char
  432.      *      
  433.      * @return boolean True if it is Arabic Hamza form
  434.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  435.      */
  436.     public function isHamza($archar)
  437.     {
  438.         $key array_search($archar$this->_chars);
  439.  
  440.         if (in_array($key$this->_charGroups['HAMZAT'])) {
  441.             $value true;
  442.         else {
  443.             $value false;
  444.         }
  445.         
  446.         return $value;
  447.     }
  448.     
  449.     /**
  450.      * Checks for Arabic Alef forms (i.e. ALEF, ALEF MADDA, ALEF HAMZA ABOVE,
  451.      * ALEF HAMZA BELOW,ALEF WASLA, ALEF MAKSURA).
  452.      *
  453.      * @param string $archar Arabic unicode char
  454.      *      
  455.      * @return boolean True if it is Arabic Alef form
  456.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  457.      */
  458.     public function isAlef($archar)
  459.     {
  460.         $key array_search($archar$this->_chars);
  461.  
  462.         if (in_array($key$this->_charGroups['ALEFAT'])) {
  463.             $value true;
  464.         else {
  465.             $value false;
  466.         }
  467.         
  468.         return $value;
  469.     }
  470.     
  471.     /**
  472.      * Checks for Arabic Weak letters (i.e. ALEF, WAW, YEH, ALEF_MAKSURA).
  473.      *
  474.      * @param string $archar Arabic unicode char
  475.      *      
  476.      * @return boolean True if it is Arabic Weak letter
  477.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  478.      */
  479.     public function isWeak($archar)
  480.     {
  481.         $key array_search($archar$this->_chars);
  482.  
  483.         if (in_array($key$this->_charGroups['WEAK'])) {
  484.             $value true;
  485.         else {
  486.             $value false;
  487.         }
  488.         
  489.         return $value;
  490.     }
  491.     
  492.     /**
  493.      * Checks for Arabic Yeh forms (i.e. YEH, YEH HAMZA, SMALL YEH, ALEF MAKSURA).
  494.      *
  495.      * @param string $archar Arabic unicode char
  496.      *      
  497.      * @return boolean True if it is Arabic Yeh form
  498.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  499.      */
  500.     public function isYehlike($archar)
  501.     {
  502.         $key array_search($archar$this->_chars);
  503.  
  504.         if (in_array($key$this->_charGroups['YEHLIKE'])) {
  505.             $value true;
  506.         else {
  507.             $value false;
  508.         }
  509.         
  510.         return $value;
  511.     }
  512.     
  513.     /**
  514.      * Checks for Arabic Waw like forms (i.e. WAW, WAW HAMZA, SMALL WAW).
  515.      *
  516.      * @param string $archar Arabic unicode char
  517.      *      
  518.      * @return boolean True if it is Arabic Waw like form
  519.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  520.      */
  521.     public function isWawlike($archar)
  522.     {
  523.         $key array_search($archar$this->_chars);
  524.  
  525.         if (in_array($key$this->_charGroups['WAWLIKE'])) {
  526.             $value true;
  527.         else {
  528.             $value false;
  529.         }
  530.         
  531.         return $value;
  532.     }
  533.     
  534.     /**
  535.      * Checks for Arabic Teh forms (i.e. TEH, TEH MARBUTA).
  536.      *
  537.      * @param string $archar Arabic unicode char
  538.      *      
  539.      * @return boolean True if it is Arabic Teh form
  540.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  541.      */
  542.     public function isTehlike($archar)
  543.     {
  544.         $key array_search($archar$this->_chars);
  545.  
  546.         if (in_array($key$this->_charGroups['TEHLIKE'])) {
  547.             $value true;
  548.         else {
  549.             $value false;
  550.         }
  551.         
  552.         return $value;
  553.     }
  554.     
  555.     /**
  556.      * Checks for Arabic Small letters (i.e. SMALL ALEF, SMALL WAW, SMALL YEH).
  557.      *
  558.      * @param string $archar Arabic unicode char
  559.      *      
  560.      * @return boolean True if it is Arabic Small letter
  561.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  562.      */
  563.     public function isSmall($archar)
  564.     {
  565.         $key array_search($archar$this->_chars);
  566.  
  567.         if (in_array($key$this->_charGroups['SMALL'])) {
  568.             $value true;
  569.         else {
  570.             $value false;
  571.         }
  572.         
  573.         return $value;
  574.     }
  575.     
  576.     /**
  577.      * Checks for Arabic Moon letters.
  578.      *
  579.      * @param string $archar Arabic unicode char
  580.      *      
  581.      * @return boolean True if it is Arabic Moon letter
  582.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  583.      */
  584.     public function isMoon($archar)
  585.     {
  586.         $key array_search($archar$this->_chars);
  587.  
  588.         if (in_array($key$this->_charGroups['MOON'])) {
  589.             $value true;
  590.         else {
  591.             $value false;
  592.         }
  593.         
  594.         return $value;
  595.     }
  596.     
  597.     /**
  598.      * Checks for Arabic Sun letters.
  599.      *
  600.      * @param string $archar Arabic unicode char
  601.      *      
  602.      * @return boolean True if it is Arabic Sun letter
  603.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  604.      */
  605.     public function isSun($archar)
  606.     {
  607.         $key array_search($archar$this->_chars);
  608.  
  609.         if (in_array($key$this->_charGroups['SUN'])) {
  610.             $value true;
  611.         else {
  612.             $value false;
  613.         }
  614.         
  615.         return $value;
  616.     }
  617.     
  618.     /**
  619.      * Return Arabic letter name in arabic.
  620.      *
  621.      * @param string $archar Arabic unicode char
  622.      *      
  623.      * @return string Arabic letter name in arabic
  624.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  625.      */
  626.     public function charName($archar)
  627.     {
  628.         $key array_search($archar$this->_chars);
  629.  
  630.         $name $this->_charArNames["$key"];
  631.         
  632.         return $name;
  633.     }
  634. }

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