Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Park committed Jun 14, 2017
2 parents 823594f + eba2382 commit 4672096
Show file tree
Hide file tree
Showing 21 changed files with 522 additions and 291 deletions.
10 changes: 5 additions & 5 deletions examples/categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
rosette_api = RosetteAPI.new(api_key, url)
end

categories_url_data = "http://www.onlocationvacations.com/2015/03/05/the-new-ghostbusters-movie-begins-filming-in-boston-in-june/"
categories_url_data = 'http://www.onlocationvacations.com/2015/03/05/the-new-ghostbusters-movie-begins-filming-in-boston-in-june/'
begin
params = DocumentParameters.new(content_uri: categories_url_data)
response = rosette_api.get_categories(params)
puts JSON.pretty_generate(response)
params = DocumentParameters.new(content_uri: categories_url_data)
response = rosette_api.get_categories(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message)
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
21 changes: 21 additions & 0 deletions examples/name_deduplication.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'rosette_api'

api_key, url = ARGV

if !url
rosette_api = RosetteAPI.new(api_key)
else
rosette_api = RosetteAPI.new(api_key, url)
end

name_dedupe_data = 'John Smith,Johnathon Smith,Fred Jones'

threshold = 0.75
names = name_dedupe_data.split(',').map { |n| NameParameter.new(n) }
begin
params = NameDeduplicationParameters.new(names, threshold)
response = rosette_api.get_name_deduplication(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
14 changes: 7 additions & 7 deletions examples/name_similarity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
rosette_api = RosetteAPI.new(api_key, url)
end

matched_name_data1 = "Michael Jackson"
matched_name_data2 = "迈克尔·杰克逊"
matched_name_data1 = 'Michael Jackson'
matched_name_data2 = '迈克尔·杰克逊'
begin
name1 = NameParameter.new(matched_name_data1, entity_type: 'PERSON', language:'eng')
params = NameSimilarityParameters.new(name1, matched_name_data2)
response = rosette_api.name_similarity(params)
puts JSON.pretty_generate(response)
name1 = NameParameter.new(matched_name_data1, entity_type: 'PERSON', language: 'eng')
params = NameSimilarityParameters.new(name1, matched_name_data2)
response = rosette_api.get_name_similarity(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message)
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
10 changes: 5 additions & 5 deletions examples/name_translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
rosette_api = RosetteAPI.new(api_key, url)
end

translated_name_data = "معمر محمد أبو منيار القذاف"
translated_name_data = 'معمر محمد أبو منيار القذاف'
begin
params = NameTranslationParameters.new(translated_name_data, 'eng', target_script: 'Latn')
response = rosette_api.name_translation(params)
puts JSON.pretty_generate(response)
params = NameTranslationParameters.new(translated_name_data, 'eng', target_script: 'Latn')
response = rosette_api.get_name_translation(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message)
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
6 changes: 3 additions & 3 deletions examples/ping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
rosette_api = RosetteAPI.new(api_key, url)
end
begin
response = rosette_api.ping
puts JSON.pretty_generate(response)
response = rosette_api.ping
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message)
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
10 changes: 5 additions & 5 deletions examples/relationships.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

relationships_text_data = "Bill Gates, Microsoft's former CEO, is a philanthropist."
begin
params = DocumentParameters.new(content: relationships_text_data)
params.rosette_options = { accuracyMode: 'PRECISION' }
response = rosette_api.get_relationships(params)
puts JSON.pretty_generate(response)
params = DocumentParameters.new(content: relationships_text_data)
params.rosette_options = { accuracyMode: 'PRECISION' }
response = rosette_api.get_relationships(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message)
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
10 changes: 5 additions & 5 deletions examples/sentences.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
sentences_data = "This land is your land. This land is my land\nFrom California to the New York island;\nFrom the red wood forest to the Gulf Stream waters\n\nThis land was made for you and Me.\n\nAs I was walking that ribbon of highway,\nI saw above me that endless skyway:\nI saw below me that golden valley:\nThis land was made for you and me."

