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("/'+/""/([\- ])'/"'/(.)#/');
  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.     /**
  122.      * Loads initialize values
  123.      *
  124.      * @ignore
  125.      */         
  126.     public function __construct()
  127.     {
  128.         $xml simplexml_load_file(dirname(__FILE__).'/data/Transliteration.xml');
  129.  
  130.         foreach ($xml->xpath("//preg_replace[@function='ar2en']/pair"as $pair{
  131.             array_push(self::$_ar2enPregSearch(string)$pair->search);
  132.             array_push(self::$_ar2enPregReplace(string)$pair->replace);
  133.         }
  134.  
  135.         foreach ($xml->xpath("//str_replace[@function='ar2en']/pair"as $pair{
  136.             array_push(self::$_ar2enStrSearch(string)$pair->search);
  137.             array_push(self::$_ar2enStrReplace(string)$pair->replace);
  138.         }
  139.  
  140.         foreach ($xml->xpath("//preg_replace[@function='en2ar']/pair"as $pair{
  141.             array_push(self::$_en2arPregSearch(string)$pair->search);
  142.             array_push(self::$_en2arPregReplace(string)$pair->replace);
  143.         }
  144.     
  145.         foreach ($xml->xpath("//str_replace[@function='en2ar']/pair"as $pair{
  146.             array_push(self::$_en2arStrSearch(string)$pair->search);
  147.             array_push(self::$_en2arStrReplace(string)$pair->replace);
  148.         }
  149.     }
  150.         
  151.     /**
  152.      * Transliterate English string into Arabic by render them in the
  153.      * orthography of the Arabic language
  154.      *         
  155.      * @param string $string English string you want to transliterate
  156.      *                    
  157.      * @return String Out of vocabulary English string in Arabic characters
  158.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  159.      */
  160.     public static function en2ar($string)
  161.     {
  162.         $string strtolower($string);
  163.         $words  explode(' '$string);
  164.         $string '';
  165.         
  166.         foreach ($words as $word{
  167.             $word preg_replace(self::$_en2arPregSearch
  168.                                  self::$_en2arPregReplace$word);
  169.                                       
  170.             $word str_replace(self::$_en2arStrSearch
  171.                                 self::$_en2arStrReplace$word);
  172.  
  173.             $string .= ' ' $word;
  174.         }
  175.         
  176.         return $string;
  177.     }
  178.  
  179.     /**
  180.      * Transliterate Arabic string into English by render them in the
  181.      * orthography of the English language
  182.      *           
  183.      * @param string $string Arabic string you want to transliterate
  184.      *                    
  185.      * @return String Out of vocabulary Arabic string in English characters
  186.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  187.      */
  188.     public static function ar2en($string)
  189.     {
  190.         $string str_replace('/ة ال/''tul'$string);
  191.  
  192.         $words  explode(' '$string);
  193.         $string '';
  194.  
  195.         foreach ($words as $word{
  196.             $temp preg_replace(self::$_ar2enPregSearch
  197.                                  self::$_ar2enPregReplace$word);
  198.  
  199.             $temp str_replace(self::$_ar2enStrSearch
  200.                                 self::$_ar2enStrReplace$temp);
  201.  
  202.             $temp preg_replace(self::$_arFinePatterns
  203.                                  self::$_arFineReplacements$temp);
  204.             
  205.             $temp ucwords($temp);
  206.             $pos  strpos($temp'-');
  207.  
  208.             if ($pos 0{
  209.                 $temp2  substr($temp0$pos);
  210.                 $temp2 .= '-'.strtoupper($temp[$pos+1]);
  211.                 $temp2 .= substr($temp$pos+2);
  212.             else {
  213.                 $temp2 $temp;
  214.             }
  215.  
  216.             $string .= ' ' $temp2;
  217.         }
  218.         
  219.         return $string;
  220.     }
  221.     
  222.     /**
  223.      * Render numbers in given string using HTML entities that will show them as
  224.      * Arabic digits (i.e. 1, 2, 3, etc.) whatever browser language settings are
  225.      * (if browser supports UTF-8 character set).
  226.      *         
  227.      * @param string $string String includes some digits here or there
  228.      *                    
  229.      * @return String Original string after replace digits by HTML entities that
  230.      *                 will show given number using Indian digits
  231.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  232.      */
  233.     public static function enNum($string)
  234.     {
  235.         $html '';
  236.  
  237.         $digits str_split("$string");
  238.  
  239.         foreach ($digits as $digit{
  240.             $html .= preg_match('/\d/'$digit"&#x3$digit;$digit;
  241.         }
  242.         
  243.         return $html;
  244.     }
  245.     
  246.     /**
  247.      * Render numbers in given string using HTML entities that will show them as
  248.      * Indian digits (i.e. ١, ٢, ٣, etc.) whatever browser language settings are
  249.      * (if browser supports UTF-8 character set).
  250.      *         
  251.      * @param string $string String includes some digits here or there
  252.      *                    
  253.      * @return String Original string after replace digits by HTML entities that
  254.      *                 will show given number using Arabic digits
  255.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  256.      */
  257.     public static function arNum($string)
  258.     {
  259.         $html '';
  260.  
  261.         $digits str_split("$string");
  262.  
  263.         foreach ($digits as $digit{
  264.             $html .= preg_match('/\d/'$digit"&#x066$digit;$digit;
  265.         }
  266.         
  267.         return $html;
  268.     }
  269. }

Documentation generated on Tue, 17 Jan 2012 09:18:29 +0200 by phpDocumentor 1.4.0