Class: AylienTextApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/aylien_text_api/client.rb

Overview

The Client class is the main class for calling Text API endpoints

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Creates a Client object.

Parameters:

  • options (Hash) (defaults to: {})

    Configuration params

Options Hash (options):

  • :app_id (String)

    The APP_ID

  • :app_key (String)

    The APP_KEY

  • :base_uri (String) — default: 'https://api.aylien.com/api/v1/'

    An URL that points to the Aylien Text API

  • :method (Symbol) — default: :post

    Request method.

  • :user_agent (String)

    Request user-agent



29
30
31
32
33
34
35
# File 'lib/aylien_text_api/client.rb', line 29

def initialize(options={})
  merged_options = AylienTextApi.options.merge(options)

  Configuration::VALID_CONFIG_KEYS.each do |key|
    send("#{key}=", merged_options[key])
  end
end

Instance Method Details

#aspect_based_sentiment(value = nil, params = {}) ⇒ Hash?

Detects aspects and sentiment of a body of text. Given a review for a product or service, analyzes the sentiment of the review towards each of the aspects of the product or review that are mentioned in it.

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The ABSA endpoint options

Options Hash (params):

  • :url (String)

    The URL

  • :text (String)

    Text

  • :domain (String)

    Domain which document belongs to

Returns:



426
427
428
429
430
431
# File 'lib/aylien_text_api/client.rb', line 426

def aspect_based_sentiment(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:absa])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#aspect_based_sentiment!(value = nil, params = {}) ⇒ Object

Same as aspect_based_sentiment, but calls request! so an exception is raised if the request fails.



436
437
438
439
440
441
# File 'lib/aylien_text_api/client.rb', line 436

def aspect_based_sentiment!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:absa])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#classify(value = nil, params = {}) ⇒ Hash?

Classifies a piece of text according to IPTC NewsCode standard.

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The classify endpoint options

Options Hash (params):

  • :url (String)

    The URL

  • :text (String)

    Text

  • :language (String) — default: 'en'

    Language of text. Valid options are en, de, fr, es, it, pt, and auto. If set to auto, it’ll try to detect the language.

Returns:



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

def classify(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:classify])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#classify!(value = nil, params = {}) ⇒ Object

Same as classify, but calls request! so an exception is raised if the request fails.



122
123
124
125
126
127
# File 'lib/aylien_text_api/client.rb', line 122

def classify!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:classify])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#classify_by_taxonomy(value = nil, params = {}) ⇒ Hash?

Classifies a piece of text according to the specified taxonomy.

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The classify endpoint options

Options Hash (params):

  • :url (String)

    The URL

  • :text (String)

    Text

  • :language (String) — default: 'en'

    Language of text. Valid options are en, de, fr, es, it, pt, and auto. If set to auto, it’ll try to detect the language.

Returns:



81
82
83
84
85
86
# File 'lib/aylien_text_api/client.rb', line 81

def classify_by_taxonomy(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:classify_by_taxonomy])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#classify_by_taxonomy!(value = nil, params = {}) ⇒ Object

Same as classify_by_taxonomy, but calls request! so an exception is raised if the request fails.



91
92
93
94
95
96
# File 'lib/aylien_text_api/client.rb', line 91

def classify_by_taxonomy!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:classify_by_taxonomy])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#combined(value = nil, params = {}) ⇒ Hash?

Runs multiple analysis operations in one API call.

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The Combined endpoint options

Options Hash (params):

  • :url (String)

    The URL

  • :text (String)

    Text

  • :endpoint (Array<String>)

    Array of endpoints

Returns:



368
369
370
371
372
373
# File 'lib/aylien_text_api/client.rb', line 368

def combined(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:combined])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#combined!(value = nil, params = {}) ⇒ Object

Same as combined, but calls request! so an exception is raised if the request fails.



378
379
380
381
382
383
# File 'lib/aylien_text_api/client.rb', line 378

