Class: Amazon::WebServices::MechanicalTurkRequester

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

Constant Summary collapse

WSDL_VERSION =
"2014-08-15"
LOCALE_QUALIFICATION_TYPE_ID =

Current qualification type IDs

"00000000000000000071"
TOTAL_NUMBER_OF_HITS_APPROVED_QUALIFICATION_TYPE_ID =
"00000000000000000040"
ADULT_QUALIFICATION_TYPE_ID =
"00000000000000000060"
MASTERS_QUALIFICATION_TYPE_ID =
"2F1QJWKUDD8XADTFD2Q0G6UTO95ALH"
MASTERS_SANDBOX_QUALIFICATION_TYPE_ID =
"2ARFPLSP75KLA8M8DH1HTEQVJT3SY6"
DEFAULT_THREADCOUNT =
10

Constants inherited from Util::ConvenienceWrapper

Util::ConvenienceWrapper::DEFAULT_UNIQUE_REQUEST_TOKEN_FACTORY, Util::ConvenienceWrapper::REQUIRED_PARAMETERS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Util::ConvenienceWrapper

#callService, idempotent, #method_missing, paginate, real_method, serviceCall

Methods included from Util::Logging

deprecated, #log, #set_log

Constructor Details

#initialize(args = {}) ⇒ MechanicalTurkRequester

Returns a new instance of MechanicalTurkRequester.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 129

def initialize(args={})
  newargs = args.dup
  unless args[:Config].nil?
    if args[:Config] == :Rails
      rails_config = Amazon::Util::DataReader.load( File.join(::RAILS_ROOT,'config','mturk.yml'), :YAML )
      newargs = args.merge rails_config[::RAILS_ENV].inject({})  {|a,b| a[b[0].to_sym] = b[1] ; a }
    else
      loaded = Amazon::Util::DataReader.load( args[:Config], :YAML )
      newargs = args.merge loaded.inject({}) {|a,b| a[b[0].to_sym] = b[1] ; a }
    end
  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

Class Method Details

.const_missing(name) ⇒ Object

Deprecated qualification type IDs



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 19

def self.const_missing(name)
  deprecated = {
    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",
    PHOTO_MODERATION_MASTERS_QUALIFICATION_TYPE_ID: "21VZU98JHSTLZ5BPP4A9NOBJEK3DPG",
    PHOTO_MODERATION_MASTERS_SANDBOX_QUALIFICATION_TYPE_ID: "2TGBB6BFMFFOM08IBMAFGGESC1UWJX",
    CATEGORIZATION_MASTERS_QUALIFICATION_TYPE_ID: "2NDP2L92HECWY8NS8H3CK0CP5L9GHO",
    CATEGORIZATION_MASTERS_SANDBOX_QUALIFICATION_TYPE_ID: "2F1KVCNHMVHV8E9PBUB2A4J79LU20F",
  }
  replacements = {
    PHOTO_MODERATION_MASTERS_QUALIFICATION_TYPE_ID: :MASTERS_QUALIFICATION_TYPE_ID,
    PHOTO_MODERATION_MASTERS_SANDBOX_QUALIFICATION_TYPE_ID: :MASTERS_SANDBOX_QUALIFICATION_TYPE_ID,
    CATEGORIZATION_MASTERS_QUALIFICATION_TYPE_ID: :MASTERS_QUALIFICATION_TYPE_ID,
    CATEGORIZATION_MASTERS_SANDBOX_QUALIFICATION_TYPE_ID: :MASTERS_SANDBOX_QUALIFICATION_TYPE_ID,
  }
  if deprecated.has_key?(name)
    Amazon::Util::Logging.deprecated "#{self}::#{name}", alt: replacements[name], from: Kernel.caller.first
    deprecated[name]
  else
    super
  end
end

Instance Method Details

#availableFundsObject

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



285
286
287
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 285

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



153
154
155
156
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
186
187
188
189
190
191
192
193
194
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 153

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 = annotation_template.nil? ? nil : b.erb_eval( annotation_template )
        numassignments = numassignments_template.nil? ? nil : 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 )
        end
      end
    end # tp.addWork
  end # hit_data_set.each
  tp.finish

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

#getHITResults(list) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 266

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



290
291
292
293
294
295
296
297
298
299
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 290

def simplifyAnswer( answerXML )
  answerHash = Amazon::WebServices::Util::XMLSimplifier.simplify REXML::Document.new(answerXML)
  list = [answerHash[:Answer]].flatten
  list.inject({}) { |answers, answer|
    id = answer[:QuestionIdentifier]
    result = answer[:FreeText] || answer[:SelectionIdentifier] || answer[:UploadedFileKey]
    answers[id] = result
    answers
  }
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.



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

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)



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 199

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