<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cyril Hanquez &#187; API</title>
	<atom:link href="http://cyrilhanquez.com/blog/category/api/feed/" rel="self" type="application/rss+xml" />
	<link>http://cyrilhanquez.com/blog</link>
	<description>Rich Internet Application developer</description>
	<lastBuildDate>Mon, 07 Jun 2010 08:12:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Have fun with URL Encoding!</title>
		<link>http://cyrilhanquez.com/blog/2009/03/27/have-fun-with-url-encoding/</link>
		<comments>http://cyrilhanquez.com/blog/2009/03/27/have-fun-with-url-encoding/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 16:26:41 +0000</pubDate>
		<dc:creator>Cyril H.</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[URLEncoding]]></category>
		<category><![CDATA[w3c]]></category>

		<guid isPermaLink="false">http://cyrilhanquez.com/blog/?p=377</guid>
		<description><![CDATA[I had some trouble while playing with the Google Language API regarding the number of characters that can be sent using a GET request: Although the specification of the HTTP protocol does not specify any maximum length, URLs over 2,000 characters will not work in the most popular web browser. Sounds okay for me, but [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fcyrilhanquez.com%252Fblog%252F2009%252F03%252F27%252Fhave-fun-with-url-encoding%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Have%20fun%20with%20URL%20Encoding%21%22%20%7D);"></div>
<p style="text-align: justify;">I had some trouble while playing with the <strong><a href="http://code.google.com/intl/fr-BE/apis/ajaxlanguage/" target="_blank">Google Language API</a></strong> regarding the number of characters that can be sent using a GET request: Although the <strong><a href="http://www.w3.org/Protocols/rfc2616/rfc2616.html" target="_blank">specification</a></strong> of the HTTP protocol does not specify any  maximum length,  <strong>URLs over 2,000 characters will not work</strong> in the most popular web browser.</p>
<p style="text-align: justify;">Sounds okay for me, but we had also to remember that query strings have to be URLEncoded: I have large texts that need to be split in several parts so that it could sent using a GET request. So I need to calculate where to cut the text to have less than 2000 characters.</p>
<p style="text-align: justify;"><span id="more-377"></span><span style="text-decoration: underline;"><strong>FLEX/AS3</strong></span></p>
<p style="text-align: justify;">I wanted to use <strong>encodeURI()</strong> function to calculate the size of my URLEncoded string to split it wisely. In the documentation, you can read :</p>
<table border="0" align="center">
<tbody>
<tr>
<th>Characters not encoded</th>
</tr>
<tr>
<td><code>0 1 2 3 4 5 6 7 8 9</code></td>
</tr>
<tr>
<td><code>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</code></td>
</tr>
<tr>
<td style="text-align: center;"><code>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</code></td>
</tr>
<tr>
<td><code>; / ? : @ &amp; = + $ , #</code></td>
</tr>
<tr>
<td><code>- _ . ! ~ * ' ( )</code></td>
</tr>
</tbody>
</table>
<p style="text-align: justify;">
Why not&#8230;</p>
<p style="text-align: justify;">Using <strong>HTTPService,</strong> unless you specify the contentType to be <code>application/xml,</code> the parameters are urlencoded which makes sense, of course.</p>
<p style="text-align: justify;">Now the fun part:</p>
<p style="padding-left: 30px;"><strong><em>source String</em></strong><em><strong> &#8211; 59chars</strong></em><strong><em> : </em></strong><br />
Phrase_choc &#8211; j&#8217;adore quand un plan se déroule sans accroc!</p>
<p style="padding-left: 30px;"><em><strong>encodeURI()</strong></em><em><strong> &#8211; 82chars</strong></em><em><strong> : </strong></em><br />
Phrase_choc%20-%20j&#8217;adore%20quand%20un%20plan%20se%20d%C3%A9roule%20sans%20accroc!</p>
<p style="padding-left: 30px;"><em><strong>HTTPService parameter &#8211; 90chars:</strong></em><br />
Phrase%5Fchoc%20%2D%20j%27adore%20quand%20un%20plan%20se%20d%C3%A9roule%20sans%20accroc%21</p>
<p style="text-align: justify;">So there&#8217;s a difference between the encoding done by <strong>HTTPService</strong> and  using <strong>encodeURI</strong> function?! <strong>HTTPService</strong> is encoding also the characters from the last two rows in the above table&#8230; is that &#8220;normal&#8221; ?  More fun ?</p>
<p style="text-align: justify;">If you&#8217;re sending the encodedURI String via the HTTPService you will get a nice 112 chars long string :</p>
<p style="padding-left: 30px;">Phrase%5Fchoc%2520%2D%2520j%27adore%2520quand%2520un%2520plan<br />
%2520se%2520d%25C3%25A9roule%2520sans%2520accroc%21</p>
<p>Let&#8217;s compare with other languages.</p>
<p><span style="text-decoration: underline;"><strong>COLDFUSION</strong></span></p>
<p>Using<strong> URLEncodedFormat()</strong> I got the following encoded string:</p>
<p style="padding-left: 30px;">UntitledPhrase%5Fchoc%20%2D%20j%27adore%20quand%20un%20plan%20se%20d%C3%A9roule%20sans%20accroc%21</p>
<p>same as the one from<strong> HTTPService</strong>.</p>
<p><span style="text-decoration: underline;"><strong>JAVASCRIPT</strong></span></p>
<p>Using <strong>encodeURI()</strong> function I got the following encoded string:</p>
<p style="padding-left: 30px;">Phrase_choc%20-%20j&#8217;adore%20quand%20un%20plan%20se%20d%C3%A9roule%20sans%20accroc!</p>
<p>same as the one from <strong>AS3 encodeURI()</strong>.</p>
<p>Well,  I&#8217;m finding this a bit disturbing, and the W3C is not really helping.</p>
<blockquote>
<p style="text-align: justify;">The same encoding method may be used for encoding characters whose use,  although technically allowed in a URL, would be unwise due to problems of  corruption by imperfect gateways or misrepresentation due to the use of variant  character sets, or which would simply be awkward in a given environment. Because  a % sign always indicates an encoded character, a URL may be made safer simply  by encoding any characters considered unsafe, while leaving already encoded  characters still encoded. Similarly, in cases where a larger set of characters  is acceptable, % signs can be selectively and reversibly expanded.</p>
<p style="text-align: justify;">The reserved characters shall however never be arbitrarly encoded and  decoded.</p>
</blockquote>
<p>Any thoughts ?</p>

]]></content:encoded>
			<wfw:commentRss>http://cyrilhanquez.com/blog/2009/03/27/have-fun-with-url-encoding/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Playing with the Google Language API</title>
		<link>http://cyrilhanquez.com/blog/2008/09/26/playing-with-the-google-language-api/</link>
		<comments>http://cyrilhanquez.com/blog/2008/09/26/playing-with-the-google-language-api/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 09:57:54 +0000</pubDate>
		<dc:creator>Cyril H.</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Language detection]]></category>

		<guid isPermaLink="false">http://cyrilhanquez.com/blog/?p=187</guid>
		<description><![CDATA[&#160;The Google AJAX Language API lets you translate and detect the language of blocks of text. For non-Javascript environments, it exposes also a simple RESTful interface to use in combination with JSON to decode the result. Let&#8217;s try a simple Flex example of language detection : Demo (right click for the source code) It&#8217;s pretty easy to achieve. Here is [...]]]></description>
			<content:encoded><![CDATA[
<div class="topsy_widget_data topsy_theme_blue" style="float: right;margin-left: 0.75em; background: url(data:,%7B%20%22url%22%3A%20%22http%253A%252F%252Fcyrilhanquez.com%252Fblog%252F2008%252F09%252F26%252Fplaying-with-the-google-language-api%252F%22%2C%20%22style%22%3A%20%22big%22%2C%20%22title%22%3A%20%22Playing%20with%20the%20Google%20Language%20API%22%20%7D);"></div>
<p style="text-align: justify;">&nbsp;<BR>The <a href="http://code.google.com/apis/ajaxlanguage/" target="_blank"><strong>Google AJAX Language API</strong></a> lets you translate and detect the language of blocks of text. For non-Javascript environments, it exposes also a simple RESTful interface to use in combination with JSON to decode the result. Let&#8217;s try a simple Flex example of language detection :</p>
<p style="text-align: justify;"><a href="http://cyrilhanquez.net/simpleGoogleLanguageDetection/GoogleLanguageDetection.html" target="_blank"><strong>Demo</strong></a> <em>(right click for the source code)</em></p>
<p style="text-align: justify;">It&#8217;s pretty easy to achieve. Here is how to call the service</p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> srv<span style="color: #000000; font-weight: bold;">:</span>HTTPService = <span style="color: #0033ff; font-weight: bold;">new</span> HTTPService<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; 
srv.<span style="color: #004993;">url</span> = <span style="color: #990000;">'http://ajax.googleapis.com/ajax/services/language/detect'</span>; 
srv.request.v = <span style="color: #990000;">'1.0'</span>; <span style="color: #3f5fbf;">/*version - might change in the future*/</span> 
srv.request.q = <span style="color: #3f5fbf;">/*the text to send*/</span>; 
srv.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>ResultEvent.RESULT, <span style="color: #3f5fbf;">/*a resultHandler function*/</span><span style="color: #000000;">&#41;</span>; 
srv.<span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span>FaultEvent.FAULT, <span style="color: #3f5fbf;">/*a faultHandler function*/</span><span style="color: #000000;">&#41;</span>; 
srv.<span style="color: #004993;">send</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</pre></div></div>

</p>
<p style="text-align: justify;">The JSON Response is the following :</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
  <span style="color: #3366CC;">&quot;responseData&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;language&quot;</span> <span style="color: #339933;">:</span> the<span style="color: #339933;">-</span>detected<span style="color: #339933;">-</span>language<span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;isReliable&quot;</span> <span style="color: #339933;">:</span> the<span style="color: #339933;">-</span>reliability<span style="color: #339933;">-</span>of<span style="color: #339933;">-</span>the<span style="color: #339933;">-</span>detect<span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;confidence&quot;</span> <span style="color: #339933;">:</span> the<span style="color: #339933;">-</span>confidence<span style="color: #339933;">-</span>level<span style="color: #339933;">-</span>of<span style="color: #339933;">-</span>the<span style="color: #339933;">-</span>detect
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;responseDetails&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">|</span> string<span style="color: #339933;">-</span>on<span style="color: #339933;">-</span>error<span style="color: #339933;">,</span>
  <span style="color: #3366CC;">&quot;responseStatus&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span> <span style="color: #339933;">|</span> error<span style="color: #339933;">-</span>code
<span style="color: #009900;">&#125;</span></pre></div></div>

<p style="text-align: justify;">Okay, nice but <strong>Why am I so enthusiast about that API ? </strong></p>
<p><span id="more-187"></span></p>
<p style="text-align: justify;">Those last years, many of my projects involved to deal with translations in the 22 (formally 23 with the Gaelic) official EU languages. When I was thinking about adding more automation, I was always facing the problem of being sure about the language used in the document - It&#8217;s something that I already discussed with some guys at <a href="http://www.360flex.com/360flex_europe/" target="_blank"><strong>360|Flex Europe</strong></a> in April. Now I could achieve this quite easily :-)</p>
<p style="text-align: justify;"><img class="alignnone size-full wp-image-188" title="transexplained" src="http://cyrilhanquez.com/blog/wp-content/uploads/2008/09/transexplained.png" alt="" width="373" height="253" /></p>
<p style="text-align: justify;"> <a href="http://cyrilhanquez.net/documentLanguageDetection/documentLanguageDetection.html" target="_blank"><strong>Demo</strong></a> <em>(right click for the source code)</em></p>
<p style="text-align: justify;">Maybe it deserves a better explanation <img src='http://cyrilhanquez.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  To be able to detect the language in a document (a MS Word document only at the moment, but I will try to support more document types later), we need to send plain text to the Google API. To do this, I&#8217;m using the <a href="http://poi.apache.org/" target="_blank"><strong>Apache POI project</strong></a> and their <strong>HWPF extractor</strong> wrapper to extract the text, and then I split it in several parts of 300 characters (500 is the maximum supported but it makes my code freezing) and send them to the Google detection API. After recovering all of them, I compile and display the language(s) found. </p>
<p style="text-align: justify;">My demo is not 100% perfect but it&#8217;s working pretty well and it&#8217;s really fast. I&#8217;m using <strong>ColdFusion </strong>to upload the texts and call the POI library, and I let Flex deal with the multiple calls to the Google API. For an easier implementation, I created (or at least tried) a component <strong>detectLanguage</strong> that manage all steps from extraction to language detection.</p>
<p style="text-align: justify;">If you need some MS Word documents for testing, you can have a look <a target="_blank" href="http://ec.europa.eu/enterprise/tris/pisa/app/search/index.cfm?fuseaction=advanced&amp;lang=EN">here</a>, most of the texts are available in 22 languages since 2007. <strong>I&#8217;m waiting for you feedback and suggestions. Enjoy !</strong></p>

]]></content:encoded>
			<wfw:commentRss>http://cyrilhanquez.com/blog/2008/09/26/playing-with-the-google-language-api/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
