Source for file KeySwap.php
Documentation is available at KeySwap.php
* ----------------------------------------------------------------------
* Copyright (c) 2006-2016 Khaled Al-Sham'aa
* ----------------------------------------------------------------------
* This program is open source product; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License (LGPL)
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/lgpl.txt>.
* ----------------------------------------------------------------------
* Class Name: Arabic Keyboard Swapping Language
* Original Author(s): Khaled Al-Sham'aa <khaled@ar-php.org>
* Purpose: Convert keyboard language programmatically (English - Arabic)
* ----------------------------------------------------------------------
* Arabic Keyboard Swapping Language
* PHP class to convert keyboard language between English and Arabic
* programmatically. This function can be helpful in dual language forms when
* users miss change keyboard language while they are entering data.
* If you wrote an Arabic sentence while your keyboard stays in English mode by
* mistake, you will get a non-sense English text on your PC screen. In that case
* you can use this class to make a kind of magic conversion to swap that odd text
* by original Arabic sentence you meant when you type on your keyboard.
* Please note that magic conversion in the opposite direction (if you type English
* sentences while your keyboard stays in Arabic mode) is also available in this
* class, but it is not reliable as much as previous case because in Arabic keyboard
* we have some keys provide a short-cut to type two chars in one click (those keys
* include: b, B, G and T).
* Well, we try in this class to come over this issue by suppose that user used
* optimum way by using short-cut keys when available instead of assemble chars
* using stand alone keys, but if (s)he does not then you may have some typo chars
* include('./I18N/Arabic.php');
* $obj = new I18N_Arabic('KeySwap');
* $str = "Hpf lk hgkhs hglj'vtdkK Hpf hg`dk dldg,k f;gdjil Ygn ,p]hkdm ...";
* echo "<p><u><i>Before:</i></u><br />$str<br /><br />";
* $text = $obj->swapEa($str);
* echo "<u><i>After:</i></u><br />$text<br /><br />";
* @author Khaled Al-Sham'aa <khaled@ar-php.org>
* @copyright 2006-2016 Khaled Al-Sham'aa
* @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
* @link http://www.ar-php.org
* This PHP class convert keyboard language programmatically (English - Arabic)
* @author Khaled Al-Sham'aa <khaled@ar-php.org>
* @copyright 2006-2016 Khaled Al-Sham'aa
* @license LGPL <http://www.gnu.org/licenses/lgpl.txt>
* @link http://www.ar-php.org
private $_transliteration = array();
* Loads initialize values
public function __construct()
foreach ($xml->transliteration->item as $item) {
$this->_transliteration["$index"] = (string) $item;
foreach ($xml->arabic->key as $key) {
$index = (int) $key['id'];
$this->_arKeyboard[$index] = (string) $key;
foreach ($xml->english->key as $key) {
$index = (int) $key['id'];
$this->_enKeyboard[$index] = (string) $key;
foreach ($xml->french->key as $key) {
$index = (int) $key['id'];
$this->_frKeyboard[$index] = (string) $key;
$this->_arLogodd = file(dirname(__FILE__ ). '/data/ar-logodd.php');
$this->_enLogodd = file(dirname(__FILE__ ). '/data/en-logodd.php');
* Make conversion to swap that odd Arabic text by original English sentence
* you meant when you type on your keyboard (if keyboard language was
* @param string $text Odd Arabic string
* @return string Normal English string
* @author Khaled Al-Sham'aa
$output = $this->_swapCore($text, 'ar', 'en');
* Make conversion to swap that odd English text by original Arabic sentence
* you meant when you type on your keyboard (if keyboard language was
* @param string $text Odd English string
* @return string Normal Arabic string
* @author Khaled Al-Sham'aa
$output = $this->_swapCore($text, 'en', 'ar');
* Make conversion to swap that odd Arabic text by original French sentence
* you meant when you type on your keyboard (if keyboard language was
* @param string $text Odd Arabic string
* @return string Normal French string
* @author Khaled Al-Sham'aa
$output = $this->_swapCore($text, 'ar', 'fr');
* Make conversion to swap that odd French text by original Arabic sentence
* you meant when you type on your keyboard (if keyboard language was
* @param string $text Odd French string
* @return string Normal Arabic string
* @author Khaled Al-Sham'aa
$output = $this->_swapCore($text, 'fr', 'ar');
* Make conversion between different keyboard maps to swap odd text in
* one language by original meaningful text in another language that
* you meant when you type on your keyboard (if keyboard language was
* @param string $text Odd string
* @param string $in Input language [ar|en|fr]
* @param string $out Output language [ar|en|fr]
* @return string Normal string
* @author Khaled Al-Sham'aa
private function _swapCore($text, $in, $out)
$inputMap = $this->_arKeyboard;
$inputMap = $this->_enKeyboard;
$inputMap = $this->_frKeyboard;
$outputMap = $this->_arKeyboard;
$outputMap = $this->_enKeyboard;
$outputMap = $this->_frKeyboard;
for ($i= 0; $i< $max; $i++ ) {
$output .= $outputMap[$key];
* Calculate the log odd probability that inserted string from keyboard
* @param string $str Inserted string from the keyboard
* @return float Calculated score for input string as English language
* @author Khaled Al-Sham'aa
$lines = $this->_enLogodd;
foreach ($lines as $line) {
for ($i= 0; $i< 28; $i++ ) {
$logodd["$first"]["{ $second[$i]}"] = $values[$i];
for ($i= 1; $i< $max; $i++ ) {
if (isset ($logodd["$first"]["$second"])) {
$rank += $logodd["$first"]["$second"];
* Calculate the log odd probability that inserted string from keyboard
* @param string $str Inserted string from the keyboard
* @return float Calculated score for input string as Arabic language
* @author Khaled Al-Sham'aa
$lines = $this->_arLogodd;
foreach ($lines as $line) {
for ($i= 0; $i< 37; $i++ ) {
$logodd["$first"]["{ $second[$i]}"] = $values[$i];
for ($i= 1; $i< $max; $i++ ) {
if (isset ($logodd["$first"]["$second"])) {
$rank += $logodd["$first"]["$second"];
* This method will automatically detect the language of content supplied
* in the input string. It will return the suggestion of correct inserted text.
* The accuracy of the automatic language detection increases with the amount
* @param string $str Inserted string from the keyboard
* @return string Fixed string language and letter case to the better guess
* @author Khaled Al-Sham'aa
$arNum = count($matches[0]);
$capital = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$small = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
if ($arNum > $nonArNum) {
$strCaps = strtr($str, $capital, $small);
$arStrCaps = $this->swapEa($strCaps);
if ($arNum > $nonArNum) {
$arCapsRank = $this->checkAr($arStrCaps);
if ($enRank > $arRank && $enRank > $arCapsRank) {
$capsNum = count($matches[0]);
$nonCapsNum = count($matches[0]);
if ($capsNum > $nonCapsNum && $nonCapsNum > 0) {
$enCapsStr = strtr($enStr, $capital, $small);
if ($arCapsRank > $arRank) {
|