uclassify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/.rvmrc +1 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +36 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +50 -0
- data/lib/uclassify.rb +129 -0
- data/lib/uclassify_class.rb +12 -0
- data/lib/uclassify_classifier.rb +14 -0
- data/lib/uclassify_create_id.rb +14 -0
- data/lib/uclassify_query.rb +67 -0
- data/lib/uclassify_request.rb +13 -0
- data/lib/uclassify_response.rb +15 -0
- data/lib/uclassify_text.rb +16 -0
- data/lib/uclassify_training_class.rb +15 -0
- data/lib/uclassify_utils.rb +6 -0
- data/lib/uclassify_write_call.rb +45 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/uclassify_class_spec.rb +12 -0
- data/spec/uclassify_classifier_spec.rb +10 -0
- data/spec/uclassify_create_id_spec.rb +12 -0
- data/spec/uclassify_query_spec.rb +13 -0
- data/spec/uclassify_spec.rb +66 -0
- data/spec/uclassify_text_spec.rb +12 -0
- data/spec/uclassify_training_class_spec.rb +10 -0
- data/spec/uclassify_utils_spec.rb +8 -0
- data/spec/uclassify_write_call_spec.rb +17 -0
- data/test-helpers/xml_compare.rb +19 -0
- metadata +186 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use 1.8.7@uclassify-dev
|
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
|
9
|
+
gem "nokogiri"
|
10
|
+
|
11
|
+
group :development do
|
12
|
+
gem "rspec", "~> 2.3.0"
|
13
|
+
gem "bundler", "~> 1.0.0"
|
14
|
+
gem "jeweler", "~> 1.6.4"
|
15
|
+
gem "rcov", ">= 0"
|
16
|
+
gem "httparty", '0.8.1'
|
17
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
httparty (0.8.1)
|
7
|
+
multi_json
|
8
|
+
multi_xml
|
9
|
+
jeweler (1.6.4)
|
10
|
+
bundler (~> 1.0)
|
11
|
+
git (>= 1.2.5)
|
12
|
+
rake
|
13
|
+
multi_json (1.0.3)
|
14
|
+
multi_xml (0.4.1)
|
15
|
+
nokogiri (1.5.0)
|
16
|
+
rake (0.9.2)
|
17
|
+
rcov (0.9.10)
|
18
|
+
rspec (2.3.0)
|
19
|
+
rspec-core (~> 2.3.0)
|
20
|
+
rspec-expectations (~> 2.3.0)
|
21
|
+
rspec-mocks (~> 2.3.0)
|
22
|
+
rspec-core (2.3.1)
|
23
|
+
rspec-expectations (2.3.0)
|
24
|
+
diff-lcs (~> 1.1.2)
|
25
|
+
rspec-mocks (2.3.0)
|
26
|
+
|
27
|
+
PLATFORMS
|
28
|
+
ruby
|
29
|
+
|
30
|
+
DEPENDENCIES
|
31
|
+
bundler (~> 1.0.0)
|
32
|
+
httparty (= 0.8.1)
|
33
|
+
jeweler (~> 1.6.4)
|
34
|
+
nokogiri
|
35
|
+
rcov
|
36
|
+
rspec (~> 2.3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Manish Chakravarty
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= uclassify
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to uclassify
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Manish Chakravarty. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "uclassify"
|
18
|
+
gem.homepage = "http://github.com/manishchaks/uclassify"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A Ruby gem for UClassify}
|
21
|
+
gem.description = %Q{A gem enabling you to create classsifers and use them using the UClassify API. www.uclassify.com/XmlApiDocumentation.aspx#examples}
|
22
|
+
gem.email = "manishchaks@gmail.com"
|
23
|
+
gem.authors = ["Manish Chakravarty"]
|
24
|
+
gem.version = '0.1.0'
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
+
spec.rcov = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'rake/rdoctask'
|
43
|
+
Rake::RDocTask.new do |rdoc|
|
44
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
45
|
+
|
46
|
+
rdoc.rdoc_dir = 'rdoc'
|
47
|
+
rdoc.title = "uclassify #{version}"
|
48
|
+
rdoc.rdoc_files.include('README*')
|
49
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
50
|
+
end
|
data/lib/uclassify.rb
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'nokogiri'
|
3
|
+
require File.join(File.dirname(__FILE__), 'uclassify_write_call.rb')
|
4
|
+
require File.join(File.dirname(__FILE__), 'uclassify_create_id.rb')
|
5
|
+
require File.join(File.dirname(__FILE__), 'uclassify_class.rb')
|
6
|
+
require File.join(File.dirname(__FILE__), 'uclassify_text.rb')
|
7
|
+
require File.join(File.dirname(__FILE__), 'uclassify_training_class.rb')
|
8
|
+
require File.join(File.dirname(__FILE__), 'uclassify_classifier.rb')
|
9
|
+
require File.join(File.dirname(__FILE__), 'uclassify_query.rb')
|
10
|
+
require File.join(File.dirname(__FILE__), 'uclassify_request.rb')
|
11
|
+
require File.join(File.dirname(__FILE__), 'uclassify_utils.rb')
|
12
|
+
class UClassify
|
13
|
+
attr_accessor :write_api_key
|
14
|
+
attr_accessor :read_api_key
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
@write_calls = Array.new
|
18
|
+
@text_trains = Array.new
|
19
|
+
end
|
20
|
+
|
21
|
+
def create_classifier(classifier_name)
|
22
|
+
check_for_write_key
|
23
|
+
@write_api_key = write_api_key
|
24
|
+
write_call = UClassifyWriteCall.new(write_api_key,classifier_name)
|
25
|
+
id = UClassifyCreateID.new(UClassifyUtils.string_to_id(classifier_name))
|
26
|
+
write_call.add_create_id(id)
|
27
|
+
@write_calls << write_call
|
28
|
+
end
|
29
|
+
|
30
|
+
def generate_request_string
|
31
|
+
@document = Nokogiri::XML::Document.new("1.0")
|
32
|
+
@document.encoding="utf-8"
|
33
|
+
@uclassify_root_node = Nokogiri::XML::Node.new('uclassify',@document)
|
34
|
+
@uclassify_root_node['xmlns']="http://api.uclassify.com/1/RequestSchema"
|
35
|
+
@uclassify_root_node['version']="1.01"
|
36
|
+
@document.root=@uclassify_root_node
|
37
|
+
generate_train_texts
|
38
|
+
generate_write_calls
|
39
|
+
xml_string = @document.to_xml
|
40
|
+
xml_string.split.join("\n")
|
41
|
+
xml_string
|
42
|
+
end
|
43
|
+
|
44
|
+
def fire_request
|
45
|
+
request = UClassifyRequest.new
|
46
|
+
@last_response = request.fire_request(generate_request_string)
|
47
|
+
if is_response_valid?
|
48
|
+
puts "SUCCESS!"
|
49
|
+
else
|
50
|
+
puts "FAILURE"
|
51
|
+
end
|
52
|
+
puts "Reply from server:"
|
53
|
+
@last_response
|
54
|
+
end
|
55
|
+
|
56
|
+
def is_response_valid?
|
57
|
+
response_as_hash['uclassify']['status']['success'] == "true"
|
58
|
+
end
|
59
|
+
|
60
|
+
def response_as_hash
|
61
|
+
if @last_response
|
62
|
+
@last_response.parsed_response
|
63
|
+
else
|
64
|
+
raise "No request has been fired yet. Please do a uclassify.fire_request"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def with_classifier_name classifier_name
|
69
|
+
check_for_write_key
|
70
|
+
write_call = UClassifyWriteCall.new(@write_api_key,classifier_name)
|
71
|
+
@write_calls << write_call
|
72
|
+
self
|
73
|
+
end
|
74
|
+
|
75
|
+
def add_class (class_name)
|
76
|
+
new_class = UClassifyClass.new(class_name)
|
77
|
+
@write_calls.last.add_class(new_class)
|
78
|
+
self
|
79
|
+
end
|
80
|
+
|
81
|
+
def train_class_with_text (class_name,text)
|
82
|
+
train_id = UClassifyUtils.string_to_id(class_name)
|
83
|
+
text_id = UClassifyUtils.string_to_id(text)
|
84
|
+
add_training_class(train_id,class_name,text_id)
|
85
|
+
train_text(text)
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def add_training_class (train_id,class_name,text_id)
|
91
|
+
training_class = UClassifyTrainClass.new(train_id,class_name,text_id)
|
92
|
+
@write_calls.last.add_training_class(training_class)
|
93
|
+
self
|
94
|
+
end
|
95
|
+
|
96
|
+
def train_text(text)
|
97
|
+
text = UClassifyText.new(text)
|
98
|
+
@text_trains << text
|
99
|
+
self
|
100
|
+
end
|
101
|
+
|
102
|
+
def generate_train_texts
|
103
|
+
#add all the text train calls
|
104
|
+
if @text_trains.size >0
|
105
|
+
main_texts_node = Nokogiri::XML::Node.new('texts',@document)
|
106
|
+
@uclassify_root_node.add_child(main_texts_node)
|
107
|
+
@text_trains.each do |text_train|
|
108
|
+
node=text_train.to_xml_node(@document)
|
109
|
+
main_texts_node.add_child(node)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def generate_write_calls
|
115
|
+
#add all the write calls
|
116
|
+
if @write_calls.size > 0
|
117
|
+
@write_calls.each do |write_call|
|
118
|
+
node=write_call.to_xml_node(@document)
|
119
|
+
@uclassify_root_node.add_child(node)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def check_for_write_key
|
125
|
+
if (!@write_api_key)
|
126
|
+
raise "No Write API Key for UClassify. Please specify one with 'uclassify.write_api_key=YOUR_KEY'"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class UClassifyClass
|
2
|
+
def initialize(class_name)
|
3
|
+
@class_name=class_name
|
4
|
+
end
|
5
|
+
|
6
|
+
def to_xml_node xml_document
|
7
|
+
class_node = Nokogiri::XML::Node.new('addClass',xml_document)
|
8
|
+
class_node['id']=(UClassifyUtils.string_to_id @class_name)
|
9
|
+
class_node['className']=@class_name
|
10
|
+
class_node
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class UClassifyClassifier
|
2
|
+
def initialize (classifier_name,text)
|
3
|
+
@classifier_name = classifier_name
|
4
|
+
@text = text
|
5
|
+
end
|
6
|
+
|
7
|
+
def to_xml_node xml_document
|
8
|
+
classifier_node = Nokogiri::XML::Node.new('classify',xml_document)
|
9
|
+
classifier_node['id'] = UClassifyUtils.string_to_id(@classifier_name)
|
10
|
+
classifier_node['classifierName'] = @classifier_name
|
11
|
+
classifier_node['textId'] = UClassifyUtils.string_to_id(@text)
|
12
|
+
classifier_node
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'nokogiri'
|
3
|
+
class UClassifyCreateID
|
4
|
+
def initialize (createid)
|
5
|
+
@create_id = createid
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_xml_node xml_document
|
9
|
+
create_id_node = Nokogiri::XML::Node.new('create',xml_document)
|
10
|
+
create_id_node['id'] = @create_id.to_s
|
11
|
+
create_id_node
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
class UClassifyQuery
|
2
|
+
def initialize (read_api_key)
|
3
|
+
@read_api_key = read_api_key
|
4
|
+
@texts = Array.new
|
5
|
+
@classifiers = Array.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def add_text (text)
|
9
|
+
text = UClassifyText.new(text)
|
10
|
+
@texts << text
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_classifier (classifier_name,text)
|
15
|
+
classifier = UClassifyClassifier.new(classifier_name,text)
|
16
|
+
@classifiers << classifier
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def classify_text(classifier_name,text)
|
21
|
+
add_classifier(classifier_name,text)
|
22
|
+
add_text(text)
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
def get_query_string
|
27
|
+
@document = Nokogiri::XML::Document.new("1.0")
|
28
|
+
@document.encoding="utf-8"
|
29
|
+
@uclassify_root_node = Nokogiri::XML::Node.new('uclassify',@document)
|
30
|
+
@uclassify_root_node['xmlns']="http://api.uclassify.com/1/RequestSchema"
|
31
|
+
@uclassify_root_node['version']="1.01"
|
32
|
+
@document.root=@uclassify_root_node
|
33
|
+
generate_texts
|
34
|
+
generate_read_calls
|
35
|
+
@document.to_xml
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def generate_read_calls
|
41
|
+
if @classifiers.size > 0
|
42
|
+
read_node = Nokogiri::XML::Node.new('readCalls',@document)
|
43
|
+
read_node['readApiKey']=@read_api_key
|
44
|
+
@uclassify_root_node.add_child(read_node)
|
45
|
+
@classifiers.each do |classifier|
|
46
|
+
node = classifier.to_xml_node(@document)
|
47
|
+
read_node.add_child(node)
|
48
|
+
end
|
49
|
+
else
|
50
|
+
raise 'ERROR: Classifiers not specified. Use add_classifier method to add classifiers to use for this query'
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def generate_texts
|
55
|
+
#add all the text train calls
|
56
|
+
if @texts.size >0
|
57
|
+
main_texts_node = Nokogiri::XML::Node.new('texts',@document)
|
58
|
+
@uclassify_root_node.add_child(main_texts_node)
|
59
|
+
@texts.each do |text|
|
60
|
+
node=text.to_xml_node(@document)
|
61
|
+
main_texts_node.add_child(node)
|
62
|
+
end
|
63
|
+
else
|
64
|
+
raise 'ERROR: Texts not specified. Use add_text method to add text(s) to run the classifier on'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'httparty'
|
3
|
+
class UClassifyRequest
|
4
|
+
include HTTParty
|
5
|
+
format :xml
|
6
|
+
base_uri 'http://api.uclassify.com'
|
7
|
+
headers 'Content-Type' => 'text/xml'
|
8
|
+
|
9
|
+
def fire_request(string)
|
10
|
+
self.class.post('',:body=>string)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'uclassify_create_id.rb')
|
2
|
+
class UClassifyResponse < HTTParty::Response
|
3
|
+
def initialize ( uclassify_response)
|
4
|
+
@uclassify_response = uclassify_response
|
5
|
+
end
|
6
|
+
|
7
|
+
def is_valid?
|
8
|
+
@uclassify_response.parsed_response['uclassify']['status']['success'] == "true"
|
9
|
+
end
|
10
|
+
|
11
|
+
def response_as_hash
|
12
|
+
@uclassify.parsed_response
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require "Base64"
|
2
|
+
class UClassifyText
|
3
|
+
def initialize(text)
|
4
|
+
@text = text
|
5
|
+
end
|
6
|
+
|
7
|
+
def to_xml_node xml_document
|
8
|
+
text_base64_node = Nokogiri::XML::Node.new('textBase64',xml_document)
|
9
|
+
text_base64_node['id'] = UClassifyUtils.string_to_id(@text)
|
10
|
+
encoded_string = Base64.encode64(@text)
|
11
|
+
|
12
|
+
frag = text_base64_node.parse(encoded_string.strip)
|
13
|
+
text_base64_node.add_child(frag)
|
14
|
+
text_base64_node
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class UClassifyTrainClass
|
2
|
+
def initialize(train_id,class_name,text_id)
|
3
|
+
@train_id = train_id
|
4
|
+
@class_name = class_name
|
5
|
+
@text_id = text_id
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_xml_node xml_document
|
9
|
+
class_node = Nokogiri::XML::Node.new('train',xml_document)
|
10
|
+
class_node['id']=@train_id
|
11
|
+
class_node['className']=@class_name
|
12
|
+
class_node['textId']=@text_id
|
13
|
+
class_node
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'uclassify_create_id.rb')
|
2
|
+
class UClassifyWriteCall
|
3
|
+
def initialize (write_api_key,classifier_name)
|
4
|
+
@api_key = write_api_key
|
5
|
+
@classifier_name = classifier_name
|
6
|
+
@createIDs = Array.new
|
7
|
+
@classes = Array.new
|
8
|
+
@training_classes = Array.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_create_id create_id
|
12
|
+
@createIDs << create_id
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_class my_class
|
16
|
+
@classes << my_class
|
17
|
+
end
|
18
|
+
|
19
|
+
def add_training_class training_class
|
20
|
+
@training_classes << training_class
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_xml_node xml_document
|
24
|
+
write_call_node= Nokogiri::XML::Node.new('writeCalls',xml_document)
|
25
|
+
write_call_node['writeApiKey'] = @api_key
|
26
|
+
write_call_node['classifierName']= @classifier_name
|
27
|
+
|
28
|
+
@createIDs.each do |id|
|
29
|
+
node = id.to_xml_node(xml_document)
|
30
|
+
write_call_node.add_child(node)
|
31
|
+
end
|
32
|
+
|
33
|
+
@classes.each do |my_class|
|
34
|
+
node = my_class.to_xml_node(xml_document)
|
35
|
+
write_call_node.add_child(node)
|
36
|
+
end
|
37
|
+
|
38
|
+
@training_classes.each do |training_class|
|
39
|
+
node = training_class.to_xml_node(xml_document)
|
40
|
+
write_call_node.add_child(node)
|
41
|
+
end
|
42
|
+
write_call_node
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require File.join(File.dirname(__FILE__), '..','/test-helpers/','xml_compare.rb')
|
5
|
+
# Requires supporting files with custom matchers and macros, etc,
|
6
|
+
# in ./support/ and its subdirectories.
|
7
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'lib/uclassify'
|
3
|
+
|
4
|
+
describe "UClassify Create Class" do
|
5
|
+
it "Should be able to create a simple class" do
|
6
|
+
create_class = UClassifyClass.new("DUMMY_CLASS")
|
7
|
+
document = Nokogiri::XML::Document.new
|
8
|
+
|
9
|
+
expected_string = "<create id=\"DUMMY_CREATE_ID\"/>"
|
10
|
+
create_class.to_xml_node(document).to_xml.should =="<addClass id=\"7642b754271a20bbf7b0d91962ec62e5\" className=\"DUMMY_CLASS\"/>"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'lib/uclassify'
|
3
|
+
|
4
|
+
describe "UClassify Classifer Use" do
|
5
|
+
it "Should be able to create a classifier name and text" do
|
6
|
+
classifier = UClassifyClassifier.new('classifier_name','text')
|
7
|
+
document = Nokogiri::XML::Document.new
|
8
|
+
classifier.to_xml_node(document).to_xml.should == "<classify id=\"7fd9109348061ba2dc36223395ae0760\" classifierName=\"classifier_name\" textId=\"1cb251ec0d568de6a929b520c4aed8d1\"/>"
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'lib/uclassify'
|
3
|
+
|
4
|
+
describe "UClassify Create ID" do
|
5
|
+
it "Should be able to a create a classifier" do
|
6
|
+
create_id = UClassifyCreateID.new('DUMMY_CREATE_ID')
|
7
|
+
document = Nokogiri::XML::Document.new
|
8
|
+
|
9
|
+
expected_string = "<create id=\"DUMMY_CREATE_ID\"/>"
|
10
|
+
xml_cmp(create_id.to_xml_node(document).to_xml, expected_string).should == true
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'lib/uclassify'
|
3
|
+
|
4
|
+
describe "UClassify Query" do
|
5
|
+
it "Should be able to add classifier to a query" do
|
6
|
+
document = Nokogiri::XML::Document.new
|
7
|
+
query = UClassifyQuery.new('DUMMY_READ_KEY')
|
8
|
+
query.classify_text('classifier_name','text')
|
9
|
+
expected_string ="<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<uclassify xmlns=\"http://api.uclassify.com/1/RequestSchema\" version=\"1.01\">\n <texts>\n <textBase64 id=\"1cb251ec0d568de6a929b520c4aed8d1\">dGV4dA==</textBase64>\n </texts>\n <readCalls readApiKey=\"DUMMY_READ_KEY\">\n <classify id=\"7fd9109348061ba2dc36223395ae0760\" classifierName=\"classifier_name\" textId=\"1cb251ec0d568de6a929b520c4aed8d1\"/>\n </readCalls>\n</uclassify>\n"
|
10
|
+
xml_cmp(query.get_query_string,expected_string).should == true
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
require 'lib/uclassify'
|
4
|
+
|
5
|
+
describe "UClassify base" do
|
6
|
+
it "Should be able to a create a classifier" do
|
7
|
+
uclassify = UClassify.new
|
8
|
+
uclassify.write_api_key = 'DUMMY_API_KEY'
|
9
|
+
uclassify.create_classifier('DUMMY_CLASSIFIER')
|
10
|
+
expected_string = %Q{
|
11
|
+
<?xml version="1.0" encoding="utf-8"?>
|
12
|
+
<uclassify xmlns="http://api.uclassify.com/1/RequestSchema" version="1.01">
|
13
|
+
<writeCalls writeApiKey="DUMMY_API_KEY" classifierName="DUMMY_CLASSIFIER">
|
14
|
+
<create id="5d32967361454d354c26dde8fab3ce28"/>
|
15
|
+
</writeCalls>
|
16
|
+
</uclassify>
|
17
|
+
}
|
18
|
+
xml_cmp(uclassify.generate_request_string,expected_string).should == true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should not allow classifier creation and raise an exception when write api key has not been specified" do
|
22
|
+
begin
|
23
|
+
uclassify = UClassify.new
|
24
|
+
uclassify.create_classifier('DUMMY_CLASSIFIER')
|
25
|
+
rescue Exception => e
|
26
|
+
e.to_s.should == "No Write API Key for UClassify. Please specify one with 'uclassify.write_api_key=YOUR_KEY'"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should allow classes to be added to a classifier" do
|
31
|
+
uclassify = UClassify.new
|
32
|
+
uclassify.write_api_key = 'DUMMY_API_KEY'
|
33
|
+
uclassify.with_classifier_name("DUMMY_CLASSIFIER_ID").add_class("Class_Name_1").add_class("Class_Name_2")
|
34
|
+
|
35
|
+
expected_string = %Q{
|
36
|
+
<?xml version="1.0" encoding="utf-8"?>
|
37
|
+
<uclassify xmlns="http://api.uclassify.com/1/RequestSchema" version="1.01">
|
38
|
+
<writeCalls writeApiKey="DUMMY_API_KEY" classifierName="DUMMY_CLASSIFIER_ID">
|
39
|
+
<addClass id="451685f25480447daf6f6d7b0a5df79d" className="Class_Name_1"/>
|
40
|
+
<addClass id="e0d6cc5b323e77cf61428f3bfc0ef894" className="Class_Name_2"/>
|
41
|
+
</writeCalls>
|
42
|
+
</uclassify>
|
43
|
+
}
|
44
|
+
xml_cmp(uclassify.generate_request_string,expected_string).should==true
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should be possible to train classes with texts " do
|
48
|
+
uclassify = UClassify.new
|
49
|
+
uclassify.write_api_key = 'DUMMY_API_KEY'
|
50
|
+
uclassify.with_classifier_name("DUMMY_CLASSIFIER").train_class_with_text("ClassOne","SomeTextOne").train_class_with_text("ClassTwo","SomeTextTwo")
|
51
|
+
|
52
|
+
expected_string = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
|
53
|
+
<uclassify xmlns=\"http://api.uclassify.com/1/RequestSchema\" version=\"1.01\">
|
54
|
+
<texts>
|
55
|
+
<textBase64 id=\"a9aea8382b0cfabd38baaa7ae6e66937\">U29tZVRleHRPbmU=</textBase64>
|
56
|
+
<textBase64 id=\"30baefbb457d5a23b95b184c8f05eb8d\">U29tZVRleHRUd28=</textBase64>
|
57
|
+
</texts>
|
58
|
+
<writeCalls writeApiKey=\"DUMMY_API_KEY\" classifierName=\"DUMMY_CLASSIFIER\">
|
59
|
+
<train id=\"7f2e5c9c34e90179ea539c7f7bc1cc1a\" className=\"ClassOne\" textId=\"a9aea8382b0cfabd38baaa7ae6e66937\"/>
|
60
|
+
<train id=\"7911cc8cbb85a37c063049a9c7336acd\" className=\"ClassTwo\" textId=\"30baefbb457d5a23b95b184c8f05eb8d\"/>
|
61
|
+
</writeCalls>
|
62
|
+
</uclassify>"
|
63
|
+
|
64
|
+
xml_cmp(uclassify.generate_request_string,expected_string).should==true
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'lib/uclassify'
|
3
|
+
|
4
|
+
describe "UClassify Train text" do
|
5
|
+
it "should be able to create a simple train text" do\
|
6
|
+
train_text = UClassifyText.new("DUMMY_TEXT")
|
7
|
+
document = Nokogiri::XML::Document.new
|
8
|
+
expected_xml = "<textBase64 id=\"d2b2dd63ac18b9ce2829358489d73134\">RFVNTVlfVEVYVA==</textBase64>"
|
9
|
+
xml_cmp(train_text.to_xml_node(document).to_xml,expected_xml).should == true
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'lib/uclassify'
|
3
|
+
|
4
|
+
describe "UClassify Train Class" do
|
5
|
+
it "Should be able to create a training class given and ID, Name and Text" do
|
6
|
+
train_class = UClassifyTrainClass.new("DUMMY_ID","DUMMY_CLASS_NAME", "DUMMY_TEXT_ID")
|
7
|
+
document = Nokogiri::XML::Document.new
|
8
|
+
train_class.to_xml_node(document).to_xml.should == "<train id=\"DUMMY_ID\" className=\"DUMMY_CLASS_NAME\" textId=\"DUMMY_TEXT_ID\"/>"
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'lib/uclassify'
|
3
|
+
|
4
|
+
describe "UClassify Utils" do
|
5
|
+
it "Should take a string and convert it into a valid UClassify ID" do
|
6
|
+
UClassifyUtils.string_to_id('Hello how are you').should == '512a1a7503426d5400b1d51011540279'
|
7
|
+
end
|
8
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'lib/uclassify'
|
3
|
+
|
4
|
+
describe "UClassify Create Call" do
|
5
|
+
it "Should be able to create a classifier with a given name and createID" do
|
6
|
+
write_call = UClassifyWriteCall.new("DUMMY_WRITE_API_KEY","DUMMY_CLASSIFIER_ID")
|
7
|
+
|
8
|
+
# add a create operation to the write call
|
9
|
+
create_id = UClassifyCreateID.new('DUMMY_CREATE_ID')
|
10
|
+
write_call.add_create_id(create_id)
|
11
|
+
|
12
|
+
document = Nokogiri::XML::Document.new
|
13
|
+
expected_string= "<writeCalls writeApiKey=\"DUMMY_WRITE_API_KEY\" classifierName=\"DUMMY_CLASSIFIER_ID\"><create id=\"DUMMY_CREATE_ID\"/></writeCalls>"
|
14
|
+
|
15
|
+
xml_cmp(write_call.to_xml_node(document).to_xml , expected_string).should == true
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rexml/document'
|
3
|
+
|
4
|
+
# A simple method to compare two XML files - regardless of whitepsace and carriage returns
|
5
|
+
def xml_cmp a, b
|
6
|
+
a = REXML::Document.new(a.to_s)
|
7
|
+
b = REXML::Document.new(b.to_s)
|
8
|
+
|
9
|
+
normalized = Class.new(REXML::Formatters::Pretty) do
|
10
|
+
def write_text(node, output)
|
11
|
+
super(node.to_s.strip, output)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
normalized.new(indentation=0,ie_hack=false).write(node=a, a_normalized='')
|
16
|
+
normalized.new(indentation=0,ie_hack=false).write(node=b, b_normalized='')
|
17
|
+
|
18
|
+
a_normalized == b_normalized
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,186 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uclassify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Manish Chakravarty
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-11-06 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
type: :runtime
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
version_requirements: *id001
|
32
|
+
name: nokogiri
|
33
|
+
prerelease: false
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
type: :development
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 3
|
42
|
+
segments:
|
43
|
+
- 2
|
44
|
+
- 3
|
45
|
+
- 0
|
46
|
+
version: 2.3.0
|
47
|
+
version_requirements: *id002
|
48
|
+
name: rspec
|
49
|
+
prerelease: false
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
type: :development
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 23
|
58
|
+
segments:
|
59
|
+
- 1
|
60
|
+
- 0
|
61
|
+
- 0
|
62
|
+
version: 1.0.0
|
63
|
+
version_requirements: *id003
|
64
|
+
name: bundler
|
65
|
+
prerelease: false
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
type: :development
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ~>
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 7
|
74
|
+
segments:
|
75
|
+
- 1
|
76
|
+
- 6
|
77
|
+
- 4
|
78
|
+
version: 1.6.4
|
79
|
+
version_requirements: *id004
|
80
|
+
name: jeweler
|
81
|
+
prerelease: false
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
type: :development
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
version_requirements: *id005
|
94
|
+
name: rcov
|
95
|
+
prerelease: false
|
96
|
+
- !ruby/object:Gem::Dependency
|
97
|
+
type: :development
|
98
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - "="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
hash: 61
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
- 8
|
107
|
+
- 1
|
108
|
+
version: 0.8.1
|
109
|
+
version_requirements: *id006
|
110
|
+
name: httparty
|
111
|
+
prerelease: false
|
112
|
+
description: A gem enabling you to create classsifers and use them using the UClassify API. www.uclassify.com/XmlApiDocumentation.aspx#examples
|
113
|
+
email: manishchaks@gmail.com
|
114
|
+
executables: []
|
115
|
+
|
116
|
+
extensions: []
|
117
|
+
|
118
|
+
extra_rdoc_files:
|
119
|
+
- LICENSE.txt
|
120
|
+
- README.rdoc
|
121
|
+
files:
|
122
|
+
- .document
|
123
|
+
- .rspec
|
124
|
+
- .rvmrc
|
125
|
+
- Gemfile
|
126
|
+
- Gemfile.lock
|
127
|
+
- LICENSE.txt
|
128
|
+
- README.rdoc
|
129
|
+
- Rakefile
|
130
|
+
- lib/uclassify.rb
|
131
|
+
- lib/uclassify_class.rb
|
132
|
+
- lib/uclassify_classifier.rb
|
133
|
+
- lib/uclassify_create_id.rb
|
134
|
+
- lib/uclassify_query.rb
|
135
|
+
- lib/uclassify_request.rb
|
136
|
+
- lib/uclassify_response.rb
|
137
|
+
- lib/uclassify_text.rb
|
138
|
+
- lib/uclassify_training_class.rb
|
139
|
+
- lib/uclassify_utils.rb
|
140
|
+
- lib/uclassify_write_call.rb
|
141
|
+
- spec/spec_helper.rb
|
142
|
+
- spec/uclassify_class_spec.rb
|
143
|
+
- spec/uclassify_classifier_spec.rb
|
144
|
+
- spec/uclassify_create_id_spec.rb
|
145
|
+
- spec/uclassify_query_spec.rb
|
146
|
+
- spec/uclassify_spec.rb
|
147
|
+
- spec/uclassify_text_spec.rb
|
148
|
+
- spec/uclassify_training_class_spec.rb
|
149
|
+
- spec/uclassify_utils_spec.rb
|
150
|
+
- spec/uclassify_write_call_spec.rb
|
151
|
+
- test-helpers/xml_compare.rb
|
152
|
+
homepage: http://github.com/manishchaks/uclassify
|
153
|
+
licenses:
|
154
|
+
- MIT
|
155
|
+
post_install_message:
|
156
|
+
rdoc_options: []
|
157
|
+
|
158
|
+
require_paths:
|
159
|
+
- lib
|
160
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
161
|
+
none: false
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
hash: 3
|
166
|
+
segments:
|
167
|
+
- 0
|
168
|
+
version: "0"
|
169
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
170
|
+
none: false
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
hash: 3
|
175
|
+
segments:
|
176
|
+
- 0
|
177
|
+
version: "0"
|
178
|
+
requirements: []
|
179
|
+
|
180
|
+
rubyforge_project:
|
181
|
+
rubygems_version: 1.8.6
|
182
|
+
signing_key:
|
183
|
+
specification_version: 3
|
184
|
+
summary: A Ruby gem for UClassify
|
185
|
+
test_files: []
|
186
|
+
|