Skip to content

Instantly share code, notes, and snippets.

@RohitR
Forked from stpnlr/example
Created October 2, 2013 08:52
Show Gist options
  • Save RohitR/6790882 to your computer and use it in GitHub Desktop.
Save RohitR/6790882 to your computer and use it in GitHub Desktop.
ml =GoogleTransliterate.new('poda poda').transliterated_text
require 'json'
require 'open-uri'
class GoogleTransliterate
API_URL = "http://www.google.com/uds/Gtransliterate"
EN_ML_PARAM = "?context=22&langpair=en|ml&key=notsupplied&v=1.0"
def initialize(words_array)
if words_array.length > 5
raise 'TooManyWords'
else
@words = words_array
end
end
def param
'&q='+@words.join('&q=')
end
def result
@result ||=JSON.parse(open(URI.encode(API_URL+EN_ML_PARAM+param)).read)
end
def transliterated_text
result_str = result
if result_str['responseStatus']==200
resp = result_str['responseData']
mal = ''
unless resp.nil?
result_str['responseData']['transliterations'].each do |trans|
mal += (trans['transliteratedWords'][0] unless trans['transliteratedWords'].nil?)||trans['sourceWord']
end
end
mal
else
raise result_str['responseDetails']
nil
end
end
end
class Transliterator
def initialize(source_text)
words = source_text.split(/(\W)/)
@words = words.each_slice(5)
end
def result
result_text = ''
@words.each do |words|
ml =GoogleTransliterate.new(words).transliterated_text
result_text += ml
end
result_text
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment