Friday, March 19, 2010

Using GMail's Transliteration tool to create documents in Punjabi(Gurmukhi) or Hindi or any other supported script

One can use GMail's transliteration tool to create documents in Punjabi(Gurmukhi) or Hindi or any other supported script. At the time of this writing GMail supports Arabic, Bangla, Gujrati, Hindi, Kannada, Malayalam, Marathi, Nepali, Punjabi, Tamil, Telugu, Urdu and Farsi.  If you are not already doing it, this post explains how you can do it.

Launch your browser and open GMail. Now compose a new mail. If your browser looks like the following screenshot, then you do not have transliteration enabled at this time.


Now click on settings from the top right options and once "Settings" are loaded click on "Show all language options" as shown in the screenshot below.


After page loads up, you should be able to see a check box labelled "Enable Transliteration - type using Phonetic English", as shown in the screenshot below.


Now click on the dropdown and choose your preferred language for transliteration.


Click on "Save Changes" to enable transliteration.



Now compose a mail. You should be able to see an extra button labelled "ਅ" in case you selected "ਪੰਜਾਬੀ" or  "अ" in case you chose "हिन्दी" as your preferred language of transliteration, as shown in the following screenshots.





Now assuming you want to create document in Punjabi, click on Transliteration button and start typing. In the following example, I typed sat sri akal.








If you feel the word is incorrectly transliterated, you can fix it by clicking on the text and you will be provided with various options of the word as shown in the following screenshot.


Following is an example of creating a document in Hindi.




If you feel the word was incorrectly transliterated, you can fix it by clicking on the text and you will be provided with various options of the word as shown in the following screenshot.


Now you can not only send emails in Hindi/Punjabi or any other language supported by GMail, but you can also create other documents. Simply cut and paste the text from your browser to the other application and you are good to go.

Labels: , , , , , , ,

Thursday, October 22, 2009

Creating a simple CAPTCHA tool

CAPTCHA or a reverse Turing test is used to stop bots or automated programs to spam or register for services such as free email services like GMail or Yahoo mail.


There are several types of CAPTCHAs being used. Following is the CAPTCHA being used by Google for it's services. This is a powerful CAPTCHA but not always human solvable. Does it frustrate users? You bet!



Like Google's CAPTCHA Hotmail/Passport's CAPTCHA is also many times not human solvable. An example is shown below.

Another example of CAPTCHA, this time from Yahoo.


The problem with most CAPTCHAs is that if they are strong enough(read unsolvable) for bots, they are then unsolvable by humans too, as various characters used are overlapped, distorted, sometimes characters get merged with their backgrounds and make them hard to decipher by humans too. Such an example of CAPTCHA I found some time back. Here it's for your viewing pleasure !


In this post I'll explain how to implement a simple yet powerful enough CAPTCHA tool. Though I am trying to explain the concept using PHP, any language/platform can be used which knows how to interpret TrueType fonts, create images dynamically and supports (HTTP) session management. This is the first version of my CAPTCHA tool and it was done as a proof of concept.

  • Create a folder on server and save TrueType fonts you want to use for the CAPTCHA tool, in this folder. Ensure PHP has read permissions for the folder/files. The more the better.
  • Define an array $captchaChars, this shall hold all the charcters used in CAPTCHA.
$captchaChars = array("a", "b", "c", "d", "e", "f",
 "g", "h", "i", "j",  "k","l", "m", "n", "o", "p",
 "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ,
"1","2", "3","4", "5", "6", "7", "8", "9", "0",
 "A", "B", "C", "D", "E","F", "G", "H", "I", "J",
 "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z"); 
  • Define a variable to hold CAPTCHA text
    $captchaText = "";
  • Define a variable to hold number of characters in the CAPTCHA(the more the better, but there will be a performance hit)
    $numChars = 7;
  • Generate CAPTCHA text by reading characters from "$captchaChars", randomly.
for($i=0; $i < $numChars; $i++){ $captchaCharNum = rand(0, 61);
$captchaText .= $captchaChars[$captchaCharNum];
}

  • Start web session and save CAPTCHA text for validation later.
