PearsonLongman-Dictionary 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7592938025c5ab2d124294261dc788132361c74c
4
+ data.tar.gz: 999f9964d00bc5b0d43eefef9547030464e65bdd
5
+ SHA512:
6
+ metadata.gz: 40865771a407c61fd3ba1675da99733ac1344a73fd0b3aa83b185de7adcfbc716625b91ee41609edb82a99fb53c48673f0f1a24924d8cfc05674a478663a2d05
7
+ data.tar.gz: 8d54fcbb289649815275226cc0cd44c002bad586f95a4019e4fdc8bff0ba6ad9026d7356277f85937935b7f765deb9eaa8df95f436136901117d0e7073434a31
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Nitish Parkar
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Pearson Longman Dictionary
2
+
3
+
4
+ A ruby gem that wraps [Pearson dictionary API](http://developer.pearson.com/apis/dictionaries)
5
+
6
+ ## Getting started
7
+
8
+ You can add it to your Gemfile with:
9
+
10
+ ```ruby
11
+ gem 'pearsonlongman-dictionary'
12
+ ```
13
+ or
14
+
15
+ You can install it directly
16
+
17
+ gem install pearsonlongman-dictionary
18
+
19
+ and then require it
20
+
21
+ require 'pearsonlongman-dictionary'
22
+
23
+ ## Usage
24
+
25
+ You can lookup a word definition by:
26
+
27
+ ```ruby
28
+ results = DictionaryLookup::Base.define("hello")
29
+ ```
30
+
31
+ results will contain an array of DictionaryLookup::Definition objects.
32
+ ```ruby
33
+ results.count # => 1
34
+ results.first.part_of_speech # => "noun"
35
+ results.first.denotation # => "used as a greeting when you see or meet someone"
36
+ results.first.examples # => ["Hello, John! How are you?"]
37
+ ```
38
+
39
+ You can specify different dictionary by passing `dictionary` in config hash. List of available dictionaries could be found [here](http://developer.pearson.com/apis/dictionaries#!//listDictionaryEntries)
40
+
41
+ ```ruby
42
+ results = DictionaryLookup::Base.define("hello", {dictionary: "wordwise"})
43
+ ```
@@ -0,0 +1,14 @@
1
+ require "PearsonLongman-Dictionary/pearson"
2
+
3
+ module DictionaryLookup
4
+ class Base
5
+ # Handles a lookup request
6
+ #
7
+ # @param (see Pearson.define)
8
+ # @return (see Pearson.define)
9
+ # @raise (see Pearson.define)
10
+ def self.define(term, config={})
11
+ Pearson.define(term, config)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ module DictionaryLookup
2
+ class Definition
3
+ attr_accessor :headword, :part_of_speech, :denotation, :examples
4
+
5
+ def initialize(headword, part_of_speech, denotation, examples)
6
+ @headword = headword
7
+ @part_of_speech = part_of_speech
8
+ @denotation = denotation
9
+ @examples = examples
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,60 @@
1
+ require "net/http"
2
+ require "json"
3
+ require "PearsonLongman-Dictionary/definition"
4
+
5
+ module DictionaryLookup
6
+ class Pearson
7
+
8
+ VALID_DICTIONARIES = ["ldoce5", "lasde", "ldec", "wordwise", "laesd", "leasd", "laad3", "laes", "lase", "brep", "brpe"]
9
+
10
+ DEFAULT_DICTIONARY = VALID_DICTIONARIES.first
11
+
12
+ # Helper method. Returns either a valid dictionary specified through config
13
+ # or the default dictionary
14
+ def self.get_dictionary(config)
15
+ VALID_DICTIONARIES.include?(config[:dictionary]) ?
16
+ config[:dictionary] : DEFAULT_DICTIONARY
17
+ end
18
+
19
+ # Fetches term definitions from Pearson dictionary API
20
+ #
21
+ # @param term [String] term to be defined
22
+ # @param config [Hash] configuration hash, for now supports :dictionary key.
23
+ # For list of valid values see {DictionaryLookup::Pearson::VALID_DICTIONARIES}
24
+ # @return [Array] an array of {DictionaryLookup::Definition} objects
25
+ # @raise SocketError if not connected to the internet
26
+ def self.define(term, config)
27
+ dictionary = get_dictionary(config)
28
+
29
+ url = "https://api.pearson.com/v2/dictionaries/#{dictionary}/entries?headword=#{term}"
30
+ uri = URI(URI.escape(url))
31
+
32
+ response = Net::HTTP.get(uri)
33
+ data = JSON.parse(response)
34
+
35
+ # Select definitions that match exactly with the term
36
+ results = data["results"].select{ |d| dhead = d["headword"].downcase
37
+
38
+ d["headword"].downcase == term.downcase or dhead[term.length] == ","
39
+ }
40
+ definitions = []
41
+
42
+ results.each do |result|
43
+ headword = result["headword"]
44
+ part_of_speech = result["part_of_speech"]
45
+ # In wordwise dictionary, value of definition is String. Hence the is_a? test
46
+ _definitions = result["senses"].first["definition"]
47
+ denotation = _definitions.is_a?(Array) ? _definitions.first : _definitions
48
+ if result["senses"].first["examples"].nil?
49
+ examples = []
50
+ else
51
+ examples = result["senses"].first["examples"].map{|e| e["text"]}
52
+ end
53
+ definitions << DictionaryLookup::Definition.new(headword, part_of_speech, denotation, examples)
54
+ end
55
+
56
+ definitions
57
+ end
58
+
59
+ end
60
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: PearsonLongman-Dictionary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Arnold Tonderai Marunda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ description:
42
+ email: arnold-fungai@live.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE
48
+ - README.md
49
+ - lib/PearsonLongman-Dictionary.rb
50
+ - lib/PearsonLongman-Dictionary/definition.rb
51
+ - lib/PearsonLongman-Dictionary/pearson.rb
52
+ homepage: https://github.com/arnoldfungai/PearsonLongman-Dictionary
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.0.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.4.8
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: A ruby gem that wraps Pearson Dictionary API
76
+ test_files: []