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-2016 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-2016 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. /**
  86.  * This PHP class transliterate English words into Arabic
  87.  *  
  88.  * @category  I18N
  89.  * @package   I18N_Arabic
  90.  * @author    Khaled Al-Sham'aa <khaled@ar-php.org>
  91.  * @copyright 2006-2016 Khaled Al-Sham'aa
  92.  *    
  93.  * @license   LGPL <http://www.gnu.org/licenses/lgpl.txt>
  94.  * @link      http://www.ar-php.org
  95.  */ 
  96. {
  97.     private static $_arFinePatterns     = array("/'+/u", "/([\- ])'/u", '/(.)#/u');
  98.     private static $_arFineReplacements = array("'", '\\1', "\\1'\\1");
  99.     
  100.     private static $_en2arPregSearch  = array();
  101.     private static $_en2arPregReplace = array();
  102.     private static $_en2arStrSearch   = array();
  103.     private static $_en2arStrReplace  = array();
  104.     
  105.     private static $_ar2enPregSearch  = array();
  106.     private static $_ar2enPregReplace = array();
  107.     private static $_ar2enStrSearch   = array();
  108.     private static $_ar2enStrReplace  = array();
  109.         
  110.     private static $_diariticalSearch  = array();
  111.     private static $_diariticalReplace = array();
  112.  
  113.     private static $_iso233Search  = array();
  114.     private static $_iso233Replace = array();
  115.  
  116.     private static $_rjgcSearch  = array();
  117.     private static $_rjgcReplace = array();
  118.  
  119.     private static $_sesSearch  = array();
  120.     private static $_sesReplace = array();
  121.  
  122.     /**
  123.      * Loads initialize values
  124.      *
  125.      * @ignore
  126.      */         
  127.     public function __construct()
  128.     {
  129.         $xml = simplexml_load_file(dirname(__FILE__).'/data/Transliteration.xml');
  130.  
  131.         foreach ($xml->xpath("//preg_replace[@function='ar2en']/pair") as $pair) {
  132.             array_push(self::$_ar2enPregSearch, (string)$pair->search);
  133.             array_push(self::$_ar2enPregReplace, (string)$pair->replace);
  134.         }
  135.  
  136.         foreach (
  137.             $xml->xpath("//str_replace[@function='diaritical']/pair") as $pair
  138.         ) {
  139.             array_push(self::$_diariticalSearch, (string)$pair->search);
  140.             array_push(self::$_diariticalReplace, (string)$pair->replace);
  141.         }
  142.  
  143.         foreach ($xml->xpath("//str_replace[@function='ISO233']/pair") as $pair) {
  144.             array_push(self::$_iso233Search, (string)$pair->search);
  145.             array_push(self::$_iso233Replace, (string)$pair->replace);
  146.         }
  147.  
  148.         foreach ($xml->xpath("//str_replace[@function='RJGC']/pair") as $pair) {
  149.             array_push(self::$_rjgcSearch, (string)$pair->search);
  150.             array_push(self::$_rjgcReplace, (string)$pair->replace);
  151.         }
  152.  
  153.         foreach ($xml->xpath("//str_replace[@function='SES']/pair") as $pair) {
  154.             array_push(self::$_sesSearch, (string)$pair->search);
  155.             array_push(self::$_sesReplace, (string)$pair->replace);
  156.         }
  157.  
  158.         foreach ($xml->xpath("//str_replace[@function='ar2en']/pair") as $pair) {
  159.             array_push(self::$_ar2enStrSearch, (string)$pair->search);
  160.             array_push(self::$_ar2enStrReplace, (string)$pair->replace);
  161.         }
  162.  
  163.         foreach ($xml->xpath("//preg_replace[@function='en2ar']/pair") as $pair) {
  164.             array_push(self::$_en2arPregSearch, (string)$pair->search);
  165.             array_push(self::$_en2arPregReplace, (string)$pair->replace);
  166.         }
  167.     
  168.         foreach ($xml->xpath("//str_replace[@function='en2ar']/pair") as $pair) {
  169.             array_push(self::$_en2arStrSearch, (string)$pair->search);
  170.             array_push(self::$_en2arStrReplace, (string)$pair->replace);
  171.         }
  172.     }
  173.         
  174.     /**
  175.      * Transliterate English string into Arabic by render them in the
  176.      * orthography of the Arabic language
  177.      *         
  178.      * @param string $string English string you want to transliterate
  179.      * @param string $locale Locale information (e.g. 'en_GB' or 'de_DE')
  180.      *                    
  181.      * @return String Out of vocabulary English string in Arabic characters
  182.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  183.      */
  184.     public static function en2ar($string, $locale='en_US')
  185.     {
  186.         setlocale(LC_ALL, $locale);
  187.         $string = iconv("UTF-8", "ASCII//TRANSLIT", $string);
  188.         $string = preg_replace('/[^\w\s]/', '', $string);
  189.         
  190.         $string = strtolower($string);
  191.         $words  = explode(' ', $string);
  192.         $string = '';
  193.         
  194.         foreach ($words as $word) {
  195.             $word = preg_replace(
  196.                 self::$_en2arPregSearch, 
  197.                 self::$_en2arPregReplace, $word
  198.             );
  199.             $word = str_replace(
  200.                 self::$_en2arStrSearch, 
  201.                 self::$_en2arStrReplace, 
  202.                 $word
  203.             );
  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,
  218.      *                          SES, ISO233]
  219.      *                    
  220.      * @return String Out of vocabulary Arabic string in English characters
  221.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  222.      */
  223.     public static function ar2en($string, $standard='UNGEGN')
  224.     {
  225.         //$string = str_replace('ة ال', 'tul', $string);
  226.  
  227.         $words  = explode(' ', $string);
  228.         $string = '';
  229.                 
  230.         for ($i=0; $i<count($words)-1; $i++) {
  231.             $words[$i] = str_replace('ة', 'ت', $words[$i]);
  232.         }
  233.  
  234.         foreach ($words as $word) {
  235.             $temp = $word;
  236.  
  237.             if ($standard == 'UNGEGN+') {
  238.                 $temp = str_replace(
  239.                     self::$_diariticalSearch, 
  240.                     self::$_diariticalReplace, 
  241.                     $temp
  242.                 );
  243.             } else if ($standard == 'RJGC') {
  244.                 $temp = str_replace(
  245.                     self::$_diariticalSearch, 
  246.                     self::$_diariticalReplace, 
  247.                     $temp
  248.                 );
  249.                 $temp = str_replace(
  250.                     self::$_rjgcSearch, 
  251.                     self::$_rjgcReplace, 
  252.                     $temp
  253.                 );
  254.             } else if ($standard == 'SES') {
  255.                 $temp = str_replace(
  256.                     self::$_diariticalSearch, 
  257.                     self::$_diariticalReplace, 
  258.                     $temp
  259.                 );
  260.                 $temp = str_replace(
  261.                     self::$_sesSearch, 
  262.                     self::$_sesReplace, 
  263.                     $temp
  264.                 );
  265.             } else if ($standard == 'ISO233') {
  266.                 $temp = str_replace(
  267.                     self::$_iso233Search, 
  268.                     self::$_iso233Replace, 
  269.                     $temp
  270.                 );
  271.             }
  272.             
  273.             $temp = preg_replace(
  274.                 self::$_ar2enPregSearch, 
  275.                 self::$_ar2enPregReplace, 
  276.                 $temp
  277.             );
  278.             $temp = str_replace(
  279.                 self::$_ar2enStrSearch, 
  280.                 self::$_ar2enStrReplace, 
  281.                 $temp
  282.             );
  283.             $temp = preg_replace(
  284.                 self::$_arFinePatterns, 
  285.                 self::$_arFineReplacements, 
  286.                 $temp
  287.             );
  288.             
  289.             if (preg_match('/[a-z]/', mb_substr($temp, 0, 1))) {
  290.                 $temp = ucwords($temp);
  291.             }
  292.             
  293.             $pos  = strpos($temp, '-');
  294.  
  295.             if ($pos > 0) {
  296.                 if (preg_match('/[a-z]/', mb_substr($temp, $pos+1, 1))) {
  297.                     $temp2  = substr($temp, 0, $pos);
  298.                     $temp2 .= '-'.strtoupper($temp[$pos+1]);
  299.                     $temp2 .= substr($temp, $pos+2);
  300.                 } else {
  301.                     $temp2 = $temp;
  302.                 }
  303.             } else {
  304.                 $temp2 = $temp;
  305.             }
  306.  
  307.             $string .= ' ' . $temp2;
  308.         }
  309.         
  310.         return $string;
  311.     }
  312.     
  313.     /**
  314.      * Render numbers in given string using HTML entities that will show them as
  315.      * Arabic digits (i.e. 1, 2, 3, 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 Indian digits
  322.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  323.      */
  324.     public static function enNum($string)
  325.     {
  326.         $html = '';
  327.  
  328.         $digits = str_split("$string");
  329.  
  330.         foreach ($digits as $digit) {
  331.             $html .= preg_match('/\d/', $digit) ? "&#x3$digit;" : $digit;
  332.         }
  333.         
  334.         return $html;
  335.     }
  336.     
  337.     /**
  338.      * Render numbers in given string using HTML entities that will show them as
  339.      * Indian digits (i.e. ١, ٢, ٣, etc.) whatever browser language settings are
  340.      * (if browser supports UTF-8 character set).
  341.      *         
  342.      * @param string $string String includes some digits here or there
  343.      *                    
  344.      * @return String Original string after replace digits by HTML entities that
  345.      *                 will show given number using Arabic digits
  346.      * @author Khaled Al-Sham'aa <khaled@ar-php.org>
  347.      */
  348.     public static function arNum($string)
  349.     {
  350.         $html = '';
  351.  
  352.         $digits = str_split("$string");
  353.  
  354.         foreach ($digits as $digit) {
  355.             $html .= preg_match('/\d/', $digit) ? "&#x066$digit;" : $digit;
  356.         }
  357.         
  358.         return $html;
  359.     }
  360. }

Documentation generated on Fri, 01 Jan 2016 10:26:29 +0200 by phpDocumentor 1.4.0