Class: DictionaryRB::Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/dictionary-rb/dictionary.rb

Overview

Parses the page for a word at 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 ENDPOINT so as to make it quickly generate the other results, once a URL is parsed.

Constant Summary collapse

PREFIX =

Endpoint for Reference Dictionary

"http://dictionary.reference.com/browse/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word) ⇒ Dictionary

Returns a new instance of Dictionary.

Examples:

word = DictionaryRB::Dictionary.new('question')

Parameters:

  • word (String)

    The word for Reference Dictionary



17
18
19
20
21
# 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

Instance Attribute Details

#wordObject (readonly)

The associated word



10
11
12
# File 'lib/dictionary-rb/dictionary.rb', line 10

def word
  @word
end

Instance Method Details

#examplesArray

Returns containing the examples.

Returns:

  • (Array)

    containing the examples.



64
65
66
67
68
# 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

#meaningString

Fetches and gives first meaning for the word

Examples:

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

Returns:

  • (String)

    containing the meaning for the word

See Also:



53
54
55
# File 'lib/dictionary-rb/dictionary.rb', line 53

def meaning
  meanings.first
end

#meaningsArray

Note:

This method will hit the ENDPOINT and will consume some time to generate result

Fetches and gives meanings for word from Reference Dictionary

Examples:

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",... ]

Returns:

  • (Array)

    containing the meanings for the word

See Also:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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_wordsArray Also known as: synonyms

Fetches and gives synonyms for the word

Examples:

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

Returns:

  • (Array)

    containing similar words



75
76
77
78
79
# 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

#to_sObject



82
83
84
# File 'lib/dictionary-rb/dictionary.rb', line 82

def to_s
  sprintf("Free Dictionary (word: %s, meaning: %s", @word, @meaning)
end