Class: Amazon::WebServices::MechanicalTurkRequester

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

Constant Summary collapse

WSDL_VERSION =
"2012-03-25"
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.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 99

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

#availableFunds ⇒ Object

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



255
256
257
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 255

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


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
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 123

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



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 236

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



260
261
262
263
264
265
266
267
268
269
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 260

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.



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

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)


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
195
196
197
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 169

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