public
Created

Yandex Slovari (word translation)

  • Download Gist
ya-slovari.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
%w(rubygems open-uri nokogiri cgi).each {|lib| require lib}
 
# language directions: en-ru-en de-ru-de fr-ru-fr it-ru-it es-ru-es uk-ru ru-uk kk-ru ru-kk la-ru-la
 
def translate(text, lang=nil)
url = "http://m.slovari.yandex.ru/search.xml?text=#{CGI::escape(text)}"
url << "&lang=#{lang}" if lang
 
data = open(url).read
xml = Nokogiri::HTML(data)
 
{:translations => []}.tap do |r|
r[:word] = xml.css('.b-title')[0].inner_text
xml.css('.b-translate a').each do |v| r[:translations] << v.inner_text end
end
end
 
p translate('машина', 'es-ru-es')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.