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

Source for file Transliteration.php

Documentation is available at Transliteration.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: English-Arabic Transliteration
  31.  *  
  32.  * Filename:   Transliteration.php
  33.  *  
  34.  * Original    Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
  35.  *  
  36.  * Purpose:    Transliterate English words into Arabic by render them
  37.  *             in the orthography of the Arabic language and vise versa
  38.  *              
  39.  * ----------------------------------------------------------------------
  40.  *
  41.  * English-Arabic Transliteration
  42.  *    
  43.  * PHP class transliterate English words into Arabic by render them in the
  44.  * orthography of the Arabic language and vise versa.
  45.  *    
  46.  * Out of vocabulary (OOV) words are a common source of errors in cross language
  47.  * information retrieval. Bilingual dictionaries are often limited in their coverage
  48.  * of named- entities, numbers, technical terms and acronyms. There is a need to
  49.  * generate translations for these "on-the-fly" or at query time.
  50.  * 
  51.  * A significant proportion of OOV words are named entities and technical terms.
  52.  * Typical analyses find around 50% of OOV words to be named entities. Yet these
  53.  * can be the most important words in the queries. Cross language retrieval
  54.  * performance (average precision) reduced more than 50% when named entities in the
  55.  * queries were not translated.
  56.  * 
  57.  * When the query language and the document language share the same alphabet it may
  58.  * be sufficient to use the OOV word as its own translation. However, when the two
  59.  * languages have different alphabets, the query term must somehow be rendered in
  60.  * the orthography of the other language. The process of converting a word from one
  61.  * orthography into another is called transliteration.
  62.  * 
  63.  * Foreign words often occur in Arabic text as transliteration. This is the case for
  64.  * many categories of foreign words, not just proper names but also technical terms
  65.  * such as caviar, telephone and internet.
  66.  * 
  67.  * Example:
  68.  * <code>
  69.  *   include('./I18N/Arabic.php');
  70.  *   $obj = new I18N_Arabic('Transliteration');
  71.  *     
  72.  *   $ar_word_1 = $obj->en2ar($en_word_1);
  73.  *   $en_word_2 = $obj->ar2en($ar_word_2);
  74.  * </code>
  75.  *             
  76.  * @category  I18N
  77.  * @package   I18N_Arabic
  78.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  79.  * @copyright 2006-2013 Khaled Al-Sham'aa
  80.  *    
  81.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  82.  * @link      http://www.ar-php.org
  83.  */
  84.  
  85. // New in PHP V5.3: Namespaces
  86. // namespace I18N\Arabic;
  87. // 
  88. // $obj = new I18N\Arabic\Transliteration();
  89. // 
  90. // use I18N\Arabic;
  91. // $obj = new Arabic\Transliteration();
  92. //
  93. // use I18N\Arabic\Transliteration as Transliteration;
  94. // $obj = new Transliteration();
  95.  
  96. /**
  97.  * This PHP class transliterate English words into Arabic
  98.  *  
  99.  * @category  I18N
  100.  * @package   I18N_Arabic
  101.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  102.  * @copyright 2006-2013 Khaled Al-Sham'aa
  103.  *    
  104.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  105.  * @link      http://www.ar-php.org
  106.  */ 
  107. {
  108.     private static $_arFinePatterns     array("/'+/u""/([\- ])'/u"'/(.)#/u');
  109.     private static $_arFineReplacements array("'"'\\1'"\\1'\\1");
  110.     
  111.     private static $_en2arPregSearch  array();
  112.     private static $_en2arPregReplace array();
  113.     private static $_en2arStrSearch   array();
  114.     private static $_en2arStrReplace  array();
  115.     
  116.     private static $_ar2enPregSearch  array();
  117.     private static $_ar2enPregReplace array();
  118.     private static $_ar2enStrSearch   array();
  119.     private static $_ar2enStrReplace  array();
  120.         
  121.     private static $_diariticalSearch  array();
  122.     private static $_diariticalReplace array();
  123.  
  124.     private static $_iso233Search  array();
  125.     private static $_iso233Replace array();
  126.  
  127.     private static $_rjgcSearch  array();
  128.     private static $_rjgcReplace array();
  129.  
  130.     private static $_sesSearch  array();
  131.     private static $_sesReplace array();
  132.  
  133.     /**
  134.      * Loads initialize values
  135.      *
  136.      * @ignore
  137.      */         
  138.     public function __construct()
  139.     {
  140.         $xml simplexml_load_file(dirname(__FILE__).'/data/Transliteration.xml');
  141.  
  142.         foreach ($xml->xpath("//preg_replace[@function='ar2en']/pair"as $pair{
  143.             array_push(self::$_ar2enPregSearch(string)$pair->search);
  144.             array_push(self::$_ar2enPregReplace(string)$pair->replace);
  145.         }
  146.  
  147.         foreach (
  148.             $xml->xpath("//str_replace[@function='diaritical']/pair"as $pair
  149.         {
  150.             array_push(self::$_diariticalSearch(string)$pair->search);
  151.             array_push(self::$_diariticalReplace(string)$pair->replace);
  152.         }
  153.  
  154.         foreach ($xml->xpath("//str_replace[@function='ISO233']/pair"as $pair{
  155.             array_push(self::$_iso233Search(string)$pair->search);
  156.             array_push(self::$_iso233Replace(string)$pair->replace);
  157.         }
  158.  
  159.         foreach ($xml->xpath("//str_replace[@function='RJGC']/pair"as $pair{
  160.             array_push(self::$_rjgcSearch(string)$pair->search);
  161.             array_push(self::$_rjgcReplace(string)$pair->replace);
  162.         }
  163.  
  164.         foreach ($xml->xpath("//str_replace[@function='SES']/pair"as $pair{
  165.             array_push(self::$_sesSearch(string)$pair->search);
  166.             array_push(self::$_sesReplace(string)$pair->replace);
  167.         }
  168.  
  169.         foreach ($xml->xpath("//str_replace[@function='ar2en']/pair"as $pair{
  170.             array_push(self::$_ar2enStrSearch(string)$pair->search);
  171.             array_push(self::$_ar2enStrReplace(string)$pair->replace);
  172.         }
  173.  
  174.         foreach ($xml->xpath("//preg_replace[@function='en2ar']/pair"as $pair{
  175.             array_push(self::$_en2arPregSearch(string)$pair->search);
  176.             array_push(self::$_en2arPregReplace(string)$pair->replace);
  177.         }
  178.     
  179.         foreach ($xml->xpath("//str_replace[@function='en2ar']/pair"as $pair{
  180.             array_push(self::$_en2arStrSearch(string)$pair->search);
  181.             array_push(self::$_en2arStrReplace(string)$pair->replace);
  182.         }
  183.     }
  184.         
  185.     /**
  186.      * Transliterate English string into Arabic by render them in the
  187.      * orthography of the Arabic language
  188.      *         
  189.      * @param string $string English string you want to transliterate
  190.      *                    
  191.      * @return String Out of vocabulary English string in Arabic characters
  192.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  193.      */
  194.     public static function en2ar($string)
  195.     {
  196.         $string strtolower($string);
  197.         $words  explode(' '$string);
  198.         $string '';
  199.         
  200.         foreach ($words as $word{
  201.             $word preg_replace(
  202.                 self::$_en2arPregSearch
  203.                 self::$_en2arPregReplace$word
  204.             );
  205.             $word str_replace(
  206.                 self::$_en2arStrSearch
  207.                 self::$_en2arStrReplace
  208.                 $word
  209.             );
  210.  
  211.             $string .= ' ' $word;
  212.         }
  213.         
  214.         return $string;
  215.     }
  216.  
  217.     /**
  218.      * Transliterate Arabic string into English by render them in the
  219.      * orthography of the English language
  220.      *           
  221.      * @param string $string   Arabic string you want to transliterate
  222.      * @param string $standard Transliteration standard, default is UNGEGN
  223.      *                          and possible values are [UNGEGN, UNGEGN+, RJGC,
  224.      *                          SES, ISO233]
  225.      *                    
  226.      * @return String Out of vocabulary Arabic string in English characters
  227.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  228.      */
  229.     public static function ar2en($string$standard='UNGEGN')
  230.     {
  231.         //$string = str_replace('ة ال', 'tul', $string);
  232.  
  233.         $words  explode(' '$string);
  234.         $string '';
  235.                 
  236.         for ($i=0$i<count($words)-1$i++{
  237.             $words[$istr_replace('ة''ت'$words[$i]);
  238.         }
  239.  
  240.         foreach ($words as $word{
  241.             $temp $word;
  242.  
  243.             if ($standard == 'UNGEGN+'{
  244.                 $temp str_replace(
  245.                     self::$_diariticalSearch
  246.                     self::$_diariticalReplace
  247.                     $temp
  248.                 );
  249.             else if ($standard == 'RJGC'{
  250.                 $temp str_replace(
  251.                     self::$_diariticalSearch
  252.                     self::$_diariticalReplace
  253.                     $temp
  254.                 );
  255.                 $temp str_replace(
  256.                     self::$_rjgcSearch
  257.                     self::$_rjgcReplace
  258.                     $temp
  259.                 );
  260.             else if ($standard == 'SES'{
  261.                 $temp str_replace(
  262.                     self::$_diariticalSearch
  263.                     self::$_diariticalReplace
  264.                     $temp
  265.                 );
  266.                 $temp str_replace(
  267.                     self::$_sesSearch
  268.                     self::$_sesReplace
  269.                     $temp
  270.                 );
  271.             else if ($standard == 'ISO233'{
  272.                 $temp str_replace(
  273.                     self::$_iso233Search
  274.                     self::$_iso233Replace
  275.                     $temp
  276.                 );
  277.             }
  278.             
  279.             $temp preg_replace(
  280.                 self::$_ar2enPregSearch
  281.                 self::$_ar2enPregReplace
  282.                 $temp
  283.             );
  284.             $temp str_replace(
  285.                 self::$_ar2enStrSearch
  286.                 self::$_ar2enStrReplace
  287.                 $temp
  288.             );
  289.             $temp preg_replace(
  290.                 self::$_arFinePatterns
  291.                 self::$_arFineReplacements
  292.                 $temp
  293.             );
  294.             
  295.             if (preg_match('/[a-z]/'mb_substr($temp01))) {
  296.                 $temp ucwords($temp);
  297.             }
  298.             
  299.             $pos  strpos($temp'-');
  300.  
  301.             if ($pos 0{
  302.                 if (preg_match('/[a-z]/'mb_substr($temp$pos+11))) {
  303.                     $temp2  substr($temp0$pos);
  304.                     $temp2 .= '-'.strtoupper($temp[$pos+1]);
  305.                     $temp2 .= substr($temp$pos+2);
  306.                 else {
  307.                     $temp2 $temp;
  308.                 }
  309.             else {
  310.                 $temp2 $temp;
  311.             }
  312.  
  313.             $string .= ' ' $temp2;
  314.         }
  315.         
  316.         return $string;
  317.     }
  318.     
  319.     /**
  320.      * Render numbers in given string using HTML entities that will show them as
  321.      * Arabic digits (i.e. 1, 2, 3, etc.) whatever browser language settings are
  322.      * (if browser supports UTF-8 character set).
  323.      *         
  324.      * @param string $string String includes some digits here or there
  325.      *                    
  326.      * @return String Original string after replace digits by HTML entities that
  327.      *                 will show given number using Indian digits
  328.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  329.      */
  330.     public static function enNum($string)
  331.     {
  332.         $html '';
  333.  
  334.         $digits str_split("$string");
  335.  
  336.         foreach ($digits as $digit{
  337.             $html .= preg_match('/\d/'$digit"&#x3$digit;$digit;
  338.         }
  339.         
  340.         return $html;
  341.     }
  342.     
  343.     /**
  344.      * Render numbers in given string using HTML entities that will show them as
  345.      * Indian digits (i.e. ١, ٢, ٣, etc.) whatever browser language settings are
  346.      * (if browser supports UTF-8 character set).
  347.      *         
  348.      * @param string $string String includes some digits here or there
  349.      *                    
  350.      * @return String Original string after replace digits by HTML entities that
  351.      *                 will show given number using Arabic digits
  352.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  353.      */
  354.     public static function arNum($string)
  355.     {
  356.         $html '';
  357.  
  358.         $digits str_split("$string");
  359.  
  360.         foreach ($digits as $digit{
  361.             $html .= preg_match('/\d/'$digit"&#x066$digit;$digit;
  362.         }
  363.         
  364.         return $html;
  365.     }
  366. }

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