Class: UClassify

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUClassify

Returns a new instance of UClassify.



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

def initialize
  @write_calls = Array.new
  @text_trains = Array.new
end

Instance Attribute Details

#read_api_keyObject

Returns the value of attribute read_api_key.



15
16
17
# File 'lib/uclassify.rb', line 15

def read_api_key
  @read_api_key
end

#write_api_keyObject

Returns the value of attribute write_api_key.



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

def write_api_key
  @write_api_key
end

Instance Method Details

#add_class(class_name) ⇒ Object



76
77
78
79
80
# File 'lib/uclassify.rb', line 76

def add_class (class_name)
  new_class = UClassifyClass.new(class_name)
  @write_calls.last.add_class(new_class)
  self
end

#create_classifier(classifier_name) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/uclassify.rb', line 22

def create_classifier(classifier_name)
  check_for_write_key
  @write_api_key = write_api_key
  write_call = UClassifyWriteCall.new(write_api_key,classifier_name)
  id = UClassifyCreateID.new(UClassifyUtils.string_to_id(classifier_name))
  write_call.add_create_id(id)
  @write_calls << write_call
end

#fire_requestObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/uclassify.rb', line 45

def fire_request
  request = UClassifyRequest.new
  @last_response = request.fire_request(generate_request_string)
  if is_response_valid?
    puts "SUCCESS!"
  else
    puts "FAILURE"
  end
  puts "Reply from server:"  
  @last_response
end

#generate_request_stringObject



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

def generate_request_string
  @document = Nokogiri::XML::Document.new("1.0")
  @document.encoding="utf-8"
  @uclassify_root_node =  Nokogiri::XML::Node.new('uclassify',@document)
  @uclassify_root_node['xmlns']="http://api.uclassify.com/1/RequestSchema"
  @uclassify_root_node['version']="1.01"
  @document.root=@uclassify_root_node
  generate_train_texts
  generate_write_calls
  xml_string = @document.to_xml
  xml_string.split.join("\n")
  xml_string   
end

#is_response_valid?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/uclassify.rb', line 57

def is_response_valid? 
  response_as_hash['uclassify']['status']['success'] == "true"
end

#response_as_hashObject



61
62
63
64
65
66
67
# File 'lib/uclassify.rb', line 61

def response_as_hash
  if @last_response
        @last_response.parsed_response
    else
      raise "No request has been fired yet. Please do a uclassify.fire_request"
    end
end

#train_class_with_text(class_name, text) ⇒ Object



82
83
84
85
86
87
# File 'lib/uclassify.rb', line 82

def train_class_with_text (class_name,text)
  train_id = UClassifyUtils.string_to_id(class_name)
  text_id = UClassifyUtils.string_to_id(text)
  add_training_class(train_id,class_name,text_id)
  train_text(text)
end

#untrain_class_with_text(class_name, text) ⇒ Object



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

def untrain_class_with_text(class_name,text)
  train_id = UClassifyUtils.string_to_id(class_name)
  text_id = UClassifyUtils.string_to_id(text)
  untrain_class(train_id,class_name,text_id)
  train_text(text)
  
end

#with_classifier_name(classifier_name) ⇒ Object



69
70
71
72
73
74
# File 'lib/uclassify.rb', line 69

def with_classifier_name classifier_name 
  check_for_write_key
  write_call = UClassifyWriteCall.new(@write_api_key,classifier_name)
  @write_calls << write_call
  self
end