begin
params = DocumentParameters.new
params.content = sentences_data
response = rosette_api.get_sentences(params)
puts JSON.pretty_generate(response)
params = DocumentParameters.new
params.content = sentences_data
response = rosette_api.get_sentences(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message)
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
10 changes: 5 additions & 5 deletions examples/sentiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
rosette_api = RosetteAPI.new(api_key, url)
end

file = Tempfile.new(%w(foo .html))
file = Tempfile.new(%w[foo .html])
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>'
file.write(sentiment_file_data)
file.close
begin
params = DocumentParameters.new(file_path: file.path, language: 'eng')
response = rosette_api.get_sentiment(params)
puts JSON.pretty_generate(response)
params = DocumentParameters.new(file_path: file.path, language: 'eng')
response = rosette_api.get_sentiment(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message)
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
10 changes: 5 additions & 5 deletions examples/syntax_dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
rosette_api = RosetteAPI.new(api_key, url)
end

syntax_dependencies_data = "Yoshinori Ohsumi, a Japanese cell biologist, was awarded the Nobel Prize in Physiology or Medicine on Monday."
syntax_dependencies_data = 'Yoshinori Ohsumi, a Japanese cell biologist, was awarded the Nobel Prize in Physiology or Medicine on Monday.'
begin
params = DocumentParameters.new(content: syntax_dependencies_data, genre: 'social-media')
response = rosette_api.get_syntax_dependencies(params)
puts JSON.pretty_generate(response)
params = DocumentParameters.new(content: syntax_dependencies_data)
response = rosette_api.get_syntax_dependencies(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message)
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
8 changes: 4 additions & 4 deletions examples/text_embedding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

embeddings_data = 'Cambridge, Massachusetts'
begin
params = DocumentParameters.new(content: embeddings_data)
response = rosette_api.get_text_embedding(params)
puts JSON.pretty_generate(response)
params = DocumentParameters.new(content: embeddings_data)
response = rosette_api.get_text_embedding(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message)
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
10 changes: 5 additions & 5 deletions examples/tokens.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
rosette_api = RosetteAPI.new(api_key, url)
end

