Module: Indico

Defined in:
lib/indico.rb,
lib/indico/image.rb,
lib/indico/multi.rb,
lib/indico/helper.rb,
lib/indico/version.rb,
lib/indico/settings.rb

Defined Under Namespace

Classes: Collection

Constant Summary collapse

CLIENT_TO_SERVER =
{
  "political" => "political",
  "sentiment" => "sentiment",
  "sentiment_hq" => "sentimenthq",
  "language" => "language",
  "text_tags" => "texttags",
  "fer" => "fer",
  "keywords" => "keywords",
  "facial_features" => "facialfeatures",
  "facial_localization" => "facial_localization",
  "image_features" => "imagefeatures",
  "content_filtering" => "contentfiltering",
  "twitter_engagement" => "twitterengagement",
  "personality" => "personality",
  "personas" => "personas"
}
SERVER_TO_CLIENT =
CLIENT_TO_SERVER.invert
VERSION =
'0.10.3'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cloud_protocolObject

Returns the value of attribute cloud_protocol.



46
47
48
# File 'lib/indico/settings.rb', line 46

def cloud_protocol
  @cloud_protocol
end

.configObject

Returns the value of attribute config.



45
46
47
# File 'lib/indico/settings.rb', line 45

def config
  @config
end

Class Method Details

.analyze_image(face, apis = IMAGE_APIS, config = nil) ⇒ Object



159
160
161
162
# File 'lib/indico.rb', line 159

def self.analyze_image(face, apis = IMAGE_APIS, config = nil)
  api_hash = {'apis' => apis}
  multi(preprocess(face, 48, false), "image", apis, IMAGE_APIS, config ? config.update(api_hash) : api_hash)
end

.analyze_text(text, apis = TEXT_APIS, config = nil) ⇒ Object



164
165
166
167
# File 'lib/indico.rb', line 164

def self.analyze_text(text, apis = TEXT_APIS, config = nil)
  api_hash = {'apis' => apis}
  multi(text, "text", apis, TEXT_APIS, config ? config.update(api_hash) : api_hash)
end

.api_keyObject



12
13
14
# File 'lib/indico.rb', line 12

def self.api_key
  config['auth']
end

.api_key=(api) ⇒ Object



16
17
18
# File 'lib/indico.rb', line 16

def self.api_key=(api)
  config['auth'] = api
end

.array_contains_float(array, dimens) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/indico/image.rb', line 80

def self.array_contains_float(array, dimens)
    # Determines whether or not the array contains floats
    if not array.is_a?(Array)
        return array.class == Float
    end
    elem = array[0]
    (0..dimens.size - 2).each do |i|
        elem = elem[0]
    end

    return elem.class == Float
end

.collections(config = nil) ⇒ Object



169
170
171
# File 'lib/indico.rb', line 169

def self.collections(config = nil)
  Indico.api_handler(nil, 'custom', config, 'collections')
end

.content_filtering(image, config = nil) ⇒ Object



155
156
157
# File 'lib/indico.rb', line 155

def self.content_filtering(image, config = nil)
  api_handler(preprocess(image, 128, true), 'contentfiltering', config)
end

.emotion(text, config = nil) ⇒ Object



66
67
68
# File 'lib/indico.rb', line 66

def self.emotion(text, config = nil)
  api_handler(text, 'emotion', config)
end

.facial_features(image, config = nil) ⇒ Object



135
136
137
# File 'lib/indico.rb', line 135

def self.facial_features(image, config = nil)
  api_handler(preprocess(image, 48, false), 'facialfeatures', config)
end

.facial_localization(image, config = nil) ⇒ Object



139
140
141
# File 'lib/indico.rb', line 139

def self.facial_localization(image, config = nil)
  api_handler(preprocess(image, false, false), 'faciallocalization', config)
end

.fer(image, config = nil) ⇒ Object



130
131
132
133
# File 'lib/indico.rb', line 130

def self.fer(image, config = nil)
  size = (config != nil and config["detect"] == true) ? false : 48
  api_handler(preprocess(image, size, false), 'fer', config)
end

.get_dimension(array) ⇒ Object



75
76
77
78
# File 'lib/indico/image.rb', line 75

def self.get_dimension(array)
    return [] unless array.is_a?(Array)
    return [array.size] + get_dimension(array[0])
end

.get_rgb(value) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/indico/image.rb', line 93

