Module: Indico

Defined in:
lib/indico.rb,
lib/indico/pdf.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.5'

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



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

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



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

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



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

def self.api_key
  config['auth']
end

.api_key=(api) ⇒ Object



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

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



175
176
177
# File 'lib/indico.rb', line 175

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

.content_filtering(image, config = nil) ⇒ Object



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

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

.emotion(text, config = nil) ⇒ Object



68
69
70
# File 'lib/indico.rb', line 68

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

.facial_features(image, config = nil) ⇒ Object



141
142
143
# File 'lib/indico.rb', line 141

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

.facial_localization(image, config = nil) ⇒ Object



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

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

.fer(image, config = nil) ⇒ Object



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

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



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

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



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

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



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

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



64
65
66
# File 'lib/indico.rb', line 64

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



114
115
116
117
118
119
# File 'lib/indico.rb', line 114

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

.pdf_extraction(pdf, config = {}) ⇒ Object



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

def self.pdf_extraction(pdf, config = {})
  api_handler(preprocess_pdf(pdf), "pdfextraction", config)
end

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



107
108
109
110
111
112
# File 'lib/indico.rb', line 107

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



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

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

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



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

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

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



121
122
123
124
125
126
# File 'lib/indico.rb', line 121

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



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

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



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

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

.preprocess_pdf(pdf) ⇒ Object



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

def self.preprocess_pdf(pdf)
    if pdf.class == Array
        # Batch Request
        pdf_array = Array.new

        # process each image
        pdf.each do |_pdf|
            pdf_array.push(preprocess_pdf(_pdf))
        end

        return pdf_array
    elsif pdf.class != String
        raise Exception.new("PDF input must be filename, url or base64 string")
    end

    begin
        return Base64.encode64(File.read(pdf))
    rescue
        # likely a url or a base64 encoded string already
        return pdf
    end
end

.private_cloudObject



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

def self.private_cloud
  config['cloud']
end

.private_cloud=(cloud) ⇒ Object



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

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

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



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

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



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

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

.sentiment_hq(text, config = nil) ⇒ Object



60
61
62
# File 'lib/indico.rb', line 60

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

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



128
129
130
# File 'lib/indico.rb', line 128

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

.text_features(text, config = nil) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/indico.rb', line 99

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



72
73
74
# File 'lib/indico.rb', line 72

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

.twitter_engagement(text, config = nil) ⇒ Object



56
57
58
# File 'lib/indico.rb', line 56

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