session_start();

    $_SESSION['captcha'] = $captchaText;
    • Create an image and then add random lines in the background to confuse bots
    header("Content-type: image/jpeg");
    $im = imagecreate(155,60);
    $white = imagecolorallocate($im, rand(175,255),rand(175,255),rand(175,255));
    $black = imagecolorallocate($im, rand(0,125),rand(0,125),rand(0,125));

    $numLines = 10;

    for($i = 0; $i < $numLines; $i++){
    $line_color = imagecolorallocate($im, rand(125,165),rand(0,255),rand(125,165));
    imageline ($im, rand(0,160), rand(0,125), rand(0,125), rand(0,160), $line_color);
    }

    • Choose a font randomly to create CAPTCHA text
      $randFont = rand(0, 45);
    • Finally add the CAPTCHA text to the image and send it to browser
    imagettftext($im, 20, 0, 8, 35, $black, "path_to_capctha_fonts/$randFont".".ttf", $captchaText);
    imagejpeg($im);
    imagedestroy($im);

    • Now create an HTML page with following code

    <html>
    <head>
    </head>
    <body>
    <form action="validate_user.php" method="post">
    <img src="captcha.php" />
    <input type="text" name="captcha_text" maxlength="7" title="Enter text in the image" style="font-size:8;">
    <input type="Submit" name="submit">
    </form>
    </body>
    </html>


    • <img src="captcha.php" /> is what renders the CAPTCHA on browser.
    Now we shall see how server side validation code looks like.

    session_start();
    $captcha = $_SESSION['captcha'];
    $userCaptcha = $_POST['captcha_text'];
    • The code above starts a session and reads the CAPTCHA text's value which was saved earlier in the session
    • The value entered by user in CAPTCHA validation field is read from the HTTP request
    • This CAPTCHA tool forgives users for mistaking i or I for 1, z or Z for 2, o or O for 0 etc
    $processAdvanced = false;

      if(preg_match("/\B[i|I|1|l|s|S|5|2|z|Z|o|0|O|q|9|g]\B/i", $captcha)){

      $processAdvanced = true;
      }

      • First check if there are any characters present which user can easily make mistakes in reading, if there are then replace these characters for validation.
      if($processAdvanced){
      $patterns[0] = '/[i|I|i|l]/';
      $patterns[1] = '/[s|S|5]/';
      $patterns[2] = '/[2|z|Z]/';
      $patterns[3] = '/[o|O|0]/';
      $patterns[4] = '/[q|9|g]/';

      $replacements[0] = 'i';
      $replacements[1] = 's';
      $replacements[2] = 'z';
      $replacements[3] = 'o';
      $replacements[4] = 'q';

      //Replace the chars in both $captcha and $userCaptcha
      $captcha = preg_replace($patterns, $replacements, $captcha);
      $userCaptcha = preg_replace($patterns, $replacements, $userCaptcha);
      }
      • Finally validate and report it to the user/bot that whether validation succeeded or failed
      if(preg_match("/^$captcha$/i", $userCaptcha)){
      echo "<h3 style="'color:green;background-color:ivory'">You are indeed human</h3>
      ";
      } else {
      echo "<h1 style="'color:red;background-color:ivory;font-weight:700'">Looks like either you entered text incorrectly or you are a bot</h1>";
      }

      • You can see a demo of this CAPTCHA tool here.

      Labels: , ,

      Friday, October 9, 2009

      Murals of Lacombe

      Lacombe is a small town with a population of about 11200 in central Alberta. Its located on Hwy 2 about 120 KM from Edmonton and about 180 KM from Calgary. This town has lots of murals painted in the back alleys around its downtown.

      Here are most of the Lacombe murals.




      My personal favorite is the following one, train station with two steam locomotives.



      It all started with painting of these garbage bins. Various artists have painted these garbage bins, and most of these murals on back alleys of Lacombe have been painted by a guy called Tim.


























      Tim at work.


      Labels: , , , , ,

      Sunday, November 9, 2008

      Devnagri Keyboard(देवनागरी कुंजीपटल)

      Devanagri is the script widely used to write Sanskrit, Hindi and other North Indian languages like Marwari, Dogri, Braj Bhasha, Bhojpuri, Nepali and to some extent Konkani and Marathi. Konkani and Marathi are spoken in the Western India. Some Punjabis especially Hindus write Punjabi in Devanagari script.

      I have created a phonetic keyboard for Mircrosoft Windows XP and Microsoft Windows Vista. This article has been written using this keyboard only. Use of this keyboard is very simple as this is a phonetic keyboard. You can download it from here. Most of the consonants are mapped to their Roman transliterated letters such as 'क' is mapped to 'k' (lowercase) while 'ख' is mapped to uppercase 'K'. Similarly vowels are mapped in this fashion. 'ा' is mapped to 'a' lowercase while 'ां' is mapped to uppercase 'A'. 'ि' is mapped to 'i' lowercase and 'ी' is mapped to 'I' uppercase etc. Complete mapping keys are provided in the installation guide provided with the download. Download today and start creating Hindi articles and text in Devanagari. A web page created using Windows is viewable on Mac OS X, and Linux(Ubuntu 09.10) computers too.

      Disclaimer:Microsoft, Windows and Vista are registered trademarks of Microsoft Corp, Mac OS X is trademark of Apple Inc, Linux is registerd trademark of Linus Torvalds and I don't intend to declare these my own. :-)

      देवनागरी लिपी का उपयोग मुख्यतः संसकृत । हिंदी और दूसरी उत्तर भारतीय भाषाएं लिखने मे होता है जैसे मारवाड़ी । हरियाणवी । डोगरी । बृज भाषा । भोजपुरी । नेपाली एवं कुछ हद तक कोंकणी और मराठी भी॥ कोंकणी और मराठी पश्चिम भारत मे बोली जाती है॥ कुछ पंजाबी मुख्यतः हिंदू । पंजाबी लिखने के लिए देवनागरी लिपी का उपयोग करते हैं॥ मैने एक देवनागरी का कुंजिपटल(Keyboard) तैयार किया है जो कि एक स्वर सूचक(Phonetic) कुंजिपटल है॥ यह कुंजिपटल माइक्रोसौफ्ट विंडोज़ ऐक्स पी एवं विंडोज़ विस्टा के लिए योग्य है॥ जैसे 'राजा' लिखने के लिए आप को 'raja' शब्द टाईप करने की आवश्यक्ता है॥ इसी तरह 'स्वर' लिखने के लिए आप को 's~vr' शब्द टाईप करने की आवश्यक्ता है॥ कोई भी शब्द अगर आप आधा लिखना चाहते हैं तो उस शब्द के बाद आप '~' टाईप करें॥ उम्मीद करता हूं की आप इस कुंजिपटल का इस्तेमाल कर के हिंदी एवं अन्य भाषाऒं मे लेख लिख सकेंगे एवं पत्राचार कर पाएंगे॥ आप इस कुंजिपटल को यहां से प्राप्त कर सकते हैं॥

      Labels: , , , ,

      Monday, November 3, 2008

      Gurmukhi Keyboard(ਗੁਰਮੂਖੀ ਕੀਬੋਰੱਡ)

      Punjabi in Eastern Punjab is predominantly written using Gurmukhi script, while in Western Punjab it's usually written in Shamukhi script. I have created a keyboard which works with Microsoft Windows XP and Mircosoft Windows Vista operating systems. This article has been written using this keyboard only. Use of this keyboard is very simple as this is a phonetic keyboard. You can download it from here. Most of the consonants are mapped to their Roman transliterated letters such as 'ਕ' is mapped to 'k' (lowercase) while 'ਖ' is mapped to uppercase 'K'. Similarly vowels are mapped in this fashion. 'ਾ' Kanna is mapped to 'a' lowercase while 'ਾਂ' Kanna with bindi is mapped to uppercase 'A'. 'ਿ' Sihari is mapped to 'i' lowercase and 'ੀ' Bihari is mapped to 'I' uppercase etc. Complete mapping keys are provided in the installation guide provided with the download. Download today and start creating punjabi articles and text in Gurmukhi. A web page created using Windows is viewable on Mac OS X, and Linux(Ubuntu 09.10) computers too.

      Disclaimer:Microsoft, Windows and Vista are registered trademarks of Microsoft Corp, Mac OS X is trademark of Apple Inc, Linux is registerd trademark of Linus Torvalds and I don't intend to declare these my own. :-)

      ਪੰਜਾਬੀ ਚੜਦੇ ਪੰਜਾਬ'ਚ ਗੁਰਮੂਖੀ'ਚ ਲਿਖੀ ਜਾਂਦੀ ਹੈ ਪਰ ਲਹਿੰਦੇ ਪੰਜਾਬ'ਚ ਸ਼ਾਹਮੁਖੀ ਦੀ ਹੀ ਵਰਤੋ ਹੁੰਦੀ ਹੈ॥ ਮੈ ਇਕ ਕੀਬੋਰੱਡ ਬਨਾਇਆ ਹੈ ਜੋ ਮਾਈਕ੍ਰੋਸੋਫੱਟ ਵਿੰਡੋਜ਼ ਐਕੱਸ ਪੀ ਅਤੇ ਮਾਈਕ੍ਰੋਸੋਫੱਟ ਵਿੰਡੋਜ਼ ਵਿਸਟਾ'ਚ ਕੰਮ ਕਰਦਾ ਹੈ॥ ਇਹ ਕੀਬੋਰਡ ਸਿਖਨਾ ਬੜਾ ਹੀ ਅਸਾਨ ਹੈ ਕਿਉੰਕਿ ਇਹ ਫੋਨੈਟਿਕ ਕੀਬੋਰਡ ਹੈ॥ ਇਹ ਲੇਖ ਮੈਂ ਇਸੇ ਕੀਬੋਰਡ ਦੀ ਵਰਤੋਂ ਕਰ ਕੇ ਲਿਖਿਆ ਹੈ॥ ਤੁਸੀ ਇਹ ਕੀਬੋਰੱਡ ਏਥੋਂ ਪਰਾਪਤ ਕਰ ਸਕਦੇ ਹੋਂ॥

      Labels: , , ,