def combined!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:combined])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#concepts(value = nil, params = {}) ⇒ Hash?

Extracts named entities mentioned in a document, disambiguates and cross-links them to DBPedia and Linked Data entities, along with their semantic types (including DBPedia and schema.org).

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The concepts endpoint options

Options Hash (params):

  • :url (String)

    The URL

  • :text (String)

    Text

  • :language (String) — default: 'en'

    Language of text. Valid options are en, de, fr, es, it, pt, and auto. If set to auto, it’ll try to detect the language.

Returns:



145
146
147
148
149
150
# File 'lib/aylien_text_api/client.rb', line 145

def concepts(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:concepts])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#concepts!(value = nil, params = {}) ⇒ Object

Same as concepts, but calls request! so an exception is raised if the request fails.



155
156
157
158
159
160
# File 'lib/aylien_text_api/client.rb', line 155

def concepts!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:concepts])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#elsa(value = nil, params = {}) ⇒ Hash?

Extracts named entities (people, organizations and locations) and applies a sentiment analysis to them

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The entities endpoint options

Options Hash (params):

  • :url (String)

    The URL

  • :text (String)

    Text

Returns:



238
239
240
241
242
243
# File 'lib/aylien_text_api/client.rb', line 238

def elsa(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:elsa])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#elsa!(value = nil, params = {}) ⇒ Object

Same as elsa, but calls request! so an exception is raised if the request fails.



248
249
250
251
252
253
# File 'lib/aylien_text_api/client.rb', line 248

def elsa!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:elsa])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#entities(value = nil, params = {}) ⇒ Hash?

Extracts named entities (people, organizations and locations) and values (URLs, emails, telephone numbers, currency amounts and percentages) mentioned in a body of text.

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The entities endpoint options

Options Hash (params):

  • :url (String)

    The URL

  • :text (String)

    Text

  • :language (String) — default: 'en'

    Language of text. Valid options are en, de, fr, es, it, pt, and auto. If set to auto, it’ll try to detect the language.

Returns:



209
210
211
212
213
214
# File 'lib/aylien_text_api/client.rb', line 209

def entities(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:entities])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#entities!(value = nil, params = {}) ⇒ Object

Same as entities, but calls request! so an exception is raised if the request fails.



219
220
221
222
223
224
# File 'lib/aylien_text_api/client.rb', line 219

def entities!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:entities])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#extract(value = nil, params = {}) ⇒ Hash?

Extracts the main body of article, including embedded media such as images & videos from a URL and removes all the surrounding clutter.

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The extract endpoint options

Options Hash (params):

  • :url (String)

    The URL

  • :best_image (Boolean) — default: false

    Whether extract the best image of the article

Returns:



50
51
52
53
54
55
# File 'lib/aylien_text_api/client.rb', line 50

def extract(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:extract])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#extract!(value = nil, params = {}) ⇒ Object

Same as extract, but calls request! so an exception is raised if the request fails.



60
61
62
63
64
65
# File 'lib/aylien_text_api/client.rb', line 60

def extract!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:extract])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#hashtags(value = nil, params = {}) ⇒ Hash?

Suggests hashtags describing the document.

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The hashtags endpoint options

Options Hash (params):

  • :url (String)

    The URL

  • :text (String)

    Text

  • :language (String) — default: 'en'

    Language of text. Valid options are en, de, fr, es, it, pt, and auto. If set to auto, it’ll try to detect the language.

Returns:



176
177
178
179
180
181
# File 'lib/aylien_text_api/client.rb', line 176

def hashtags(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:hashtags])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#hashtags!(value = nil, params = {}) ⇒ Object

Same as hashtags, but calls request! so an exception is raised if the request fails.



186
187
188
189
190
191
# File 'lib/aylien_text_api/client.rb', line 186

def hashtags!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:hashtags])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#image_tags(value = nil, params = {}) ⇒ Hash?

