Class: Applied::Sentiment

Inherits:
Object
  • Object
show all
Defined in:
lib/applied/sentiment.rb

Constant Summary collapse

ENDPOINT =
"/api/sentiment_api/"
PERMITTED_OPTIONS =
[:return_original, :classifier]
CLASSIFIERS =
["default", "subjective"]
AVAILABLE_LANGUAGES =
["eng", "nld", "deu", "fra", "spa", "ita", "rus"]
DATA_MUST_BE_ARRAY =
"Data must be and Array"
TEXT_MISSING =
"Missing text parameter, it is mandatory"
ID_MISSING =
"Missing ID parameter, it is mandatory"
LANGUAGE_MISSING =
"Missing language parameter, it is mandatory"
BAD_RETURN =
"Bad value for return_original parameter. Allowed values are: true, false"
BAD_CLASSIFIER =
"Bad value for classifier parameter. Allowed values are: 'default', 'subjective'"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#classifierObject

Returns the value of attribute classifier.



21
22
23
# File 'lib/applied/sentiment.rb', line 21

def classifier
  @classifier
end

#return_originalObject

Returns the value of attribute return_original.



21
22
23
# File 'lib/applied/sentiment.rb', line 21

def return_original
  @return_original
end

Instance Method Details

#analyze(data, options = {}) ⇒ Object

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/applied/sentiment.rb', line 23

def analyze(data, options = {})

  # checks on data

  raise Applied::BadData.new(DATA_MUST_BE_ARRAY) unless data.instance_of? Array

  data.each do |d|
    raise Applied::BadData.new(TEXT_MISSING) if d[:text].nil? or d[:text].empty?
    raise Applied::BadData.new(ID_MISSING) unless [Fixnum, String].include? d[:id].class
    raise Applied::BadData.new(LANGUAGE_MISSING) if d[:language_iso].nil? or d[:language_iso].empty? or not AVAILABLE_LANGUAGES.include? d[:language_iso]
  end

  # checks on options

  unless options[:return_original].nil?
    raise Applied::BadOptions.new(BAD_RETURN) unless [true, false].include? options[:return_original]
  else
    options[:return_original] = false
  end 

  unless options[:classifier].nil?
    raise Applied::BadOptions.new(BAD_CLASSIFIER) unless CLASSIFIERS.include? options[:classifier]
  else
    options[:classifier] = "default"
  end

  # options cleanup

  options.delete_if {|o| not PERMITTED_OPTIONS.include? o}

  call(ENDPOINT, data, options)
end