Skip to content

Commit

Permalink
Refactor of Entity Extraction class
Browse files Browse the repository at this point in the history
  • Loading branch information
zenkay committed Jul 14, 2015
1 parent 0f0b8fe commit 704b41c
Show file tree
Hide file tree
Showing 14 changed files with 1,066 additions and 58 deletions.
17 changes: 15 additions & 2 deletions lib/dandelionapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.config
# Container for configuration parameters
#
class Configuration
attr_accessor :app_id, :app_key, :endpoint
attr_accessor :app_id, :app_key, :endpoint, :path
end

# Exception raised for connection error
Expand All @@ -42,5 +42,18 @@ class BadResponse < Exception; end

# Exception raised when a mandatory parameter is missing
#
class MissingParameters < Exception; end
class MissingParameter < Exception; end

# Exception raised when more than one require parameter is given
#
class TooManyParameters < Exception; end

# Exception raised when a parameter is in the wrong type
#
class WrongParameterType < Exception; end

# Exception raised when a parameter is in the wrong format
#
class WrongParameterFormat < Exception; end

end
79 changes: 63 additions & 16 deletions lib/dandelionapi/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,75 @@
require "json"

module Dandelionapi
module Base

class Base
class Request

protected
def call(endpoint, params)
begin
params = params.merge(
:$app_id => Dandelionapi.config.app_id,
:$app_key => Dandelionapi.config.app_key
)
conn = Faraday.new(url: Dandelionapi.config.endpoint) do |faraday|
faraday.request :url_encoded
faraday.adapter Faraday.default_adapter
end
response = conn.post "#{Dandelionapi.config.path}#{endpoint}", params
JSON.parse response.body
rescue Exception => e
raise Dandelionapi::BadResponse
end
end

def filter_permitted_params options
params = {}
options.each { |key, value| params[key.to_s] = value }
params.select { |key,value| (self.class::MANDATORY_FIELDS + self.class::OPTIONAL_FIELDS).include? key }
end

def required_params_missing? params
mandatory_fields_count(params) == 0
end

def too_many_mandatory_parameters? params
mandatory_fields_count(params) > 1
end

def mandatory_fields_count params
(self.class::MANDATORY_FIELDS & params.keys).size
end

def call(endpoint, params)
begin
params = params.merge(
:$app_id => Dandelionapi.config.app_id,
:$app_key => Dandelionapi.config.app_key
)
conn = Faraday.new(url: Dandelionapi.config.endpoint) do |faraday|
faraday.request :url_encoded
faraday.adapter Faraday.default_adapter
def verify_type params, &block
params.each do |key, value|
formats = [self.class::FIELDS_FORMAT[key][:type]] unless self.class::FIELDS_FORMAT[key][:type].is_a? Array
unless formats.include? value.class
yield key
end
end
response = conn.post endpoint, params
JSON.parse response.body
rescue Exception => e
raise Dandelionapi::BadResponse
end

def verify_format params, &block
params.each do |key, value|
if self.class::FIELDS_FORMAT[key] and self.class::FIELDS_FORMAT[key][:valid]
yield key unless self.class::FIELDS_FORMAT[key][:valid].call value
end
end
end

end

end
class Response

attr_accessor :timestamp, :time, :lang, :annotations

def initialize(object)
timestamp = object[:timestamp]
time = object[:time]
lang = object[:lang]
annotations = object[:annotations]
end

end
end
end
87 changes: 75 additions & 12 deletions lib/dandelionapi/entity_extraction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,87 @@
require "json"

module Dandelionapi
module EntityExtraction
class Request < Base::Request

class EntityExtraction < Base
ENDPOINT = "/nex/v1"

ENDPOINT = "/datatxt/nex/v1"
MANDATORY_FIELDS = [
"text",
"url",
"html",
"html_fragment"
]

attr_accessor :text, :url, :html, :html_fragment,
:lang, :min_confidence, :min_length, :social_hashtag,
:social_mention, :include, :extra_types, :country,
:custom_spots
OPTIONAL_FIELDS = [
"lang",
"min_confidence",
"min_length",
"social.hashtag",
"social.mention",
"include",
"extra_types",
"country",
"custom_spots"
]

