rosette_api 1.8.0 → 1.14.4

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.
Files changed (43) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +13 -0
  3. data/README.md +38 -0
  4. data/examples/README.md +36 -0
  5. data/examples/address_similarity.rb +29 -0
  6. data/examples/categories.rb +22 -0
  7. data/examples/entities.rb +23 -0
  8. data/examples/info.rb +20 -0
  9. data/examples/language.rb +23 -0
  10. data/examples/language_multilingual.rb +25 -0
  11. data/examples/morphology_complete.rb +22 -0
  12. data/examples/morphology_compound-components.rb +24 -0
  13. data/examples/morphology_han-readings.rb +22 -0
  14. data/examples/morphology_lemmas.rb +22 -0
  15. data/examples/morphology_parts-of-speech.rb +22 -0
  16. data/examples/name_deduplication.rb +25 -0
  17. data/examples/name_similarity.rb +28 -0
  18. data/examples/name_translation.rb +26 -0
  19. data/examples/ping.rb +20 -0
  20. data/examples/relationships.rb +23 -0
  21. data/examples/semantic_vectors.rb +22 -0
  22. data/examples/sentences.rb +24 -0
  23. data/examples/sentiment.rb +26 -0
  24. data/examples/similar_terms.rb +23 -0
  25. data/examples/syntax_dependencies.rb +23 -0
  26. data/examples/tokens.rb +22 -0
  27. data/examples/topics.rb +23 -0
  28. data/examples/transliteration.rb +24 -0
  29. data/lib/address_parameter.rb +117 -0
  30. data/lib/address_similarity_parameters.rb +49 -0
  31. data/lib/bad_request_error.rb +2 -0
  32. data/lib/bad_request_format_error.rb +2 -0
  33. data/lib/document_parameters.rb +20 -11
  34. data/lib/name_deduplication_parameters.rb +15 -7
  35. data/lib/name_parameter.rb +6 -3
  36. data/lib/name_similarity_parameters.rb +14 -6
  37. data/lib/name_translation_parameters.rb +12 -6
  38. data/lib/request_builder.rb +49 -18
  39. data/lib/rosette_api.rb +193 -66
  40. data/lib/rosette_api_error.rb +3 -1
  41. metadata +43 -14
  42. data/.rubocop.yml +0 -312
  43. data/tests/tests_spec.rb +0 -583
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rosette_api'
4
+
5
+ api_key, url = ARGV
6
+
7
+ rosette_api = if url
8
+ RosetteAPI.new(api_key, url)
9
+ else
10
+ RosetteAPI.new(api_key)
11
+ end
12
+
13
+ begin
14
+ response = rosette_api.ping
15
+ puts JSON.pretty_generate(response)
16
+ rescue RosetteAPIError => e
17
+ printf('Rosette API Error (%<status_code>s): %<message>s',
18
+ status_code: e.status_code,
19
+ message: e.message)
20
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rosette_api'
4
+
5
+ api_key, url = ARGV
6
+
7
+ rosette_api = if url
8
+ RosetteAPI.new(api_key, url)
9
+ else
10
+ RosetteAPI.new(api_key)
11
+ end
12
+
13
+ relationships_text_data = 'FLIR Systems is headquartered in Oregon and produces thermal imaging, night vision, and infrared cameras and sensor systems. According to the SEC\'s order instituting a settled administrative proceeding, FLIR entered into a multi-million dollar contract to provide thermal binoculars to the Saudi government in November 2008. Timms and Ramahi were the primary sales employees responsible for the contract, and also were involved in negotiations to sell FLIR\'s security cameras to the same government officials. At the time, Timms was the head of FLIR\'s Middle East office in Dubai.'
14
+
15
+ begin
16
+ params = DocumentParameters.new(content: relationships_text_data)
17
+ response = rosette_api.get_relationships(params)
18
+ puts JSON.pretty_generate(response)
19
+ rescue RosetteAPIError => e
20
+ printf('Rosette API Error (%<status_code>s): %<message>s',
21
+ status_code: e.status_code,
22
+ message: e.message)
23
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rosette_api'
4
+
5
+ api_key, url = ARGV
6
+
7
+ rosette_api = if url
8
+ RosetteAPI.new(api_key, url)
9
+ else
10
+ RosetteAPI.new(api_key)
11
+ end
12
+
13
+ semantic_vectors_data = 'Cambridge, Massachusetts'
14
+ begin
15
+ params = DocumentParameters.new(content: semantic_vectors_data)
16
+ response = rosette_api.get_semantic_vectors(params)
17
+ puts JSON.pretty_generate(response)
18
+ rescue RosetteAPIError => e
19
+ printf('Rosette API Error (%<status_code>s): %<message>s',
20
+ status_code: e.status_code,
21
+ message: e.message)
22
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rosette_api'
4
+
5
+ api_key, url = ARGV
6
+
7
+ rosette_api = if url
8
+ RosetteAPI.new(api_key, url)
9
+ else
10
+ RosetteAPI.new(api_key)
11
+ end
12
+
13
+ sentences_data = 'This land is your land. This land is my land, from California to the New York island; from the red wood forest to the Gulf Stream waters. This land was made for you and Me. As I was walking that ribbon of highway, I saw above me that endless skyway: I saw below me that golden valley: This land was made for you and me.'
14
+
15
+ begin
16
+ params = DocumentParameters.new
17
+ params.content = sentences_data
18
+ response = rosette_api.get_sentences(params)
19
+ puts JSON.pretty_generate(response)
20
+ rescue RosetteAPIError => e
21
+ printf('Rosette API Error (%<status_code>s): %<message>s',
22
+ status_code: e.status_code,
23
+ message: e.message)
24
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tempfile'
4
+ require 'rosette_api'
5
+
6
+ api_key, url = ARGV
7
+
8
+ rosette_api = if url
9
+ RosetteAPI.new(api_key, url)
10
+ else
11
+ RosetteAPI.new(api_key)
12
+ end
13
+
14
+ file = Tempfile.new(%w[foo .html])
15
+ sentiment_file_data = '<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn\'t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, "The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy."</p></body></html>'
16
+ file.write(sentiment_file_data)
17
+ file.close
18
+ begin
19
+ params = DocumentParameters.new(file_path: file.path, language: 'eng')
20
+ response = rosette_api.get_sentiment(params)
21
+ puts JSON.pretty_generate(response)
22
+ rescue RosetteAPIError => e
23
+ printf('Rosette API Error (%<status_code>s): %<message>s',
24
+ status_code: e.status_code,
25
+ message: e.message)
26
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rosette_api'
4
+
5
+ api_key, url = ARGV
6
+
7
+ rosette_api = if url
8
+ RosetteAPI.new(api_key, url)
9
+ else
10
+ RosetteAPI.new(api_key)
11
+ end
12
+
13
+ similar_terms_data = 'spy'
14
+ begin
15
+ params = DocumentParameters.new(content: similar_terms_data)
16
+ params.rosette_options = { 'resultLanguages' => %w[spa deu jpn] }
17
+ response = rosette_api.get_similar_terms(params)
18
+ puts JSON.pretty_generate(response)
19
+ rescue RosetteAPIError => e
20
+ printf('Rosette API Error (%<status_code>s): %<message>s',
21
+ status_code: e.status_code,
22
+ message: e.message)
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rosette_api'
4
+
5
+ api_key, url = ARGV
6
+
7
+ rosette_api = if url
8
+ RosetteAPI.new(api_key, url)
9
+ else
10
+ RosetteAPI.new(api_key)
11
+ end
12
+
13
+ syntax_dependencies_data = 'Yoshinori Ohsumi, a Japanese cell biologist, was awarded the Nobel Prize in Physiology or Medicine on Monday.'
14
+
15
+ begin
16
+ params = DocumentParameters.new(content: syntax_dependencies_data)
17
+ response = rosette_api.get_syntax_dependencies(params)
18
+ puts JSON.pretty_generate(response)
19
+ rescue RosetteAPIError => e
20
+ printf('Rosette API Error (%<status_code>s): %<message>s',
21
+ status_code: e.status_code,
22
+ message: e.message)
23
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rosette_api'
4
+
5
+ api_key, url = ARGV
6
+
7
+ rosette_api = if url
8
+ RosetteAPI.new(api_key, url)
9
+ else
10
+ RosetteAPI.new(api_key)
11
+ end
12
+
13
+ tokens_data = '北京大学生物系主任办公室内部会议'
14
+ begin
15
+ params = DocumentParameters.new(content: tokens_data)
16
+ response = rosette_api.get_tokens(params)
17
+ puts JSON.pretty_generate(response)
18
+ rescue RosetteAPIError => e
19
+ printf('Rosette API Error (%<status_code>s): %<message>s',
20
+ status_code: e.status_code,
21
+ message: e.message)
22
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rosette_api'
4
+
5
+ api_key, url = ARGV
6
+
7
+ rosette_api = if url
8
+ RosetteAPI.new(api_key, url)
9
+ else
10
+ RosetteAPI.new(api_key)
11
+ end
12
+
13
+ topics_data = 'Lily Collins is in talks to join Nicholas Hoult in Chernin Entertainment and Fox Searchlight\'s J.R.R. Tolkien biopic Tolkien. Anthony Boyle, known for playing Scorpius Malfoy in the British play Harry Potter and the Cursed Child, also has signed on for the film centered on the famed author. In Tolkien, Hoult will play the author of the Hobbit and Lord of the Rings book series that were later adapted into two Hollywood trilogies from Peter Jackson. Dome Karukoski is directing the project.'
14
+
15
+ begin
16
+ params = DocumentParameters.new(content: topics_data)
17
+ response = rosette_api.get_topics(params)
18
+ puts JSON.pretty_generate(response)
19
+ rescue RosetteAPIError => e
20
+ printf('Rosette API Error (%<status_code>s): %<message>s',
21
+ status_code: e.status_code,
22
+ message: e.message)
23
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rosette_api'
4
+
5
+ api_key, url = ARGV
6
+
7
+ rosette_api = if url
8
+ RosetteAPI.new(api_key, url)
9
+ else
10
+ RosetteAPI.new(api_key)
11
+ end
12
+
13
+ transliteration_content_data = 'ana r2ye7 el gam3a el sa3a 3 el 3asr'
14
+
15
+ begin
16
+ params = DocumentParameters.new
17
+ params.content = transliteration_content_data
18
+ response = rosette_api.get_transliteration(params)
19
+ puts JSON.pretty_generate(response)
20
+ rescue RosetteAPIError => e
21
+ printf('Rosette API Error (%<status_code>s): %<message>s',
22
+ status_code: e.status_code,
23
+ message: e.message)
24
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This class represents an address in Rosette API.
4
+ class AddressParameter
5
+ # house (optional)
6
+ attr_accessor :house
7
+ # house_number (optional)
8
+ attr_accessor :house_number
9
+ # road (optional)
10
+ attr_accessor :road
11
+ # unit (optional)
12
+ attr_accessor :unit
13
+ # level (optional)
14
+ attr_accessor :level
15
+ # staircase (optional)
16
+ attr_accessor :staircase
17
+ # entrance (optional)
18
+ attr_accessor :entrance
19
+ # suburb (optional)
20
+ attr_accessor :suburb
21
+ # city_district (optional)
22
+ attr_accessor :city_district
23
+ # city (optional)
24
+ attr_accessor :city
25
+ # island (optional)
26
+ attr_accessor :island
27
+ # state_district (optional)
28
+ attr_accessor :state_district
29
+ # state (optional)
30
+ attr_accessor :state
31
+ # country_region (optional)
32
+ attr_accessor :country_region
33
+ # country (optional)
34
+ attr_accessor :country
35
+ # world_region (optional)
36
+ attr_accessor :world_region
37
+ # post_code (optional)
38
+ attr_accessor :post_code
39
+ # po_box (optional)
40
+ attr_accessor :po_box
41
+
42
+ def initialize(options = {}) #:notnew:
43
+ options = {
44
+ house: nil,
45
+ house_number: nil,
46
+ road: nil,
47
+ unit: nil,
48
+ level: nil,
49
+ staircase: nil,
50
+ entrance: nil,
51
+ suburb: nil,
52
+ city_district: nil,
53
+ city: nil,
54
+ island: nil,
55
+ state_district: nil,
56
+ state: nil,
57
+ country_region: nil,
58
+ country: nil,
59
+ world_region: nil,
60
+ post_code: nil,
61
+ po_box: nil
62
+ }.update options
63
+ @house = options[:house]
64
+ @house_number = options[:house_number]
65
+ @road = options[:road]
66
+ @unit = options[:unit]
67
+ @level = options[:level]
68
+ @staircase = options[:staircase]
69
+ @entrance = options[:entrance]
70
+ @suburb = options[:suburb]
71
+ @city_district = options[:city_district]
72
+ @city = options[:city]
73
+ @island = options[:island]
74
+ @state_district = options[:state_district]
75
+ @state = options[:state]
76
+ @country_region = options[:country_region]
77
+ @country = options[:country]
78
+ @world_region = options[:world_region]
79
+ @post_code = options[:post_code]
80
+ @po_box = options[:po_box]
81
+ end
82
+
83
+ # Converts this class to Hash with its keys in lower CamelCase.
84
+ #
85
+ # Returns the new Hash.
86
+ def load_param
87
+ to_hash.select { |_key, value| value }
88
+ .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
89
+ .to_h
90
+ end
91
+
92
+ # Converts this class to Hash.
93
+ #
94
+ # Returns the new Hash.
95
+ def to_hash
96
+ {
97
+ house: @house,
98
+ house_number: @house_number,
99
+ road: @road,
100
+ unit: @unit,
101
+ level: @level,
102
+ staircase: @staircase,
103
+ entrance: @entrance,
104
+ suburb: @suburb,
105
+ city_district: @city_district,
106
+ city: @city,
107
+ island: @island,
108
+ state_district: @state_district,
109
+ state: @state,
110
+ country_region: @country_region,
111
+ country: @country,
112
+ world_region: @world_region,
113
+ post_code: @post_code,
114
+ po_box: @po_box
115
+ }
116
+ end
117
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'bad_request_error'
4
+ require_relative 'address_parameter'
5
+
6
+ # This class encapsulates parameters that are needed for address-similarity in
7
+ # Rosette API.
8
+ class AddressSimilarityParameters
9
+ # Address to be compared to address2
10
+ attr_accessor :address1
11
+ # Address to be compared to address1
12
+ attr_accessor :address2
13
+
14
+ def initialize(address1, address2) #:notnew:
15
+ @address1 = address1
16
+ @address2 = address2
17
+ end
18
+
19
+ # Validates the parameters by checking if address1 and address2 are instances
20
+ # of AddressParameters or Strings.
21
+ def validate_params
22
+ a1_msg = 'address1 option can only be an instance of an AddressParameter or a String'
23
+ raise BadRequestError.new(a1_msg) if [String, AddressParameter].none? { |clazz| @address1.is_a? clazz }
24
+
25
+ a2_msg = 'address2 option can only be an instance of an AddressParameter or a String'
26
+ raise BadRequestError.new(a2_msg) if [String, AddressParameter].none? { |clazz| @address2.is_a? clazz }
27
+ end
28
+
29
+ # Converts this class to Hash with its keys in lower CamelCase.
30
+ #
31
+ # Returns the new Hash.
32
+ def load_params
33
+ validate_params
34
+ to_hash
35
+ .reject { |_key, value| value.nil? }
36
+ .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
37
+ .to_h
38
+ end
39
+
40
+ # Converts this class to Hash.
41
+ #
42
+ # Returns the new Hash.
43
+ def to_hash
44
+ {
45
+ address1: @address1.is_a?(AddressParameter) ? @address1.load_param : @address1,
46
+ address2: @address2.is_a?(AddressParameter) ? @address2.load_param : @address2
47
+ }
48
+ end
49
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'rosette_api_error'
2
4
 