Assigns relevant tags to an image.

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL

  • params (Hash) (defaults to: {})

    a customizable set of options

Options Hash (params):

  • :url (String)

    The URL

Returns:



394
395
396
397
398
399
# File 'lib/aylien_text_api/client.rb', line 394

def image_tags(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:image_tags])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#image_tags!(value = nil, params = {}) ⇒ Object

Same as image_tags, but calls request! so an exception is raised if the request fails.



404
405
406
407
408
409
# File 'lib/aylien_text_api/client.rb', line 404

def image_tags!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:image_tags])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#language(value = nil, params = {}) ⇒ Hash?

Detects the main language a document is written in and returns it in ISO 639-1 format.

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The language endpoint options

Options Hash (params):

  • :url (String)

    The URL

  • :text (String)

    Text

Returns:



267
268
269
270
271
272
# File 'lib/aylien_text_api/client.rb', line 267

def language(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:language])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#language!(value = nil, params = {}) ⇒ Object

Same as language, but calls request! so an exception is raised if the request fails.



277
278
279
280
281
282
# File 'lib/aylien_text_api/client.rb', line 277

def language!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:language])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#rate_limitsHash?

Returns current client’s rate limit values. If it gets failed, nil will be returned.

Returns:



449
450
451
452
453
# File 'lib/aylien_text_api/client.rb', line 449

def rate_limits
  self.language text: "Hello world!" unless defined?(@connection)
  
  (@connection || {}).rate_limits
end

#sentiment(value = nil, params = {}) ⇒ Hash?

Detects sentiment of a document in terms of polarity (“positive” or “negative”) and subjectivity (“subjective” or “objective”).

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The sentiment endpoint options

Options Hash (params):

  • :url (String)

    The URL

  • :text (String)

    Text

  • :mode (String) — default: 'tweet'

    Analyze mode. Valid options are tweet, and document.

Returns:



299
300
301
302
303
304
# File 'lib/aylien_text_api/client.rb', line 299

def sentiment(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:sentiment])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#sentiment!(value = nil, params = {}) ⇒ Object

Same as sentiment, but calls request! so an exception is raised if the request fails.



309
310
311
312
313
314
# File 'lib/aylien_text_api/client.rb', line 309

def sentiment!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:sentiment])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end

#summarize(value = nil, params = {}) ⇒ Hash?

Summarizes an article into a few key sentences.

Parameters:

  • value (String) (defaults to: nil)

    (nil) URL or Text

  • params (Hash) (defaults to: {})

    The summarize endpoint options

Options Hash (params):

  • :mode (String) — default: 'default'

    Analyze mode. Valid options are default and short. short mode produces shorter sentences.

  • :url (String)

    The URL

  • :title (String)

    Title

  • :text (String)

    Text

  • :sentences_number (Integer) — default: 5

    Number of sentences to be returned. Only in default mode (not applicable to short mode). Also has precedence over sentences_percentage.

  • :sentences_percentage (Integer)

    Percentage of sentences to be returned. Only in default mode (not applicable to short mode). Possible range is 1-100.

  • :language (String) — default: 'en'

    Language of text. Valid options are en, de, fr, es, it, pt, and auto. If set to auto, it’ll try to detect the language.

Returns:



339
340
341
342
343
344
# File 'lib/aylien_text_api/client.rb', line 339

def summarize(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:summarize])
  @connection = Connection.new(endpoint, params, config)
  @connection.request
end

#summarize!(value = nil, params = {}) ⇒ Object

Same as summarize, but calls request! so an exception is raised if the request fails.



349
350
351
352
353
354
# File 'lib/aylien_text_api/client.rb', line 349

def summarize!(value=nil, params={})
  endpoint, params, config = common_endpoint(value, params, 
    Configuration::ENDPOINTS[:summarize])
  @connection = Connection.new(endpoint, params, config)
  @connection.request!
end