def self.get_rgb(value)
    # Returns Integer encoding of RGB value used by ChunkyPNG
    return [
        ChunkyPNG::Color.r(value),
        ChunkyPNG::Color.g(value),
        ChunkyPNG::Color.b(value)
    ]
end

.handle_image_input(str, size, min_axis) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/indico/image.rb', line 39

def self.handle_image_input(str, size, min_axis)
    # Handles string input
    if File.file?(str)
        # Handling File Inputs
        begin
            image = ChunkyPNG::Image.from_file(str)
            if min_axis
                image = self.min_resize(image, size)
            else
                image = image.resize(size, size)
            end
            image = image.to_data_url.gsub("data:image/png;base64," ,"")
        rescue
            File.open(str, 'r') do |file|
                image = Base64.encode64(file.read)
            end
        end
    elsif str =~ /\A#{URI::regexp}\z/
        image = str
    else
        begin
            image = ChunkyPNG::Image.from_data_url("data:image/png;base64," + str.gsub("data:image/png;base64," ,""))
            if min_axis
                image = self.min_resize(image, size)
            else
                image = image.resize(size, size)
            end
            image = image.to_data_url.gsub("data:image/png;base64," ,"")
        rescue
            image = str
        end
    end

    return image
end

.handle_multi(results) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/indico/multi.rb', line 71

def self.handle_multi(results)
  converted_results = Hash.new
  results.each do |key, value|
    if value.is_a?(Hash) && value.has_key?("results")
      converted_results[key] = value["results"]
    else
      raise IndicoError, 'unexpected result from ' + key + '. ' + value.fetch("error", "")
    end
  end
  converted_results
end

.image_features(image, config = {}) ⇒ Object



143
144
145
146
147
148
# File 'lib/indico.rb', line 143

def self.image_features(image, config = {})
  unless config.key?('v') or config.key?('version')
    config['version'] = "3"
  end
  api_handler(preprocess(image, 512, true), 'imagefeatures', config)
end

.image_recognition(image, config = nil) ⇒ Object



150
151
152
# File 'lib/indico.rb', line 150

def self.image_recognition(image, config = nil)
  api_handler(preprocess(image, 144, true), 'imagerecognition', config)
end

