Sha256: 0f62ee7df59e4bac42d94717ea4f55ea131a6a2cabade158915e1bf2ee1b7ad9

Contents?: true

Size: 1.51 KB

Versions: 3

Compression:

Stored size: 1.51 KB

Contents

require "synonymous/word"

module Synonymous
  class Sense
    def initialize(data)
      if data.length > 1
        pp data
        raise Synonymous::Error, "Sense Sequence has more than one element"
      end
      unless data[0][0] == "sense"
        pp data[0]
        raise Synonymous::Error, "Sense didn't start with keyword 'sense'"
      end
      unless data[0].length == 2
        pp data[0]
        raise Synonymous::Error, "Sense has more than two entries"
      end

      @data = data[0][1]
    end

    def number
      # https://dictionaryapi.com/products/json#sec-2.sn
      @data["sn"]
    end

    def to_s
      # https://dictionaryapi.com/products/json#sec-2.dt
      @data.fetch("dt").to_h.fetch("text").strip
    end

    def synonyms
      # https://dictionaryapi.com/products/json#sec-3.synlist
      @data.fetch("syn_list", []).flatten.map(&Word.method(:new))
    end

    def related_words
      # https://dictionaryapi.com/products/json#sec-3.rellist
      @data.fetch("rel_list", []).flatten.map(&Word.method(:new))
    end

    def synonymous_phrases
      # https://dictionaryapi.com/products/json#sec-3.phraselist
      @data.fetch("phrase_list", []).flatten.map(&Word.method(:new))
    end

    def antonyms
      # https://dictionaryapi.com/products/json#sec-3.antlist
      @data.fetch("ant_list", []).flatten.map(&Word.method(:new))
    end

    def near_antonyms
      # https://dictionaryapi.com/products/json#sec-3.nearlist
      @data.fetch("near_list", []).flatten.map(&Word.method(:new))
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
synonymous-0.3.0 lib/synonymous/sense.rb
synonymous-0.2.0 lib/synonymous/sense.rb
synonymous-0.1.0 lib/synonymous/sense.rb