def analyze(options)
FIELDS_FORMAT = {
"text" => {type: String},
"url" => {type: String},
"html" => {type: String},
"html_fragment" => {type: String},
"lang" => {
type: String,
valid: lambda {|value| ["de", "en", "fr", "it", "pt", "auto"].include? value}
},
"min_confidence" => {
type: Float,
valid: lambda {|value| [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0].include? value}
},
"min_length" => {
type: Integer,
valid: lambda {|value| value >= 2}
},
"social.hashtag" => {type: [TrueClass, FalseClass]},
"social.mention" => {type: [TrueClass, FalseClass]},
"include" => {
type: Array,
valid: lambda {|values| values.all?{|value|["types", "categories", "abstract", "image", "lod", "alternate_labels"].include? value}}
},
"extra_types" => {
type: Array,
valid: lambda {|values| values.all?{|value|["phone", "vat"].include? value}}
},
"country" => {
type: String,
valid: lambda {|value| ["AD", "AE", "AM", "AO", "AQ", "AR", "AU", "BB", "BR", "BS", "BY", "CA", "CH", "CL", "CN", "CX", "DE", "FR", "GB", "HU", "IT", "JP", "KR", "MX", "NZ", "PG", "PL", "RE", "SE", "SG", "US", "YT", "ZW"].include? value}
},
"custom_spots" => {type: String}
}

raise MissingParameters.new("Please provide one of the following parameters: text, url, html or html_fragment") if ([:text, :url, :html, :html_fragment] & options.keys).empty?
def analyze(options)

params = options
call(ENDPOINT, params)
end
params = filter_permitted_params options

end
if required_params_missing? params
raise MissingParameter.new "Please provide one of the following parameters: #{MANDATORY_FIELDS.join(", ")} as input"
end

if too_many_mandatory_parameters? params
raise TooManyParameters.new "Please provide only one of the following parameters: #{MANDATORY_FIELDS.join(", ")} as input"
end

verify_type params do |wrong_param|
raise WrongParameterType.new "Type of #{wrong_param} should be #{FIELDS_FORMAT[wrong_param][:type]}"
end

verify_format params do |wrong_param|
raise WrongParameterFormat.new "Wrong format for #{wrong_param}"
end

call(ENDPOINT, params)
end

end
end
end
8 changes: 5 additions & 3 deletions lib/dandelionapi/language_detection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

module Dandelionapi

class LanguageDetection < Base
class LanguageDetection

ENDPOINT = "/datatxt/li/v1"
include Base

ENDPOINT = "/li/v1"

attr_accessor :text, :url, :html, :html_fragment, :clean

def analyze(options)

raise MissingParameters.new("Please provide one of the following parameters: text, url, html or html_fragment") if ([:text, :url, :html, :html_fragment] & options.keys).empty?
raise MissingParameter.new("Please provide one of the following parameters: text, url, html or html_fragment") if ([:text, :url, :html, :html_fragment] & options.keys).empty?

params = options
call(ENDPOINT, params)
Expand Down
8 changes: 5 additions & 3 deletions lib/dandelionapi/sentiment_analysis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

module Dandelionapi

class SentimentAnalysis < Base
class SentimentAnalysis

ENDPOINT = "/datatxt/sent/v1"
include Base

ENDPOINT = "/sent/v1"

attr_accessor :text, :url, :html, :html_fragment, :lang

def analyze(options)

raise MissingParameters.new("Please provide one of the following parameters: text, url, html or html_fragment") if ([:text, :url, :html, :html_fragment] & options.keys).empty?
raise MissingParameter.new("Please provide one of the following parameters: text, url, html or html_fragment") if ([:text, :url, :html, :html_fragment] & options.keys).empty?

params = options
call(ENDPOINT, params)
Expand Down
6 changes: 4 additions & 2 deletions lib/dandelionapi/text_similarity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

module Dandelionapi

class TextSimilarity < Base
class TextSimilarity

ENDPOINT = "/datatxt/sim/v1"
include Base

ENDPOINT = "/sim/v1"

attr_accessor :text1, :url1, :html1, :html_fragment1, :text2, :url2, :html2, :html_fragment2, :lang, :bow

Expand Down
Loading

0 comments on commit 704b41c

Please sign in to comment.