Turkee Description

Seamlessly convert your Rails forms for use on Mechanical Turk. Then, easily import the data posted by the Mechanical Turk workers back into your data models.

External forms are created using a simple form helper. HITs are created by issuing a rake command. Retrieving submitted response data and importing that data into your model(s) requires just one more rake command.

Rails 2

I’m no longer supporting Rails 2.x. If you have Rails 2 changes, message me and we can look to maintaing a Rails 2.x branch.

Mechanical Turk API Changes

Mechanical Turk is now requiring that the hitId, workerId, and the turkSubmitTo parameters be passed in along with the assignmentId and form parameters.

What does this mean for you? Not much besides the fact that now when you construct your forms using turkee_form_for, you’ll be passing in your entire params hash instead of just the assignment_id. The code snippet below reflect this change.

Install

Add turkee to your Gemfile as a gem dependency, then do a ‘bundle install’:

gem 'turkee'

To access the Turkee rake tasks, add the following to your application’s Rakefile:

require 'tasks/turkee'

Run the turkee generator from your application directory to copy the needed migrations and config/initializer into your application:

rails g turkee

If you haven’t created a Mechanical Turk account, surf on over to Amazon’s Web Services and create an AWS account.

Once you have your account created, you can access your AWS access key and secret access key from here.

Configuration

Inside the config/initializers directory, you’ll see the file turkee.rb. Edit that file with your Amazon credenti.

AWSACCESSKEYID      = 'XXXXXXXXXXXXXXXXXX'
AWSSECRETACCESSKEY  = 'YYYYYYYYYYYYYYYYYYYYYYYYYYYY'

RTurk::logger.level     = Logger::DEBUG
RTurk.setup(AWSACCESSKEYID, AWSSECRETACCESSKEY, :sandbox => (Rails.env == 'production' ? false : true))

Use

1) Run your migrations :

rake db:migrate

2) You should disable form controls if the Turker hasn’t accepted the HIT. You can determine this from your controller:

class SurveysController < ApplicationController

    def new
        @disabled      = Turkee::TurkeeFormHelper::disable_form_fields?(params)

        # If you wanted to find the associated turkee_task, you could do a find by hitId
        #  Not necessary in a simple example.
        # @turkee_task   = Turkee::TurkeeTask.find_by_hit_id(params[:hitId]).id rescue nil

        ...
        @survey        = Survey.new
    end

3) Change your forms to use the form helper.

<% turkee_form_for(@survey, params) do |f| %>
    <p><%= f.text_area :value, :disabled => @disabled %></p>
    <p><%= f.submit 'Create', :disabled => @disabled %></p>
<% end %>

Using the turkee_form_for helper will post the form to the Mechanical Turk sandbox if you’re in development/test mode, otherwise it will be posted to Mechanical Turk production/live site.

4) Run the following rake task to post to Mechanical Turk :

# Host URL of your application
# Title of your HIT
# Description of your HIT
# Model name of your task form (the New action should be implemented)
# Number of assignments for HIT
# The reward for a successful completion
# The lifetime of the HIT in days (e.g. 5 days)

rake turkee:post_hit[<Host>, <Title>, <Description>, <Model>, <Number of Assignments>, <Reward>, <Lifetime>]

e.g. :
rake turkee:post_hit["https://www.yourapp.com","Please complete our survey","Tell us your favorite color.","Survey",100,0.05,2]
** Do not put spaces before or after commas for the rake task parameters

This will insert a row for the requested HIT into the turkee_tasks table. The turkee_tasks entry stores (along with the other properties) the HIT URL (e.g. workersandbox.mturk.com/mturk/preview?groupId=1HGHJJGHQSJB7WMWJ33YS8WM169XNIL ) and HIT ID (e.g. 1J1EXO8SUQ3URTTUYGHJ7EKUT11 ). These values are returned from Mechanical Turk when the HIT request is posted.

When a Turk worker views your HIT, the HIT will display your form within an iFrame. With the above example, Mechanical Turk will open an iFrame for the HIT assignment displaying the form www.yourapp.com/surveys/new

** The application that hosts your external forms preferably should have an https interface (you’re going to have to buy an SSL certificate). If the forms are hosted on an unsecured host (http), because Mechanical Turk defaults to https, you’re going to receive the ugly popup from IE regarding “mixed content” (msdn.microsoft.com/en-us/library/ee264315%28v=vs.85%29.aspx).

5) Allow some time for the Mechanical Turk workers (“Turkers”) to respond to your HIT.

6) Run the rake task that retrieves the values from Mechanical Turk and stores the user entered values into your model.

rake turkee::get_all_results

Rerun this task periodically to retrieve newly entered form values. You can setup this task as a cronjob to automate this.

crontab -e

# E.g. run every five minutes
*/5 * * * * cd /var/www/your/turk/application && rake turkee:get_all_results

Or you can directly call :

Turkee::TurkeeTask.process_hits

7) When a response is retrieved from Mechanical Turk, Turkee attempts to create a data row for the model specified using the corresponding retrieved data. If the row cannot be created (input failed model validations), the assignment is rejected. As for Mechanical Turk approval, if the row is created and you haven’t specified your own custom approve? method for the model, the assignment will automatically be approved. If you’d like to add your own custom approval method, add the approve? instance method to your model. E.g. :

class Survey < ActiveRecord::Base
  def approve?
    (!response.blank? && !comment.blank?)
  end
end

Copyright © 2010 - 2011 Jim Jones. See LICENSE for details.