google_prediction 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
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
+ gem "activesupport", ">= 2.3.2"
7
+ gem "gdata", ">= 1.1.1"
8
+
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem "shoulda", ">= 0"
13
+ gem "bundler", "~> 1.0.0"
14
+ gem "jeweler", "~> 1.5.2"
15
+ gem "rcov", ">= 0"
16
+ gem "flexmock", ">=0"
17
+ end
@@ -0,0 +1,26 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.0.5)
5
+ flexmock (0.9.0)
6
+ gdata (1.1.1)
7
+ git (1.2.5)
8
+ jeweler (1.5.2)
9
+ bundler (~> 1.0.0)
10
+ git (>= 1.2.5)
11
+ rake
12
+ rake (0.8.7)
13
+ rcov (0.9.9)
14
+ shoulda (2.11.3)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ activesupport (>= 2.3.2)
21
+ bundler (~> 1.0.0)
22
+ flexmock
23
+ gdata (>= 1.1.1)
24
+ jeweler (~> 1.5.2)
25
+ rcov
26
+ shoulda
@@ -0,0 +1,202 @@
1
+
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [2010] Google Inc.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
@@ -0,0 +1,57 @@
1
+ = Google Prediction API for Ruby
2
+
3
+ * http://code.google.com/p/ruby-google-prediction-api/
4
+
5
+ == DESCRIPTION:
6
+
7
+ Ruby wrapper for working with Google Prediction API
8
+
9
+ == SYNOPSIS:
10
+
11
+ # This assumes you have training data in Google Storage at gs://bucket/object.csv
12
+ @classifier = GooglePrediction::Classifier.new('api@google.com', 'password', 'bucket', 'object.csv')
13
+
14
+ # Train the model on the data
15
+ @classifier.train!
16
+
17
+ # Check the model for its accuracy
18
+ @classifier.check['data']['modelinfo']
19
+ => 'estimated accuracy: 0.76'
20
+
21
+ @classifier.predict('La idioma mas fina')
22
+ => 'spanish'
23
+
24
+ == REQUIREMENTS:
25
+
26
+ * gdata gem at version 1.1.1
27
+ * training data hosted on Google Storage
28
+
29
+ Tested against ...
30
+
31
+ == INSTALL:
32
+
33
+ sudo gem install ruby-google-prediction-api
34
+
35
+ To generate documentation:
36
+
37
+ rake doc
38
+
39
+ To run unit tests:
40
+
41
+ rake test
42
+
43
+ == LICENSE:
44
+
45
+ Copyright (C) 2010 Google Inc.
46
+
47
+ Licensed under the Apache License, Version 2.0 (the "License");
48
+ you may not use this file except in compliance with the License.
49
+ You may obtain a copy of the License at
50
+
51
+ http://www.apache.org/licenses/LICENSE-2.0
52
+
53
+ Unless required by applicable law or agreed to in writing, software
54
+ distributed under the License is distributed on an "AS IS" BASIS,
55
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
56
+ See the License for the specific language governing permissions and
57
+ limitations under the License.
@@ -0,0 +1,74 @@
1
+ # Copyright (C) 2010 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'rubygems'
16
+ require 'bundler'
17
+ begin
18
+ Bundler.setup(:default, :development)
19
+ rescue Bundler::BundlerError => e
20
+ $stderr.puts e.message
21
+ $stderr.puts "Run `bundle install` to install missing gems"
22
+ exit e.status_code
23
+ end
24
+ require 'rake'
25
+
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |gem|
28
+ gem.authors = ['Brandon Liu']
29
+ gem.email = 'bdon@google.com'
30
+ gem.homepage = 'http://code.google.com/p/ruby-google-prediction-api'
31
+ gem.summary = "Google Prediction API Ruby Library"
32
+ gem.name = 'google_prediction'
33
+ gem.version = '0.0.1'
34
+ gem.add_runtime_dependency('gdata', '>= 1.1.1')
35
+ gem.add_runtime_dependency('activesupport', '>= 2.3.2')
36
+ gem.requirements = 'Training data on Google Storage'
37
+ gem.description = "This gem provides a wrapper around the Google Prediction API."
38
+ end
39
+
40
+ Jeweler::RubygemsDotOrgTasks.new
41
+
42
+ require 'rake/testtask'
43
+ Rake::TestTask.new(:test) do |test|
44
+ test.libs << 'lib' << 'test'
45
+ test.pattern = 'test/**/test_*.rb'
46
+ test.verbose = true
47
+ end
48
+
49
+ require 'rcov/rcovtask'
50
+ Rcov::RcovTask.new do |test|
51
+ test.libs << 'test'
52
+ test.pattern = 'test/**/test_*.rb'
53
+ test.verbose = true
54
+ end
55
+
56
+ task :default => :test
57
+
58
+ require 'rake/rdoctask'
59
+ Rake::RDocTask.new do |rdoc|
60
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
61
+
62
+ rdoc.rdoc_dir = 'rdoc'
63
+ rdoc.title = "hello-gem #{version}"
64
+ rdoc.rdoc_files.include('README*')
65
+ rdoc.rdoc_files.include('lib/**/*.rb')
66
+ end
67
+
68
+ task :test do
69
+ ruby "test/test_google_prediction.rb"
70
+ end
71
+
72
+ task :doc do
73
+ system "rdoc"
74
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,85 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{google_prediction}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Brandon Liu"]
12
+ s.date = %q{2011-03-22}
13
+ s.description = %q{This gem provides a wrapper around the Google Prediction API.}
14
+ s.email = %q{bdon@google.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "google_prediction.gemspec",
28
+ "lib/google_prediction.rb",
29
+ "lib/google_prediction/classifier.rb",
30
+ "lib/ruby-google-prediction-api.rb",
31
+ "test/helper.rb",
32
+ "test/test_google_prediction.rb",
33
+ "test/test_helper.rb",
34
+ "test/test_ruby-google-prediction-api.rb"
35
+ ]
36
+ s.homepage = %q{http://code.google.com/p/ruby-google-prediction-api}
37
+ s.require_paths = ["lib"]
38
+ s.requirements = ["Training data on Google Storage"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{Google Prediction API Ruby Library}
41
+ s.test_files = [
42
+ "test/helper.rb",
43
+ "test/test_google_prediction.rb",
44
+ "test/test_helper.rb",
45
+ "test/test_ruby-google-prediction-api.rb"
46
+ ]
47
+
48
+ if s.respond_to? :specification_version then
49
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
50
+ s.specification_version = 3
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.2"])
54
+ s.add_runtime_dependency(%q<gdata>, [">= 1.1.1"])
55
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
56
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
57
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
58
+ s.add_development_dependency(%q<rcov>, [">= 0"])
59
+ s.add_development_dependency(%q<flexmock>, [">= 0"])
60
+ s.add_runtime_dependency(%q<gdata>, [">= 1.1.1"])
61
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.3.2"])
62
+ else
63
+ s.add_dependency(%q<activesupport>, [">= 2.3.2"])
64
+ s.add_dependency(%q<gdata>, [">= 1.1.1"])
65
+ s.add_dependency(%q<shoulda>, [">= 0"])
66
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
67
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
68
+ s.add_dependency(%q<rcov>, [">= 0"])
69
+ s.add_dependency(%q<flexmock>, [">= 0"])
70
+ s.add_dependency(%q<gdata>, [">= 1.1.1"])
71
+ s.add_dependency(%q<activesupport>, [">= 2.3.2"])
72
+ end
73
+ else
74
+ s.add_dependency(%q<activesupport>, [">= 2.3.2"])
75
+ s.add_dependency(%q<gdata>, [">= 1.1.1"])
76
+ s.add_dependency(%q<shoulda>, [">= 0"])
77
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
78
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
79
+ s.add_dependency(%q<rcov>, [">= 0"])
80
+ s.add_dependency(%q<flexmock>, [">= 0"])
81
+ s.add_dependency(%q<gdata>, [">= 1.1.1"])
82
+ s.add_dependency(%q<activesupport>, [">= 2.3.2"])
83
+ end
84
+ end
85
+
@@ -0,0 +1,17 @@
1
+ # Copyright (C) 2010 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ $:.unshift(File.dirname(__FILE__))
16
+
17
+ require 'google_prediction/classifier'
@@ -0,0 +1,113 @@
1
+ # Copyright (C) 2010 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'rubygems'
16
+ require 'gdata'
17
+ require 'active_support'
18
+ require 'active_support/json'
19
+
20
+ module GooglePrediction
21
+
22
+ # Base class for all exceptions raised by the classifier.
23
+ class ClassifierException < StandardError
24
+ end
25
+
26
+ # the Classifier class encapsulates one predictive model.
27
+ class Classifier
28
+ HEADERS = {'Content-Type' => 'application/json'}
29
+
30
+ # Create a new Classifier.
31
+ #
32
+ # Args:
33
+ # - login: your google login (passed to GData)
34
+ # - password: your google password.
35
+ # - bucket: the Google Storage bucket where the training data lives.
36
+ # - object: the name of the training data, including file extension.
37
+ #
38
+ def initialize(login, password, bucket, object)
39
+
40
+ # Use the GData gem to authenticate over HTTP.
41
+ @login = GData::Auth::ClientLogin.new('xapi')
42
+ @login.get_token(login, password, '')
43
+
44
+ @service = GData::HTTP::DefaultService.new
45
+ @bucket = bucket
46
+ @object = object
47
+ @base_uri = "https://www.googleapis.com/prediction/v1/training"
48
+ @model = "#{@bucket}%2F#{@object}"
49
+ end
50
+
51
+ # Send a request to start training on the model in @bucket/@object.
52
+ #
53
+ # Returns:
54
+ # Hash of response from server. See web API documentation.
55
+ #
56
+ def train!
57
+ body = {:data => {}}.to_json
58
+ req = GData::HTTP::Request.new(@base_uri + '?data=' + @model,
59
+ {:method => :post, :body => body, :headers => HEADERS})
60
+ sign_and_request(req)
61
+ end
62
+
63
+ # Send a check request for the status/accuracy of the model.
64
+ #
65
+ # Returns:
66
+ # A hash with information about the model. See web API documentation.
67
+ #
68
+ def check
69
+ req = GData::HTTP::Request.new(@base_uri + '/' + @model)
70
+ sign_and_request(req)
71
+ end
72
+
73
+ # Send a predict request to the API.
74
+ #
75
+ # Args:
76
+ # - signals: a variable number of text and numeric features.
77
+ #
78
+ # Returns:
79
+ # - The output label as a String.
80
+ #
81
+ # Raises:
82
+ # - ClassifierException. Catch this and wait/retry if hitting rate limit.
83
+ #
84
+ def predict(*signals)
85
+ body = {:data => {:input => {:text => [signals]}}}.to_json
86
+ req = GData::HTTP::Request.new(@base_uri + '/' + @model + "/predict",
87
+ {:method => :post, :body => body, :headers => HEADERS})
88
+ hsh = sign_and_request(req)
89
+
90
+ return hsh['data']['output']['output_label'] if hsh['data']
91
+ if hsh['error']
92
+ raise ClassifierException, hsh['error']['message']
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ # Signs an HTTP request using the auth token and make the request.
99
+ #
100
+ # Args:
101
+ # - req: a GData::HTTP::Request object.
102
+ #
103
+ # Returns:
104
+ # - a hash representing the response body.
105
+ #
106
+ def sign_and_request(req)
107
+ @login.sign_request!(req)
108
+ ActiveSupport::JSON.decode(@service.make_request(req).body)
109
+ end
110
+
111
+ end
112
+
113
+ end
File without changes
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'ruby-google-prediction-api'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,62 @@
1
+ # Copyright (C) 2010 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ $:.unshift(File.dirname(__FILE__))
16
+ require 'test_helper'
17
+
18
+
19
+ class TestGooglePrediction < Test::Unit::TestCase
20
+
21
+ def setup
22
+ login = flexmock(:get_token => true, :sign_request! => true)
23
+ flexmock(GData::Auth::ClientLogin).should_receive(:new).and_return(login)
24
+
25
+ @srv = flexmock
26
+ flexmock(GData::HTTP::DefaultService).should_receive(:new).and_return(@srv)
27
+
28
+ @cls = GooglePrediction::Classifier.new('apiuser@google.com',
29
+ 'password',
30
+ 'bucket',
31
+ 'object.csv')
32
+ end
33
+
34
+ def test_predict_success
35
+ body = {'data' => {'output' => {'output_label' => 'label'}}}.to_json
36
+ @srv.should_receive(:make_request).once.and_return(flexmock(:body => body))
37
+ assert @cls.predict('signal')
38
+ end
39
+
40
+ def test_predict_exception
41
+ body = {'error' => 'message'}.to_json
42
+ @srv.should_receive(:make_request).once.and_return(flexmock(:body => body))
43
+
44
+ assert_raise GooglePrediction::ClassifierException do
45
+ @cls.predict('signal')
46
+ end
47
+ end
48
+
49
+ # TODO: improve these tests...
50
+ def test_check
51
+ body = {}.to_json
52
+ @srv.should_receive(:make_request).once.and_return(flexmock(:body => body))
53
+ @cls.check
54
+ end
55
+
56
+ def test_train
57
+ body = {}.to_json
58
+ @srv.should_receive(:make_request).once.and_return(flexmock(:body => body))
59
+ @cls.train!
60
+ end
61
+
62
+ end
@@ -0,0 +1,20 @@
1
+ # Copyright (C) 2010 Google Inc.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ require 'test/unit'
16
+ require 'rubygems'
17
+ require 'flexmock/test_unit'
18
+
19
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
20
+ require 'google_prediction'
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestRubyGooglePredictionApi < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,211 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_prediction
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Brandon Liu
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-03-22 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activesupport
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 3
30
+ - 2
31
+ version: 2.3.2
32
+ type: :runtime
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: gdata
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 1
45
+ - 1
46
+ version: 1.1.1
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: shoulda
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: bundler
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 1
72
+ - 0
73
+ - 0
74
+ version: 1.0.0
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: jeweler
80
+ requirement: &id005 !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 1
87
+ - 5
88
+ - 2
89
+ version: 1.5.2
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: *id005
93
+ - !ruby/object:Gem::Dependency
94
+ name: rcov
95
+ requirement: &id006 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ segments:
101
+ - 0
102
+ version: "0"
103
+ type: :development
104
+ prerelease: false
105
+ version_requirements: *id006
106
+ - !ruby/object:Gem::Dependency
107
+ name: flexmock
108
+ requirement: &id007 !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: *id007
119
+ - !ruby/object:Gem::Dependency
120
+ name: gdata
121
+ requirement: &id008 !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ segments:
127
+ - 1
128
+ - 1
129
+ - 1
130
+ version: 1.1.1
131
+ type: :runtime
132
+ prerelease: false
133
+ version_requirements: *id008
134
+ - !ruby/object:Gem::Dependency
135
+ name: activesupport
136
+ requirement: &id009 !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ segments:
142
+ - 2
143
+ - 3
144
+ - 2
145
+ version: 2.3.2
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: *id009
149
+ description: This gem provides a wrapper around the Google Prediction API.
150
+ email: bdon@google.com
151
+ executables: []
152
+
153
+ extensions: []
154
+
155
+ extra_rdoc_files:
156
+ - LICENSE.txt
157
+ - README.rdoc
158
+ files:
159
+ - .document
160
+ - Gemfile
161
+ - Gemfile.lock
162
+ - LICENSE.txt
163
+ - README.rdoc
164
+ - Rakefile
165
+ - VERSION
166
+ - google_prediction.gemspec
167
+ - lib/google_prediction.rb
168
+ - lib/google_prediction/classifier.rb
169
+ - lib/ruby-google-prediction-api.rb
170
+ - test/helper.rb
171
+ - test/test_google_prediction.rb
172
+ - test/test_helper.rb
173
+ - test/test_ruby-google-prediction-api.rb
174
+ has_rdoc: true
175
+ homepage: http://code.google.com/p/ruby-google-prediction-api
176
+ licenses: []
177
+
178
+ post_install_message:
179
+ rdoc_options: []
180
+
181
+ require_paths:
182
+ - lib
183
+ required_ruby_version: !ruby/object:Gem::Requirement
184
+ none: false
185
+ requirements:
186
+ - - ">="
187
+ - !ruby/object:Gem::Version
188
+ hash: -3115851700946741873
189
+ segments:
190
+ - 0
191
+ version: "0"
192
+ required_rubygems_version: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ">="
196
+ - !ruby/object:Gem::Version
197
+ segments:
198
+ - 0
199
+ version: "0"
200
+ requirements:
201
+ - Training data on Google Storage
202
+ rubyforge_project:
203
+ rubygems_version: 1.3.7
204
+ signing_key:
205
+ specification_version: 3
206
+ summary: Google Prediction API Ruby Library
207
+ test_files:
208
+ - test/helper.rb
209
+ - test/test_google_prediction.rb
210
+ - test/test_helper.rb
211
+ - test/test_ruby-google-prediction-api.rb