Class: Amazon::WebServices::MechanicalTurkRequester

Inherits:
Util::ConvenienceWrapper show all
Defined in:
lib/amazon/webservices/mechanical_turk_requester.rb

Constant Summary collapse

WSDL_VERSION =
"2007-06-21"
ABANDONMENT_RATE_QUALIFICATION_TYPE_ID =
"00000000000000000070"
APPROVAL_RATE_QUALIFICATION_TYPE_ID =
"000000000000000000L0"
REJECTION_RATE_QUALIFICATION_TYPE_ID =
"000000000000000000S0"
RETURN_RATE_QUALIFICATION_TYPE_ID =
"000000000000000000E0"
SUBMISSION_RATE_QUALIFICATION_TYPE_ID =
"00000000000000000000"
LOCALE_QUALIFICATION_TYPE_ID =
"00000000000000000071"
DEFAULT_THREADCOUNT =
10

Constants inherited from Util::ConvenienceWrapper

Util::ConvenienceWrapper::REQUIRED_PARAMETERS

Instance Method Summary collapse

Methods inherited from Util::ConvenienceWrapper

#callService, #method_missing, paginate, real_method, serviceCall

Methods included from Util::Logging

#log, #set_log

Constructor Details

#initialize(args = {}) ⇒ MechanicalTurkRequester

Returns a new instance of MechanicalTurkRequester.



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 92

def initialize(args={})
  newargs = args.dup
  unless args[:Config].nil?
    loaded = Amazon::Util::DataReader.load( args[:Config], :YAML )
    newargs = args.merge loaded.inject({}) {|a,b| a[b[0].to_sym] = b[1] ; a }
  end
  @threadcount = args[:ThreadCount].to_i
  @threadcount = DEFAULT_THREADCOUNT unless @threadcount >= 1
  raise "Cannot override WSDL version ( #{WSDL_VERSION} )" unless args[:Version].nil? or args[:Version].equals? WSDL_VERSION
  super newargs.merge( :Name => :AWSMechanicalTurkRequester,
                       :ServiceClass => Amazon::WebServices::MechanicalTurk,
                       :Version => WSDL_VERSION )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Amazon::WebServices::Util::ConvenienceWrapper

Instance Method Details

#availableFundsObject

Returns available funds in USD Calls getAccountBalance and parses out the correct amount



321
322
323
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 321

def availableFunds
  return getAccountBalance[:AvailableBalance][:Amount]
end

#createHITs(hit_template, question_template, hit_data_set) ⇒ Object

Create a series of similar HITs, sharing common parameters. Utilizes HITType

  • hit_template is the array of parameters to pass to createHIT.

  • question_template will be passed as a template into ERB to generate the :Question parameter

  • the RequesterAnnotation parameter of hit_template will also be passed through ERB

  • hit_data_set should consist of an array of hashes defining unique instance variables utilized by question_template



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 111

def createHITs( hit_template, question_template, hit_data_set )
  hit_template = hit_template.dup
  lifetime = hit_template[:LifetimeInSeconds]
  numassignments_template = hit_template[:MaxAssignments]
  annotation_template = hit_template[:RequesterAnnotation]
  hit_template.delete :LifetimeInSeconds
  hit_template.delete :MaxAssignments
  hit_template.delete :RequesterAnnotation

  ht = hit_template[:HITTypeId] || registerHITType( hit_template )[:HITTypeId]

  tp = Amazon::Util::ThreadPool.new @threadcount

  created = [].extend(MonitorMixin)
  failed = [].extend(MonitorMixin)
  hit_data_set.each do |hd|
    tp.addWork(hd) do |hit_data|
      begin
        b = Amazon::Util::Binder.new( hit_data )
        annotation = b.erb_eval( annotation_template )
        numassignments = b.erb_eval( numassignments_template.to_s ).to_i
        question = b.erb_eval( question_template )
        result = self.createHIT( :HITTypeId => ht,
                                 :LifetimeInSeconds => lifetime,
                                 :MaxAssignments => ( hit_data[:MaxAssignments] || numassignments || 1 ),
                                 :Question => question,
                                 :RequesterAnnotation => ( hit_data[:RequesterAnnotation] || annotation || "")
                               )
        created.synchronize do
          created << result
        end
      rescue => e
        failed.synchronize do
          failed << hit_data.merge( :Error => e.message, :Description => e.description )
        end
      end
    end # tp.addWork
  end # hit_data_set.each
  tp.finish

  return :Created => created, :Failed => failed
end

#deleteHITs(hit_ids) ⇒ Object

Deletes given hits from mturk

  • hit_ids is an array of HITIds



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 242

def deleteHITs(hit_ids)
  tp = Amazon::Util::ThreadPool.new @threadcount

  deleted = [].extend(MonitorMixin)
  failed = [].extend(MonitorMixin)
  hit_ids.each do |hid|
    tp.addWork(hid) do |hit_id|
      begin
        disableHIT( :HITId => hit_id )
        deleted.synchronize do
          deleted << hit_id
        end
      rescue => e
        failed.synchronize do
          failed << { :HITId => hit_id, :Error => e.message }
        end
      end
    end # tp.addWork
  end # hit_ids.each
  tp.finish

  return :Deleted => deleted, :Failed => failed
