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-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: 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-2012 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-2012 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 ($xml->xpath("//str_replace[@function='diaritical']/pair"as $pair{
  148.             array_push(self::$_diariticalSearch(string)$pair->search);
  149.             array_push(self::$_diariticalReplace(string)$pair->replace);
  150.         }
  151.  
  152.         foreach ($xml->xpath("//str_replace[@function='ISO233']/pair"as $pair{
  153.             array_push(self::$_iso233Search(string)$pair->search);
  154.             array_push(self::$_iso233Replace(string)$pair->replace);
  155.         }
  156.  
  157.         foreach ($xml->xpath("//str_replace[@function='RJGC']/pair"as $pair{
  158.             array_push(self::$_rjgcSearch(string)$pair->search);
  159.             array_push(self::$_rjgcReplace(string)$pair->replace);
  160.         }
  161.  
  162.         foreach ($xml->xpath("//str_replace[@function='SES']/pair"as $pair{
  163.             array_push(self::$_sesSearch(string)$pair->search);
  164.             array_push(self::$_sesReplace(string)$pair->replace);
  165.         }
  166.  
  167.         foreach ($xml->xpath("//str_replace[@function='ar2en']/pair"as $pair{
  168.             array_push(self::$_ar2enStrSearch(string)$pair->search);
  169.             array_push(self::$_ar2enStrReplace(string)$pair->replace);
  170.         }
  171.  
  172.         foreach ($xml->xpath("//preg_replace[@function='en2ar']/pair"as $pair{
  173.             array_push(self::$_en2arPregSearch(string)$pair->search);
  174.             array_push(self::$_en2arPregReplace(string)$pair->replace);
  175.         }
  176.     
  177.         foreach ($xml->xpath("//str_replace[@function='en2ar']/pair"as $pair{
  178.             array_push(self::$_en2arStrSearch(string)$pair->search);
  179.             array_push(self::$_en2arStrReplace(string)$pair->replace);
  180.         }
  181.     }
  182.         
  183.     /**
  184.      * Transliterate English string into Arabic by render them in the
  185.      * orthography of the Arabic language
  186.      *         
  187.      * @param string $string English string you want to transliterate
  188.      *                    
  189.      * @return String Out of vocabulary English string in Arabic characters
  190.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  191.      */
  192.     public static function en2ar($string)
  193.     {
  194.         $string strtolower($string);
  195.         $words  explode(' '$string);
  196.         $string '';
  197.         
  198.         foreach ($words as $word{
  199.             $word preg_replace(self::$_en2arPregSearch
  200.                                  self::$_en2arPregReplace$word);
  201.                                       
  202.             $word str_replace(self::$_en2arStrSearch
  203.                                 self::$_en2arStrReplace$word);
  204.  
  205.             $string .= ' ' $word;
  206.         }
  207.         
  208.         return $string;
  209.     }
  210.  
  211.     /**
  212.      * Transliterate Arabic string into English by render them in the
  213.      * orthography of the English language
  214.      *           
  215.      * @param string $string   Arabic string you want to transliterate
  216.      * @param string $standard Transliteration standard, default is UNGEGN
  217.      *                          and possible values are [UNGEGN, UNGEGN+, RJGC, SES, ISO233]
  218.      *                    
  219.      * @return String Out of vocabulary Arabic string in English characters
  220.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  221.      */
  222.     public static function ar2en($string$standard='UNGEGN')
  223.     {
  224.         //$string = str_replace('ة ال', 'tul', $string);
  225.  
  226.         $words  explode(' '$string);
  227.         $string '';
  228.                 
  229.         for ($i=0$i<count($words)-1$i++{
  230.             $words[$istr_replace('ة''ت'$words[$i]);
  231.         }
  232.  
  233.         foreach ($words as $word{
  234.             $temp $word;
  235.  
  236.             if ($standard == 'UNGEGN+'{
  237.                 $temp str_replace(self::$_diariticalSearch
  238.                                      self::$_diariticalReplace$temp);
  239.             else if ($standard == 'RJGC'{
  240.                 $temp str_replace(self::$_diariticalSearch
  241.                                      self::$_diariticalReplace$temp);
  242.  
  243.                 $temp str_replace(self::$_rjgcSearch
  244.                                      self::$_rjgcReplace$temp);
  245.             else if ($standard == 'SES'{
  246.                 $temp str_replace(self::$_diariticalSearch
  247.                                      self::$_diariticalReplace$temp);
  248.  
  249.                 $temp str_replace(self::$_sesSearch
  250.                                      self::$_sesReplace$temp);
  251.             else if ($standard == 'ISO233'{
  252.                 $temp str_replace(self::$_iso233Search
  253.                                      self::$_iso233Replace$temp);
  254.             }
  255.             
  256.             $temp preg_replace(self::$_ar2enPregSearch
  257.                                  self::$_ar2enPregReplace$temp);
  258.  
  259.             $temp str_replace(self::$_ar2enStrSearch
  260.                                 self::$_ar2enStrReplace$temp);
  261.  
  262.             $temp preg_replace(self::$_arFinePatterns
  263.                                  self::$_arFineReplacements$temp);
  264.             
  265.             if (preg_match('/[a-z]/'mb_substr($temp01))) {
  266.                 $temp ucwords($temp);
  267.             }
  268.             
  269.             $pos  strpos($temp'-');
  270.  
  271.             if ($pos 0{
  272.                 if (preg_match('/[a-z]/'mb_substr($temp$pos+11))) {
  273.                     $temp2  substr($temp0$pos);
  274.                     $temp2 .= '-'.strtoupper($temp[$pos+1]);
  275.                     $temp2 .= substr($temp$pos+2);
  276.                 else {
  277.                     $temp2 $temp;
  278.                 }
  279.             else {
  280.                 $temp2 $temp;
  281.             }
  282.  
  283.             $string .= ' ' $temp2;
  284.         }
  285.         
  286.         return $string;
  287.     }
  288.     
  289.     /**
  290.      * Render numbers in given string using HTML entities that will show them as
  291.      * Arabic digits (i.e. 1, 2, 3, etc.) whatever browser language settings are
  292.      * (if browser supports UTF-8 character set).
  293.      *         
  294.      * @param string $string String includes some digits here or there
  295.      *                    
  296.      * @return String Original string after replace digits by HTML entities that
  297.      *                 will show given number using Indian digits
  298.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  299.      */
  300.     public static function enNum($string)
  301.     {
  302.         $html '';
  303.  
  304.         $digits str_split("$string");
  305.  
  306.         foreach ($digits as $digit{
  307.             $html .= preg_match('/\d/'$digit"&#x3$digit;$digit;
  308.         }
  309.         
  310.         return $html;
  311.     }
  312.     
  313.     /**
  314.      * Render numbers in given string using HTML entities that will show them as
  315.      * Indian digits (i.e. ١, ٢, ٣, etc.) whatever browser language settings are
  316.      * (if browser supports UTF-8 character set).
  317.      *         
  318.      * @param string $string String includes some digits here or there
  319.      *                    
  320.      * @return String Original string after replace digits by HTML entities that
  321.      *                 will show given number using Arabic digits
  322.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  323.      */
  324.     public static function arNum($string)
  325.     {
  326.         $html '';
  327.  
  328.         $digits str_split("$string");
  329.  
  330.         foreach ($digits as $digit{
  331.             $html .= preg_match('/\d/'$digit"&#x066$digit;$digit;
  332.         }
  333.         
  334.         return $html;
  335.     }
  336. }

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