Class: DictionaryRB::Dictionary
- Inherits:
-
Object
- Object
- DictionaryRB::Dictionary
- 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
-
#word ⇒ Object
readonly
The associated word.
Instance Method Summary collapse
-
#examples ⇒ Array
Containing the examples.
-
#initialize(word) ⇒ Dictionary
constructor
A new instance of Dictionary.
-
#meaning ⇒ String
Fetches and gives first meaning for the word.
-
#meanings ⇒ Array
Fetches and gives meanings for word from Reference Dictionary.
-
#similar_words ⇒ Array
(also: #synonyms)
Fetches and gives synonyms for the word.
- #to_s ⇒ Object
Constructor Details
#initialize(word) ⇒ Dictionary
Returns a new instance of 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
#word ⇒ Object (readonly)
The associated word
10 11 12 |
# File 'lib/dictionary-rb/dictionary.rb', line 10 def word @word end |
Instance Method Details
#examples ⇒ Array
Returns 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 |
#meaning ⇒ String
Fetches and gives first meaning for the word
53 54 55 |
# File 'lib/dictionary-rb/dictionary.rb', line 53 def meaning meanings.first end |
#meanings ⇒ Array
This method will hit the ENDPOINT and will consume some time to generate result
Fetches and gives meanings for word from Reference Dictionary
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_words ⇒ Array Also known as: synonyms
Fetches and gives synonyms for the word
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_s ⇒ Object
82 83 84 |
# File 'lib/dictionary-rb/dictionary.rb', line 82 def to_s sprintf("Free Dictionary (word: %s, meaning: %s", @word, @meaning) end |