Class: GooglePrediction
- Inherits:
-
Object
- Object
- GooglePrediction
- Defined in:
- lib/google-prediction.rb
Overview
Provides an interface to the Google Prediction API.
- Author
-
Sam King ([email protected])
- Support
- Copyright
- License
-
GPLv3
Usage
auth_token = GooglePrediction.get_auth_token(‘[email protected]’, ‘password’)
=> long string of letters and numbers
predictor = GooglePrediction.new(auth_token, ‘bucket’, ‘object’)
predictor.#train
=> {"data"=>{"data"=>"bucket/object"}}
predictor.#check_training
=> "Training has not completed"
wait_some_time
predictor.#check_training
=> "no estimate available" or something between "0.0" and "1.0"
predictor.#predict “awesome company”
=> "Google"
predictor.#predict “awesome nonprofit”
=> "InSTEDD"
predictor.#predict 13
=> "lucky"
predictor.#predict [3, 5, 7, 11]
=> "prime"
Constant Summary collapse
- PREDICTION_URL_PREFIX =
'https://www.googleapis.com/prediction/v1/training'
Instance Attribute Summary collapse
-
#auth_code ⇒ Object
readonly
Returns the value of attribute auth_code.
-
#bucket ⇒ Object
readonly
Returns the value of attribute bucket.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
-
.check_training(auth_code, bucket, object) ⇒ Object
Wrapper.
-
.get_auth_token(email, password, url = 'https://www.google.com/accounts/ClientLogin', args = {}) ⇒ Object
Gets the auth code from Google’s ClientLogin using the provided email and password.
-
.predict(auth_code, bucket, object, submission) ⇒ Object
Wrapper.
-
.train(auth_code, bucket, object) ⇒ Object
Wrapper.
Instance Method Summary collapse
-
#check_training ⇒ Object
Gets the training status of the specified object.
-
#initialize(auth_code, bucket, object) ⇒ GooglePrediction
constructor
auth_code: the login code generated from self.get_auth_token.
-
#predict(submission) ⇒ Object
Submission must be either a string, a single number, or an array of strings or numbers.
-
#train ⇒ Object
Starts training on the specified object.
Constructor Details
#initialize(auth_code, bucket, object) ⇒ GooglePrediction
auth_code: the login code generated from self.get_auth_token. If you get your auth_code from somewhere else, make sure that it doesn’t include any extra characters like a newline at the end. That can cause bizarre errors.
bucket: the name of the bucket in Google Storage
object: the filename of the object to do prediction on
79 80 81 82 83 |
# File 'lib/google-prediction.rb', line 79 def initialize(auth_code, bucket, object) @auth_code=auth_code @bucket=bucket @object=object end |
Instance Attribute Details
#auth_code ⇒ Object (readonly)
Returns the value of attribute auth_code.
45 46 47 |
# File 'lib/google-prediction.rb', line 45 def auth_code @auth_code end |
#bucket ⇒ Object (readonly)
Returns the value of attribute bucket.
46 47 48 |
# File 'lib/google-prediction.rb', line 46 def bucket @bucket end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
47 48 49 |
# File 'lib/google-prediction.rb', line 47 def object @object end |
Class Method Details
.check_training(auth_code, bucket, object) ⇒ Object
Wrapper. Creates a new object and runs #check_training on it.
167 168 169 170 |
# File 'lib/google-prediction.rb', line 167 def self.check_training(auth_code, bucket, object) predictor = GooglePrediction.new(auth_code, bucket, object) predictor.check_training end |
.get_auth_token(email, password, url = 'https://www.google.com/accounts/ClientLogin', args = {}) ⇒ Object
Gets the auth code from Google’s ClientLogin using the provided email and password.
This will fail if Google requires a Captcha. If so, follow the instructions at
code.google.com/apis/predict/docs/getting-started.html and pass in the new URL and new arguments using the optional parameters.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/google-prediction.rb', line 56 def self.get_auth_token(email, password, url='https://www.google.com/accounts/ClientLogin', args={}) curl = Curl::Easy.new(url) post_args = { "accountType" => "HOSTED_OR_GOOGLE", "Email" => email, "Passwd" => curl.escape(password), "source" => "companyName-applicationName-versionID", "service" => "xapi" } args.each {|key, val| post_args[key] = val } post_fields = post_args.map {|k,v| Curl::PostField.content(k, v) } curl.http_post(post_fields) curl.body_str.match('Auth.*')[0][5..-1] end |
.predict(auth_code, bucket, object, submission) ⇒ Object
Wrapper. Creates a new object and runs #predict on it.
173 174 175 176 |
# File 'lib/google-prediction.rb', line 173 def self.predict(auth_code, bucket, object, submission) predictor = GooglePrediction.new(auth_code, bucket, object) predictor.predict(submission) end |
.train(auth_code, bucket, object) ⇒ Object
Wrapper. Creates a new object and runs #train on it.
161 162 163 164 |
# File 'lib/google-prediction.rb', line 161 def self.train(auth_code, bucket, object) predictor = GooglePrediction.new(auth_code, bucket, object) predictor.train end |
Instance Method Details
#check_training ⇒ Object
Gets the training status of the specified object.
If the training is incomplete, returns “Training has not completed”.
If the training did not have enough data to do cross-fold validation, returns “no estimate available”. If the training went as desired, returns the accuracy of the training from 0 to 1.
Returns
{"errors"=>{"errors"=>[{all of your errors}], "code"=>code, "message"=>}}
on error.
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/google-prediction.rb', line 113 def check_training url = PREDICTION_URL_PREFIX + "/" + @bucket + "%2F" + @object curl = Curl::Easy.new(url) curl.headers = {"Authorization" => "GoogleLogin auth=#{@auth_code}"} curl.http_get # response will be # {"data"=>{"data"=>"bucket/object", "modelinfo"=>accuracy_prediction}} # on success response = JSON.parse(curl.body_str) return response["data"]["modelinfo"] unless response["data"].nil? return response end |
#predict(submission) ⇒ Object
Submission must be either a string, a single number, or an array of strings or numbers
Gets the prediction for the label of the submission based on the training.
Returns the prediction on success and
{"errors"=>{"errors"=>[{all of your errors}], "code"=>code, "message"=>}}
on error.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/google-prediction.rb', line 135 def predict(submission) url = PREDICTION_URL_PREFIX + "/" + @bucket + "%2F" + @object + "/predict" curl = Curl::Easy.new(url) post_body = {:data => {:input => {}}} submission = [submission] unless submission.is_a? Array if submission[0].is_a? String post_body[:data][:input] = {:text => submission} elsif submission[0].is_a? Fixnum post_body[:data][:input] = {:numeric => submission} else raise Exception.new("submission must be String, Fixnum, or Array of Strings / Fixnums") end curl.post_body = JSON.generate(post_body) curl.headers = {"Content-Type" => "application/json", "Authorization" => "GoogleLogin auth=#{@auth_code}"} curl.http("POST") # response will be # {"data"=>{"output"=>{"output_label"=>label}}} # on success response = JSON.parse(curl.body_str) return response["data"]["output"]["output_label"] unless response["data"].nil? return response end |
#train ⇒ Object
Starts training on the specified object.
Returns
{"data"=>{"data"=>"bucket/object"}}
on success, and
{"errors"=>{"errors"=>[{all of your errors}], "code"=>code, "message"=>}}
on error.
92 93 94 95 96 97 98 99 100 |
# File 'lib/google-prediction.rb', line 92 def train url = PREDICTION_URL_PREFIX + "?data=" + @bucket + "%2F" + @object curl = Curl::Easy.new(url) curl.post_body = JSON.generate({:data => {}}) curl.headers = {"Content-Type" => "application/json", "Authorization" => "GoogleLogin auth=#{@auth_code}"} curl.http("POST") JSON.parse(curl.body_str) end |