class DictionaryRB::Dictionary

Parses the page for a word at {dictionary.reference.com Reference Dictionary} and extracts the {#meanings}, {#examples} and {#similar_words} for it. Lot of take care has been taken to prevent it from hitting the {PREFIX ENDPOINT} so as to make it quickly generate the other results, once a URL is parsed.

Constants

PREFIX

Endpoint for Reference Dictionary

Attributes

word[R]

The associated word

Public Class Methods

new(word) click to toggle source

@param word [String] The word for Reference Dictionary @example

word = DictionaryRB::Dictionary.new('question')
# File lib/dictionary-rb/dictionary.rb, line 17
def initialize(word)
  @word = word if word.is_a? String
  @word = word.word if word.is_a? Word
  @examples = Array.new
end

Public Instance Methods

examples() click to toggle source

@return [Array] containing the examples.

# File lib/dictionary-rb/dictionary.rb, line 64
def examples
  @doc ||= Nokogiri::HTML(open(PREFIX + CGI::escape(@word)))
  @example_results ||= @doc.css('.exsentences').map{ |x| x.text.strip }.reject(&:empty?).flatten
  @example_results #to prevent above computations on repeated call on object
end
meaning() click to toggle source

Fetches and gives first meaning for the word @example

word.meaning
#=> "a sentence in an interrogative form, addressed to someone in order to get information in reply"

@see meanings @return [String] containing the meaning for the word

# File lib/dictionary-rb/dictionary.rb, line 53
def meaning
  meanings.first
end
meanings() click to toggle source

Fetches and gives meanings for word from Reference Dictionary @note This method will hit the {PREFIX ENDPOINT} and will consume some time to generate result @example

word.meanings
#=> ["a sentence in an interrogative form, addressed to someone in order to get information in reply",
     "a problem for discussion or under discussion",
     "a matter for investigation",... ]

@see meaning @return [Array] containing the meanings for the word

# File lib/dictionary-rb/dictionary.rb, line 32
def meanings
  url = PREFIX + CGI::escape(@word)
  @doc = Nokogiri::HTML(open(url))

  nodes = [@doc.css('.luna-Ent .dndata')]
  nodes = [@doc.css('.pbk .luna-Ent')] if nodes.flatten.empty?

  (nodes ||= []).push(@doc.css(".td3n2")).flatten!
  results = nodes.map(&:text).map do |result|
    result.split ':'
  end.map { |x| x[0].split(/[.;]/) }.flatten.map(&:strip).reject(&:empty?)
  @meaning = results.first
  results
end
similar_words() click to toggle source

Fetches and gives synonyms for the word @example

word.similar_words
#=> => ["answer", "inquire", "question mark", "sentence",.. ]

@return [Array] containing similar words

# File lib/dictionary-rb/dictionary.rb, line 75
def similar_words
  @doc ||= Nokogiri::HTML(open(PREFIX + CGI::escape(@word)))
  @similar_words = @doc.css("#relatedwords .fla a").map(&:text).reject(&:empty?)
  @similar_words
end
Also aliased as: synonyms
synonyms()
Alias for: similar_words
to_s() click to toggle source
# File lib/dictionary-rb/dictionary.rb, line 82
def to_s
  sprintf("Free Dictionary (word: %s, meaning: %s", @word, @meaning)
end