Skip to content

Instantly share code, notes, and snippets.

@j2labs
Created December 14, 2010 10:54
Show Gist options
  • Save j2labs/740267 to your computer and use it in GitHub Desktop.
Save j2labs/740267 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
### Requires latest boto (cuz I checked in code to boto a moment ago)
import uuid
from boto.mturk.connection import MTurkConnection
from boto.mturk.question import Question, QuestionForm, QuestionContent
from boto.mturk.question import AnswerSpecification, FreeTextAnswer
from boto.mturk.question import Overview
#from boto.mturk.qualification import Qualifications # these exist!
#from boto.mturk.qualification import Requirement # these too
#####################
# Connect to Amazon #
#####################
aws_access_key_id = ''
aws_secret_access_key = ''
aws_host = 'mechanicalturk.sandbox.amazonaws.com'
#aws_host = 'mechanicalturk.amazonaws.com'
mtc = MTurkConnection(aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
host=aws_host)
###################################
# Start building the hit template #
###################################
# Overview design
#
# The overview is used for question oriented text and can be interwoven with
# QuestionContent's inside a QuestionForm.
#
# For this demo, I use only one Overview at the top of the QuestionForm and
# then use five QuestionContents below to host input boxes.
overview_title = 'List your top 5 fav things'
overview_content = """<![CDATA[
<p>Please list your top five favorite things in the box
below. It would help our research if you kept the names general. For example,
instead of saying \"playing shortstop\", please just say, \"baseball\".</p>
<p>Thanks, James and the boto crew</p>
]]>"""
overview = Overview()
overview.append_field('Title', overview_title)
overview.append_field('FormattedContent', overview_content)
# Question design
#
# Questions can each be designed uniquely. For this demo I only build enough
# of a QuestionContent to host an input box by using a FreeTextAnswer. There
# are multiple options for how a QuestionContent is constructed.
#
# I recommend reading: http://docs.amazonwebservices.com/AWSMechanicalTurkRequester/2007-06-21/ApiReference_QuestionFormDataStructureArticle.html
#
# Quickly, the available content types are: Title, Text, List, Binary,
# Application, EmbeddedBinary,
# FormattedContent
question_list = []
for i in xrange(5):
qc = QuestionContent()
qc.append_field('Text', "Fav #%s:" % i)
fta = FreeTextAnswer()
ansp = AnswerSpecification(fta)
q = Question(identifier=str(uuid.uuid4()),
content=qc,
answer_spec=ansp)
question_list.append(q)
# QuestionForm design
#
# A QuestionForm is a container for what the HIT task is shaped like. In this
# example, we build one like:
#
# <QuestionForm>
# <Overview></Overview>
# <QuestionContent></QuestionContent>
# ...,
# <QuestionContent></QuestionContent>
# </QuestionForm>
question_form_title=overview_title # reuse it
question_form_description='Help us by listing your five favorite things'
question_form_reward=0.2
qf = QuestionForm()
qf.append(overview)
for q in question_list:
qf.append(q)
# Assignments is an interesting concept in the mechanical turk API. You can have
# multiple instances of a hit's design by assigning it multiple times.
#
# Many users, however, work through the web interface and seem to create many
# copies of the same HIT instead. Be mindful of the way assignments are used.
max_assignments = 1
# create funciton to make a hit using our arguments
make_demo_hit = lambda: mtc.create_hit(question=qf,
max_assignments=max_assignments,
title=question_form_title,
description=question_form_description,
reward=question_form_reward)
hit_set = make_demo_hit()
@garnaat
Copy link

garnaat commented Dec 14, 2010

This looks good. I noticed that in line 61 you are including a URL to the AWS docs and those are notoriously long and ugly. I hate including them because they screw up my formatting so I've started replacing those with short URL's generated by Google's URL shortener. Looks much nicer, and you can tell if anyone is actually reading your docstrings 8^)

@j2labs
Copy link
Author

j2labs commented Dec 17, 2010

Good idea, Mitch. Will look at that this weekend as I beef up the gist into a full workflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment