Module: Monkeylearn::Classifiers

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

Class Method Summary collapse

Methods included from Requests

get_connection, request, throttled?

Class Method Details

.build_endpoint(*args) ⇒ Object



18
19
20
# File 'lib/monkeylearn/classifiers.rb', line 18

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

.categoriesObject



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

def categories
  return Categories
end

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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/monkeylearn/classifiers.rb', line 34

def classify(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, 'classify')
  query_params = { sandbox: true } if options[:sandbox]

  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, query_params
  end

  Monkeylearn::MultiResponse.new(responses)
end

.create(name, options = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/monkeylearn/classifiers.rb', line 50

def create(name, options = {})
  data = {
      name: name,
      description: options[:description],
      language: options[:language],
      ngram_range: options[:ngram_range],
      use_stemmer: options[:use_stemmer],
      stop_words: options[:stop_words],
      max_features: options[:max_features],
      strip_stopwords: options[:strip_stopwords],
      is_multilabel: options[:is_multilabel],
      is_twitter_data: options[:is_twitter_data],
      normalize_weights: options[:normalize_weights],
      classifier: options[:classifier],
      industry: options[:industry],
      classifier_type: options[:classifier_type],
      text_type: options[:text_type],
      permissions: options[:permissions]
  }.delete_if { |k,v| v.nil? }
  request :post, build_endpoint, data
end

.delete(module_id) ⇒ Object



97
98
99
# File 'lib/monkeylearn/classifiers.rb', line 97

def delete(module_id)
  request :delete, build_endpoint(module_id)
end

.deploy(module_id) ⇒ Object



93
94
95
# File 'lib/monkeylearn/classifiers.rb', line 93

def deploy(module_id)
  request :post, build_endpoint(module_id, 'deploy')
end

.detail(module_id) ⇒ Object



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

def detail(module_id)
  request :get, build_endpoint(module_id)
end

.train(module_id) ⇒ Object



89
90
91
# File 'lib/monkeylearn/classifiers.rb', line 89

def train(module_id)
  request :post, build_endpoint(module_id, 'train')
end

.upload_samples(module_id, samples_with_categories) ⇒ Object



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

def upload_samples(module_id, samples_with_categories)
  unless samples_with_categories.respond_to? :each
    raise MonkeylearnError, "The second param must be an enumerable type (i.e. an Array)."
  end
  endpoint = build_endpoint(module_id, 'samples')
  data = {
    samples: samples_with_categories.collect do |text, category_ids|
      {text: text, category_id: category_ids}
    end
  }
  request :post, endpoint, data
end

.validate_batch_size(batch_size) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/monkeylearn/classifiers.rb', line 22

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