Class: Camdict::Word

Inherits:
Object
  • Object
show all
Defined in:
lib/camdict/word.rb

Overview

Get all definitions data about a word or phrase including IPAs, pronunciations, usage sentences, etc. from Cambridge dictionary.

Instance Method Summary collapse

Constructor Details

#initialize(word, dictionary = nil) ⇒ Word

New a word or phrase, default dictionary is british.



10
11
12
13
# File 'lib/camdict/word.rb', line 10

def initialize(word, dictionary = nil)
  @word = word
  @dictionary = dictionary
end

Instance Method Details

#definitionsObject Also known as: definition

Get all definitions for this word from remote online dictionary



59
60
61
# File 'lib/camdict/word.rb', line 59

def definitions
  @definitions ||= g_definitions
end

#ipa(region = :uk) ⇒ Object



26
27
28
# File 'lib/camdict/word.rb', line 26

def ipa(region = :uk)
  definition.ipa.send(region)
end

#meaningObject



30
31
32
# File 'lib/camdict/word.rb', line 30

def meaning
  definition.senses.first.explanations.first.meaning
end

#meaningsObject



34
35
36
# File 'lib/camdict/word.rb', line 34

def meanings
  definition.senses.map { |s| s.explanations.map(&:meaning) }.flatten
end

#part_of_speechObject Also known as: pos



15
16
17
18
19
# File 'lib/camdict/word.rb', line 15

def part_of_speech
  s = definition.senses.map(&:part_of_speech).uniq
  return s.first if s.count < 2
  s
end


53
54
55
56
# File 'lib/camdict/word.rb', line 53

def print
  require 'pp'
  pp show
end

#pronunciation(region = :uk) ⇒ Object



21
22
23
24
# File 'lib/camdict/word.rb', line 21

def pronunciation(region = :uk)
  p = definition.pronunciation.send(region)
  p.mp3 || p.ogg
end

#raw_definitionObject



63
64
65
# File 'lib/camdict/word.rb', line 63

def raw_definition
  @raw_definition ||= retrieve.to_html(save_with: 0)
end

#showObject

show all important dictionary information, returns { meaning: [{ pos: ”, category: ”,

          sense: [{ meaning:, eg: [], level: '', code: '', synonym: '',
                   opposite: '', usage: '', region: ''}] }]
ipa: '' | { uk: , us: },
pronunciation: { uk: mp3|ogg, us: mp3|ogg }

}



45
46
47
48
49
50
51
# File 'lib/camdict/word.rb', line 45

def show
  {
    meaning: meanings_json,
    ipa: ipa_json,
    pronunciation: { uk: pronunciation, us: pronunciation(:us) }
  }
end