Module: Monkeylearn::Extractors

Extended by:
Requests
Defined in:
lib/monkeylearn/extractors.rb

Class Method Summary collapse

Methods included from Requests

get_connection, request, throttled?

Class Method Details

.build_endpoint(*args) ⇒ Object



14
15
16
# File 'lib/monkeylearn/extractors.rb', line 14

def build_endpoint(*args)
  File.join('extractors', *args) + '/'
end

.extract(module_id, texts, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/monkeylearn/extractors.rb', line 30

def extract(module_id, texts, options = {})
  options[:batch_size] ||= Monkeylearn::Defaults.default_batch_size
  batch_size = options[:batch_size]
  validate_batch_size batch_size

  endpoint = build_endpoint(module_id, 'extract')

  responses = (0...texts.length).step(batch_size).collect do |start_idx|
    data = { text_list: texts.slice(start_idx, batch_size) }
    response = request :post, endpoint, data
  end

  Monkeylearn::MultiResponse.new(responses)
end

.validate_batch_size(batch_size) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/monkeylearn/extractors.rb', line 18

def validate_batch_size(batch_size)
  max_size = Monkeylearn::Defaults.max_batch_size
  if batch_size >  max_size
    raise MonkeylearnError, "The param batch_size is too big, max value is #{max_size}."
  end
  min_size = Monkeylearn::Defaults.min_batch_size
  if batch_size <  min_size
    raise MonkeylearnError, "The param batch_size is too small, min value is #{min_size}."
  end
  true
end