3
5
  # This class represents Rosette API errors with badRequest status_code.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'rosette_api_error'
2
4
 
3
5
  # This class represents Rosette API errors with badRequestFormat status_code.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'bad_request_format_error'
2
4
 
3
5
  # This class encapsulates parameters that will be used by most of the endpoints
@@ -5,9 +7,11 @@ require_relative 'bad_request_format_error'
5
7
  class DocumentParameters
6
8
  # Content to be analyzed (required if no content_uri and file_path)
7
9
  attr_accessor :content
8
- # URL to retrieve content from and analyze (required if no content and file_path)
10
+ # URL to retrieve content from and analyze (required if no content and
11
+ # file_path)
9
12
  attr_accessor :content_uri
10
- # File path of the file to be analyzed (required if no content and content_uri)
13
+ # File path of the file to be analyzed (required if no content and
14
+ # content_uri)
11
15
  attr_accessor :file_path
12
16
  # genre to categorize the input data
13
17
  attr_accessor :genre
@@ -40,15 +44,19 @@ class DocumentParameters
40
44
  # Validates the parameters by checking if there are multiple content sources
41
45
  # set or no content provided at all.
42
46
  def validate_params
47
+ content_msg = 'The format of the request is invalid: multiple content ' \
48
+ 'sources; must be one of an attachment, an inline "content" field, or ' \
49
+ 'an external "contentUri"'
50
+ no_content_msg = 'The format of the request is invalid: no content ' \
51
+ 'provided; must be one of an attachment, an inline "content" field, or ' \
52
+ 'an external "contentUri"'
53
+ opt_msg = 'rosette_options can only be an instance of a Hash'
43
54
  if [@content, @content_uri, @file_path].compact.length > 1
44
- raise BadRequestFormatError.new 'The format of the request is invalid: multiple content sources;' \
45
- ' must be one of an attachment, an inline "content" field, or an external' \
46
- '"contentUri"'
55
+ raise BadRequestFormatError.new(content_msg)
47
56
  elsif [@content, @content_uri, @file_path].all?(&:nil?)
48
- raise BadRequestFormatError.new 'The format of the request is invalid: no content provided; must' \
49
- ' be one of an attachment, an inline "content" field, or an external "contentUri"'
57
+ raise BadRequestFormatError.new(no_content_msg)
50
58
  elsif @rosette_options
51
- raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash
59
+ raise BadRequestError.new(opt_msg) unless @rosette_options.is_a? Hash
52
60
  end
53
61
  end
54
62
 
@@ -57,9 +65,10 @@ class DocumentParameters
57
65
  # Returns the new Hash.
58
66
  def load_params
59
67
  validate_params
60
- to_hash.select { |_key, value| value }
61
- .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
62
- .to_h
68
+ to_hash
69
+ .select { |_key, value| value }
70
+ .map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
71
+ .to_h
63
72
  end
64
73
 
65
74
  # Converts this class to Hash.