.intersections(data, apis = nil, config = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/indico/multi.rb', line 44

def self.intersections(data, apis = nil, config = nil)

  if apis == nil
    fail "Argument 'apis' must be provided"
  end

  api_types = apis.map { |api| API_TYPES[api] }

  if !apis.is_a? Array or apis.length != 2
    fail "Argument 'apis' must be of length 2"
  elsif data.is_a? Array and data.length < 3
    fail "At least 3 examples are required to use the intersections api"
  elsif api_types[0] != api_types[1]
    fail "Both 'apis' must accept the same kind of input to use the intersections api."
  end

  if config.nil?
    config = {}
  end

  self.validate_apis(apis)
  config['apis'] = apis
  response = api_handler(data, "apis/intersections", config)
  return response

end

.keywords(text, config = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/indico.rb', line 74

def self.keywords(text, config = nil)
  if !config
    config = {}
  end
  if !config.key?(:v) and !config.key?(:version)
    config['version'] = "2"
  end
  if config.key?(:language) and config[:language] != "english"
    config['version'] = "1"
  end
  api_handler(text, 'keywords', config)

end

.language(text, config = nil) ⇒ Object



62
63
64
# File 'lib/indico.rb', line 62

def self.language(text, config = nil)
  api_handler(text, 'language', config)
end

.min_resize(decoded_image, size) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/indico/image.rb', line 25

def self.min_resize(decoded_image, size)
    img_size = [decoded_image.width, decoded_image.height]
    min_idx, max_idx = img_size[0] < img_size[1] ? [0, 1] : [1, 0]
    aspect = img_size[max_idx] / Float(img_size[min_idx])
    if aspect > 10
        warn("An aspect ratio greater than 10:1 is not recommended")
    end
    size_arr = [0, 0]
    size_arr[min_idx] = size
    size_arr[max_idx] = Integer(size * aspect)
    image = decoded_image.resize(size_arr[0], size_arr[1])
    return image
end

.multi(data, type, apis, allowed, batch = false, config) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/indico/multi.rb', line 32

def self.multi(data, type, apis, allowed, batch = false, config)

  if config.nil?
    config = {}
  end

  self.validate_apis(apis, type, allowed)
  config['apis'] = apis
  response = api_handler(data, batch ? "apis/batch" : "apis", config)
  return handle_multi(response)
end

.organizations(text, config = {}) ⇒ Object



112
113
114
115
116
117
# File 'lib/indico.rb', line 112

def self.organizations(text, config = {})
  if not (config.key?('v') or config.key?('version'))
    config['version'] = "2"
  end
  api_handler(text, "organizations", config)
end

.people(text, config = {}) ⇒ Object



105
106
107
108
109
110
# File 'lib/indico.rb', line 105

def self.people(text, config = {})
  if not (config.key?('v') or config.key?('version'))
    config['version'] = "2"
  end
  api_handler(text, "people", config)
end

.personality(text, config = nil) ⇒ Object



46
47
48
# File 'lib/indico.rb', line 46

def self.personality(text, config = nil)
  api_handler(text, 'personality', config)
end

.personas(text, config = {'persona'=> true}) ⇒ Object



50
51
52
# File 'lib/indico.rb', line 50

def self.personas(text, config = {'persona'=> true})
  api_handler(text, 'personality', config)
end

.places(text, config = {}) ⇒ Object



119
120
121
122
123
124
# File 'lib/indico.rb', line 119

def self.places(text, config = {})
  if not (config.key?('v') or config.key?('version'))
    config['version'] = "2"
  end
  api_handler(text, "places", config)
end

.political(text, config = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/indico.rb', line 28

def self.political(text, config = nil)
  if !config
    config = {}
  end
  if !config.key?(:v) and !config.key?(:version)
    config['version'] = "2"
  end
  api_handler(text, 'political', config)
end

.posneg(*args) ⇒ Object



38
39
40
# File 'lib/indico.rb', line 38

def self.posneg(*args)
  sentiment(*args)
end

.preprocess(image, size, min_axis) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/indico/image.rb', line 6

def self.preprocess(image, size, min_axis)
    if image.class == Array
        # Batch Request
        im_array = Array.new

        # process each image
        image.each do |_image|
            im_array.push(preprocess(_image, size, min_axis))
        end

        return im_array
    elsif image.class != String
        raise Exception.new("Image input must be filename or base64 string")
    end

    # Resize and export base64 encoded string
    return handle_image_input(image, size, min_axis)
end

.private_cloudObject



20
21
22
# File 'lib/indico.rb', line 20

def self.private_cloud
  config['cloud']
end

.private_cloud=(cloud) ⇒ Object



24
25
26
# File 'lib/indico.rb', line 24

def self.private_cloud=(cloud)
  config['cloud'] = cloud
end

.relevance(text, queries, config = nil) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/indico.rb', line 88

def self.relevance(text, queries, config = nil)
  if config.nil?
    config = Hash.new()
  end
  config[:queries] = queries
  config[:synonyms] = false
  return api_handler(text, 'relevance', config)
end

.sentiment(text, config = nil) ⇒ Object



42
43
44
# File 'lib/indico.rb', line 42

def self.sentiment(text, config = nil)
  api_handler(text, 'sentiment', config)
end

.sentiment_hq(text, config = nil) ⇒ Object



58
59
60
# File 'lib/indico.rb', line 58

def self.sentiment_hq(text, config = nil)
  api_handler(text, 'sentimenthq', config)
end

.summarization(text, config = {}) ⇒ Object



126
127
128
# File 'lib/indico.rb', line 126

def self.summarization(text, config = {})
  api_handler(text, "summarization", config)
end

.text_features(text, config = nil) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/indico.rb', line 97

def self.text_features(text, config = nil)
  if config.nil?
    config = Hash.new()
  end
  config[:synonyms] = false
  return api_handler(text, 'textfeatures', config)
end

.text_tags(text, config = nil) ⇒ Object



70
71
72
# File 'lib/indico.rb', line 70

def self.text_tags(text, config = nil)
  api_handler(text, 'texttags', config)
end

.twitter_engagement(text, config = nil) ⇒ Object



54
55
56
# File 'lib/indico.rb', line 54

def self.twitter_engagement(text, config = nil)
  api_handler(text, 'twitterengagement', config)
end

.validate_apis(apis, type = "api", allowed = CLIENT_TO_SERVER.keys) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/indico/multi.rb', line 24

def self.validate_apis(apis, type="api", allowed=CLIENT_TO_SERVER.keys)
  apis.each { |api|
    if not allowed.include? api
      fail  api + " is not a valid api for " + type + " requests. Please use: " + allowed.join(", ")
    end
  }
end