tokens_data = "北京大学生物系主任办公室内部会议"
tokens_data = '北京大学生物系主任办公室内部会议'
begin
params = DocumentParameters.new(content: tokens_data)
response = rosette_api.get_tokens(params)
puts JSON.pretty_generate(response)
params = DocumentParameters.new(content: tokens_data)
response = rosette_api.get_tokens(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf("Rosette API Error (%s): %s", rosette_api_error.status_code, rosette_api_error.message)
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
20 changes: 20 additions & 0 deletions examples/transliteration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rosette_api'

api_key, url = ARGV

if !url
rosette_api = RosetteAPI.new(api_key)
else
rosette_api = RosetteAPI.new(api_key, url)
end

transliteration_content_data = 'Kareem Abdul Jabbar holds the records for most points in the NBA'

begin
params = DocumentParameters.new
params.content = transliteration_content_data
response = rosette_api.get_transliteration(params)
puts JSON.pretty_generate(response)
rescue RosetteAPIError => rosette_api_error
printf('Rosette API Error (%s): %s', rosette_api_error.status_code, rosette_api_error.message)
end
4 changes: 2 additions & 2 deletions lib/document_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def validate_params
elsif [@content, @content_uri, @file_path].all?(&:nil?)
raise BadRequestFormatError.new 'The format of the request is invalid: no content provided; must' \
' be one of an attachment, an inline "content" field, or an external "contentUri"'
elsif !@rosette_options.nil?
elsif @rosette_options
raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash
end
end
Expand All @@ -57,7 +57,7 @@ def validate_params
# Returns the new Hash.
def load_params
validate_params
to_hash.select { |_key, value| !value.nil? }
to_hash.select { |_key, value| value }
.map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
.to_h
end
Expand Down
56 changes: 56 additions & 0 deletions lib/name_deduplication_parameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require_relative 'bad_request_error'
require_relative 'name_parameter'

# This class encapsulates parameters that are needed for name-deduplication in
# Rosette API.
class NameDeduplicationParameters
# Rosette API options (optional, should be a hash)
attr_accessor :rosette_options
# List of Name objects to be de-duplicated
attr_accessor :names
# Threshold for determining cluster size
attr_accessor :threshold

def initialize(names, threshold, options = {}) #:notnew:
options = {
rosette_options: nil
}.update options
@names = names
@threshold = threshold
@rosette_options = options[:rosette_options]
end

# Validates the parameters by checking if name1 and name2 are instances of
# a String or NameParameter.
def validate_params
raise BadRequestError.new('names must be an array of name_parameter') unless @names.instance_of? Array
if @threshold
raise BadRequestError.new('threshold must be a float') unless @threshold.is_a?(Float)
raise BadRequestError.new('threshold must be in the range of 0 to 1') if @threshold.negative? || @threshold > 1
end
if @rosette_options
raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash
end
end

# Converts this class to Hash with its keys in lower CamelCase.
#
# Returns the new Hash.
def load_params
validate_params
to_hash.select { |_key, value| value }
.map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
.to_h
end

# Converts this class to Hash.
#
# Returns the new Hash.
def to_hash
{
names: @names.map(&:load_param),
threshold: @threshold,
options: @rosette_options
}
end
end
6 changes: 3 additions & 3 deletions lib/name_parameter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def initialize(text, options = {}) #:notnew:
#
# Returns the new Hash.
def load_param
self.to_hash.select { |_key, value| !value.nil? }
.map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
.to_h
to_hash.select { |_key, value| value }
.map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
.to_h
end

# Converts this class to Hash.
Expand Down
16 changes: 7 additions & 9 deletions lib/name_similarity_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ def initialize(name1, name2, options = {}) #:notnew:
# Validates the parameters by checking if name1 and name2 are instances of
# a String or NameParameter.
def validate_params
if [String, NameParameter].none? { |clazz| @name1.is_a? clazz }
raise BadRequestError.new('name1 option can only be an instance of a String or NameParameter')
elsif [String, NameParameter].none? { |clazz| @name2.is_a? clazz }
raise BadRequestError.new('name2 option can only be an instance of a String or NameParameter')
elsif !@rosette_options.nil?
raise BadRequestError.new('name1 option can only be an instance of a String or NameParameter') if [String, NameParameter].none? { |clazz| @name1.is_a? clazz }
raise BadRequestError.new('name2 option can only be an instance of a String or NameParameter') if [String, NameParameter].none? { |clazz| @name2.is_a? clazz }
if @rosette_options
raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash
end
end
Expand All @@ -40,10 +38,10 @@ def validate_params
#
# Returns the new Hash.
def load_params
self.validate_params
self.to_hash.select { |_key, value| !value.nil? }
.map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
.to_h
validate_params
to_hash.reject { |_key, value| value.nil? }
.map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
.to_h
end

# Converts this class to Hash.
Expand Down
10 changes: 5 additions & 5 deletions lib/name_translation_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def initialize(name, target_language, options = {}) #:notnew:

# Validates the parameters by checking if rosette_options is an instance of a Hash.
def validate_params
if !@rosette_options.nil?
if @rosette_options
raise BadRequestError.new('rosette_options can only be an instance of a Hash') unless @rosette_options.is_a? Hash
end
end
Expand All @@ -58,10 +58,10 @@ def validate_params
#
# Returns the new Hash.
def load_params
self.validate_params
self.to_hash.select { |_key, value| !value.nil? }
.map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
.to_h
validate_params
to_hash.select { |_key, value| value }
.map { |key, value| [key.to_s.split('_').map(&:capitalize).join.sub!(/\D/, &:downcase), value] }
.to_h
end

# Converts this class to Hash.
Expand Down
Loading

0 comments on commit 4672096

Please sign in to comment.