Class: Smartdict::Drivers::LingvoYandexDriver
- Inherits:
-
AbstractDriver
- Object
- AbstractDriver
- Smartdict::Drivers::LingvoYandexDriver
- Defined in:
- lib/smartdict/drivers/lingvo_yandex_driver.rb
Overview
The translation driver for Google Translate service.
DISCLAIMER: It’s was written when I had one hand broken. Refactoring costs a lot of movements so that I’ve left it as it was. I’m gonna refactor it soon. – Sergey Potapov
TODO:
* Refactor
Constant Summary collapse
- USER_AGENT =
Pretend being Firefox :)
"Mozilla/5.0 (X11; U; Linux x86_64; ru; rv:1.9.1.16) Gecko/20110429 Iceweasel/3.5.16 (like Firefox/3.5.1623123)"
- HOST =
Host of Lingvo service.
"slovari.yandex.ru"
- WORD_CLASSES =
Mapping for word classes. Default is “other”
{ "имя существительное" => "noun", "имя прилагательное" => "adjective", "глагол" => "verb", "наречие" => "adverb", "предлог" => "preposition", "имя числительное" => "numeral", "междометие (часть речи)" => "interjection", "сокращение" => "abbreviation", "местоимение" => "pronoun", "союз (часть речи)" => "conjunction" }.tap{ |hash| hash.default = "other" }
Instance Attribute Summary
Attributes inherited from AbstractDriver
#from_lang, #to_lang, #transcription, #translated, #word
Instance Method Summary collapse
- #escape(str) ⇒ Object
- #get_response ⇒ Object
-
#grep_meanings(html_element) ⇒ Object
TODO: refactor.
-
#http_path ⇒ String
Http path for request to translate word.
-
#translate ⇒ Object
TODO: refactor.
Methods inherited from AbstractDriver
#build_translation, #initialize, set_name, translate
Constructor Details
This class inherits a constructor from Smartdict::Drivers::AbstractDriver
Instance Method Details
#escape(str) ⇒ Object
134 135 136 |
# File 'lib/smartdict/drivers/lingvo_yandex_driver.rb', line 134 def escape(str) CGI.escape(str) end |
#get_response ⇒ Object
117 118 119 120 121 |
# File 'lib/smartdict/drivers/lingvo_yandex_driver.rb', line 117 def get_response http = Net::HTTP.new(HOST, 80) request = Net::HTTP::Get.new(http_path, { "User-Agent" => USER_AGENT }) http.request(request).read_body end |
#grep_meanings(html_element) ⇒ Object
TODO: refactor
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/smartdict/drivers/lingvo_yandex_driver.rb', line 69 def grep_meanings(html_element) acronym = html_element.css("acronym").first return unless acronym ru_word_class = acronym["title"] word_class = WORD_CLASSES[ru_word_class] translations = [] html_element.css("ul > li").each do |tr| # a text line with translations separated by commas line = "" # use strong tag as an anchor strong = tr.css("strong").first if strong && strong.text =~ /\d+|[а-я]+\)/ node = strong while(node = node.next_sibling) if node.text? || node.name == "a" text = node.text line << text unless text =~ /\(|\)/ elsif ["em", "acronym"].include? node.name next else break end end end translations += words_from_line(line) end # sometimes there is only one meaning if translations.empty? if a_tag = html_element.css("span > a").first line = a_tag.text elsif span = html_element.css("span").first line = span.text elsif i_tag = html_element.xpath("i[2]") line = i_tag.text else return nil end translations = words_from_line(line) end self.translated[word_class] = translations.uniq end |
#http_path ⇒ String
Returns http path for request to translate word.
124 125 126 127 128 129 130 131 132 |
# File 'lib/smartdict/drivers/lingvo_yandex_driver.rb', line 124 def http_path phrase = case [from_lang, to_lang] when ["en", "ru"] then "en-ru" when ["ru", "en"] then "ru-en" else raise Smartdict::TranslationNotFound end "/#{escape(word)}/#{phrase}/" end |
#translate ⇒ Object
TODO: refactor
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/smartdict/drivers/lingvo_yandex_driver.rb', line 43 def translate doc = Nokogiri::HTML(get_response) if main = doc.css("div.b-translation__article > ul > li#I").first else main = doc.css("div.b-translation__article").first end raise Smartdict::TranslationNotFound unless main # Fetch transcription self.transcription = doc.css("span.b-translation__tr").first.try(:text) self.translated = {} if main.xpath("./i/acronym").any? grep_meanings(main) else main.xpath("./ul/li").each do |li| grep_meanings(li) end end end |