end

#extendHITs(hit_ids, time) ⇒ Object

Adds time for given hits. Useful for resuming paused/expired HITs

  • hit_ids is an array of HITIds

  • time is time in second we want to add



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 216

def extendHITs(hit_ids, time)
  tp = Amazon::Util::ThreadPool.new @threadcount

  extended = [].extend(MonitorMixin)
  failed = [].extend(MonitorMixin)
  hit_ids.each do |hid|
    tp.addWork(hid) do |hit_id|
      begin
        extendHIT( :HITId => hit_id, :ExpirationIncrementInSeconds => time )
        extended.synchronize do
          extended << hit_id
        end
      rescue => e
        failed.synchronize do
          failed << { :HITId => hit_id, :Error => e.message }
        end
      end
    end # tp.addWork
  end # hit_ids.each
  tp.finish

  return :Extended => extended, :Failed => failed
end

#forceExpireHITs(hit_ids) ⇒ Object

Forces expiration of hits provided. Useful for pausing HITs

  • hit_ids is an array of HITIds



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 189

def forceExpireHITs(hit_ids)
  tp = Amazon::Util::ThreadPool.new @threadcount

  expired = [].extend(MonitorMixin)
  failed = [].extend(MonitorMixin)
  hit_ids.each do |hid|
    tp.addWork(hid) do |hit_id|
      begin
        forceExpireHIT( :HITId => hit_id )
        expired.synchronize do
          expired << hit_id
        end
      rescue => e
        failed.synchronize do
          failed << { :HITId => hit_id, :Error => e.message }
        end
      end
    end # tp.addWork
  end # hit_ids.each
  tp.finish

  return :Expired => expired, :Failed => failed
end

#getHITResults(list) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 302

def getHITResults( list )
  results = [].extend(MonitorMixin)
  tp = Amazon::Util::ThreadPool.new @threadcount
  list.each do |line|
    tp.addWork(line) do |h|
      hit = getHIT( :HITId => h[:HITId] )
      getAssignmentsForHITAll( :HITId => h[:HITId] ).each {|assignment|
        results.synchronize do
          results << ( hit.merge( assignment ) )
        end
      }
    end
  end
  tp.finish
  results.flatten
end

#simplifyAnswer(answerXML) ⇒ Object

helper function to simplify answer XML



326
327
328
329
330
331
332
333
334
335
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 326

def simplifyAnswer( answerXML )
  answerHash = Amazon::WebServices::Util::XMLSimplifier.simplify REXML::Document.new(answerXML)
  list = [answerHash[:Answer]].flatten
  list.inject({}) { |list, answer|
    id = answer[:QuestionIdentifier]
    result = answer[:FreeText] || answer[:SelectionIdentifier] || answer[:UploadedFileKey]
    list[id] = result
    list
  }
end

#updateHIT(hit_id, hit_template) ⇒ Object

Update a HIT with new properties.

hit_id

Id of the HIT to update

hit_template

hash ( parameter => value ) of parameters to update

Acceptable attributes:

  • Title

  • Description

  • Keywords

  • Reward

  • QualificationRequirement

  • AutoApprovalDelayInSeconds

  • AssignmentDurationInSeconds

Behind the scenes, this function retrieves the HIT, merges the HITs current attributes with any you specify, and registers a new HIT Template. It then uses the new ChangeHITTypeOfHIT function to move your HIT to the newly-created HIT Template.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 283

def updateHIT( hit_id, hit_template )
  hit_template = hit_template.dup

  hit = getHIT( :HITId => hit_id )

  props = %w( Title Description Keywords Reward QualificationRequirement
              AutoApprovalDelayInSeconds AssignmentDurationInSeconds
            ).collect {|str| str.to_sym }

  props.each do |p|
    hit_template[p] = hit[p] if hit_template[p].nil?
  end

  hit_type_id = registerHITType( hit_template )[:HITTypeId]

  changeHITTypeOfHIT( :HITId => hit_id, :HITTypeId => hit_type_id )
end

#updateHITs(hit_template, hit_ids) ⇒ Object

Update a series of HITs to belong to a new HITType

  • hit_template is the array of parameters to pass to registerHITType

  • hit_ids is a list of HITIds (strings)



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 157

def updateHITs( hit_template, hit_ids )
  hit_template = hit_template.dup
  hit_template.delete :LifetimeInSeconds
  hit_template.delete :RequesterAnnotation

  hit_type_id = registerHITType( hit_template )[:HITTypeId]

  tp = Amazon::Util::ThreadPool.new @threadcount

  updated = [].extend(MonitorMixin)
  failed = [].extend(MonitorMixin)
  hit_ids.each do |hid|
    tp.addWork(hid) do |hit_id|
      begin
        changeHITTypeOfHIT( :HITId => hit_id, :HITTypeId => hit_type_id )
        updated.synchronize do
          updated << hit_id
        end
      rescue => e
        failed.synchronize do
          failed << { :HITId => hit_id, :Error => e.message }
        end
      end
    end # tp.addWork
  end # hit_ids.each
  tp.finish

  return :Updated => updated, :Failed => failed
end