Class: Amazon::WebServices::MechanicalTurkRequester

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

Constant Summary collapse

WSDL_VERSION =
"2013-11-15"
ABANDONMENT_RATE_QUALIFICATION_TYPE_ID =

Deprecated qualification type IDs

"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 =

Current qualification type IDs

"00000000000000000071"
TOTAL_NUMBER_OF_HITS_APPROVED_QUALIFICATION_TYPE_ID =
"00000000000000000040"
ADULT_QUALIFICATION_TYPE_ID =
"00000000000000000060"
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"
MASTERS_QUALIFICATION_TYPE_ID =
"2F1QJWKUDD8XADTFD2Q0G6UTO95ALH"
MASTERS_SANDBOX_QUALIFICATION_TYPE_ID =
"2ARFPLSP75KLA8M8DH1HTEQVJT3SY6"
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.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 110

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

Instance Method Details

#availableFundsObject

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



266
267
268
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 266

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 134

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



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

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



271
272
273
274
275
276
277
278
279
280
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 271

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.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 228

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)



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 180

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