Twitter.translate Example of How YQL Can Solve a Problem

At the Open Hack Day in Brazil earlier this month, I was stuck with a dilemma. We offered to help people with their hacks and asked them to use Twitter for their questions. But most of the questions were actually posed in Portuguese rather than English, French, or German. I don't speak Portuguese, so I was stuck.

We were showcasing the Yahoo! Query Language (YQL) as the main ingredient of awesomeness for hacks built in 24 hours, so I thought I would use it to solve this issue. The twitter.translate table provides exactly what I needed: It searches Twitter for a specific word and adds a translation to the results. Here is an example of the effect in action:

Twitter translations embedded in a twitter stream

How it works

I used the Twitter Search API in conjunction with the Google Translation API. Both get embedded in an execute block of the twitter.translate table:

var out = '';
// make the query to Twitter
var tweets = y.query('select * from json where
url="http://search.twitter.com/search.json?q='+search
+'&rpp='+amount+'"').results.json;
// loop through the results
var rows = tweets;
for each (var result in rows.results){
// replace backslashes
var text = result.text.toString();
var text = text.replace(/\/,'');
// call the Google API
var trans = y.query('select * from google.translate where q="'+
text+'" and target="'+language+'"');
// replace backslashes
var trans = trans.results.translatedText.toString();
var trans = trans.replace(/\/,'');
// add a translatedText element to each result
result.trans +=
{trans};
}
// return to YQL
response.object = rows;

This adds the translatedText element to each result of the Twitter search. I can then use, for example, PHP to display the results:

<?php
// the YQL statement
$yql = 'select * from twitter.translate where '.
'search="brhackday" and language="en" and amount=30';
echo '<pre><code>'.$yql.'</code></pre>';
// call the YQL endpoint via cURL
echo '<p><a href="https://developer.yahoo.com/yql/console/?q='.
urlencode($yql).'&env=store%3A%2F%2Fdatatables.org%2F'.
'alltableswithkeys">Try this in the console</a></p>';
$url = 'http://query.yahooapis.com/v1/public/yql?q='.
urlencode($yql).'&format=json&'.
'env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys';
$all = json_decode(get($url));
// for debugging
echo '<!-- ';
print_r($all);
echo '-->';
// loop over results, if they are available and show them as an
// unordered list
if($all->query->results){
echo '<ul>';
foreach($all->query->results->results as $r){
echo '<li><div><img src="'.$r->profile_image_url.'" alt="">'.
$r->from_user.'</div>'.
'<p>'.$r->text.'</p>'.
'<p><strong>English:</strong> '.$r->translatedText.'</p>'.
'</li>';
}
echo '</ul>';
}
function get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>

Add a form and some validation code, and you have a translating search interface for Twitter.

Have a go at execute blocks in your own tables. It is amazing how many things you can make the YQL server do for you.

Chris Heilmann @codepo8
Yahoo! Developer Network