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.



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

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



242
243
244
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 242

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



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

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

#getHITResults(list) ⇒ Object



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 223

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



247
248
249
250
251
252
253
254
255
256
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 247

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.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/amazon/webservices/mechanical_turk_requester.rb', line 204

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)



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

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