Sha256: a219818e6b589551f2dacf43fbe1b80b70f4c926721264f56d38c4333ac9895f

Contents?: true

Size: 745 Bytes

Versions: 1

Compression:

Stored size: 745 Bytes

Contents

# -*- coding: utf-8 -*-

require 'nokogiri'
require 'dict/dictionary'

WIKI_URL = 'http://en.wiktionary.org/wiki/'

module Dict
  class Wiktionary < Dictionary
    # Method returns hash with translations as keys and examples of using words as values
    def translate
      context_words = []
      url = 'http://en.wiktionary.org/wiki/'
      get_html(@uri).css('p + ol li a').each do |node|
        get_html(url + node.text.tr(' ', '_')).css('p + ol > li dl dd').each do |example|
          context_words << node.text << example.text
        end
      end
      make_hash_results(context_words)
    end

    def get_html(url)
      Nokogiri::HTML(open(url))
    rescue OpenURI::HTTPError
      raise Dictionary::ConnectError
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dict-0.2.1 lib/dict/wiktionary.rb