Skip to content

Commit

Permalink
various tweaks as I test against sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
subelsky committed Dec 4, 2011
1 parent e8ff81f commit da61d91
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Gemfile
@@ -1,4 +1,4 @@
source "http://rubygems.org"

gem "ruby-aws"
gem "nokogiri"
gem "highline"
2 changes: 1 addition & 1 deletion Gemfile.lock
Expand Up @@ -11,5 +11,5 @@ PLATFORMS
ruby

DEPENDENCIES
nokogiri
highline
ruby-aws
84 changes: 32 additions & 52 deletions bin/run
@@ -1,43 +1,19 @@
#!/usr/bin/env ruby
# adapted from ruby-aws sample code
# adapted from ruby-aws gem's sample code

require "pp"
require_relative "../lib/init"
require 'amazon/webservices/mturk/question_generator'
include Amazon::WebServices::MTurk
require "highline/import"

PREAMBLE = '<?xml version="1.0" encoding="UTF-8"?>'+"\n"+'<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">' + "\n"
PREAMBLE = '<?xml version="1.0" encoding="UTF-8"?>'+"\n"
PREAMBLE += '<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">' + "\n"
TAIL = '</QuestionForm>'

def create_question(sentence_num,story)
paragraphs = story.split(/\n/)
paragraphs = story.split(/\n+/)

components = [PREAMBLE]
#components << <<-QUESTION
#<Question>
#<QuestionIdentifier>StorySentence#{sentence_num}</QuestionIdentifier>
#<IsRequired>true</IsRequired>
#<QuestionContent>
#<Title>Add one complete sentence to the following story:</Title>
#<List>
#<ListItem>You must write a complete sentence that makes logical sense in the context of the story.</ListItem>
#<ListItem>Write the first sentence that comes to mind.</ListItem>
#<ListItem>Do not copy the sentence from another source.</ListItem>
#<ListItem>Type an asterisk character (*) if you would like to start a new paragraph.</ListItem>
#</List>
#{paragraphs.map { |p| "<Text>#{p}</Text>" }.join("\n") }
#</QuestionContent>
#<AnswerSpecification>
#<FreeTextAnswer>
#<NumberOfLinesSuggestion>2></NumberOfLinesSuggestion>
#<Constraints>
#<Length minLength="3" />
#<AnswerFormatRegex regex="\S" errorText="The content cannot be blank."/>
#</Constraints>
#</FreeTextAnswer>
#</AnswerSpecification>
#</Question>
#QUESTION

components << <<-QUESTION
<Question>
Expand All @@ -49,7 +25,7 @@ def create_question(sentence_num,story)
<ListItem>You must write a complete sentence that makes logical sense in the context of the story.</ListItem>
<ListItem>Write the first sentence that comes to mind.</ListItem>
<ListItem>Do not copy the sentence from another source.</ListItem>
<ListItem>Type an asterisk character (*) if you would like to start a new paragraph.</ListItem>
<ListItem>Begin with a asterisk character (*) if you would like to start a new paragraph.</ListItem>
</List>
#{paragraphs.map { |p| "<Text>#{p}</Text>" }.join("\n") }
</QuestionContent>
Expand All @@ -65,13 +41,6 @@ def create_question(sentence_num,story)
components.join
end

# Check to see if your account has sufficient funds
def hasEnoughFunds?
available = mturk.availableFunds
puts "Got account balance: %.2f" % available
return available > 0.055
end

# These sentences were generated by a previous mechanical turk job
OPENING_SENTENCES = [
"Jim approached the open door cautiously, unsure of the source of noise he had heard just moments prior.",
Expand All @@ -90,8 +59,7 @@ OPENING_SENTENCES = [
"The old woman turned and smiled."
]

# these are the sentences that were added by mechanical turk workers while
# testing this script.
# these are the sentences that were added by mechanical turk workers while testing this script.

story = [
"A lump rose in Andy's throat and he brushed away tears as he reread the chilling message taped to his dorm room door.",
Expand All @@ -101,25 +69,23 @@ story = [
options = {
AWSAccessKeyId: ENV['AWS_ACCESS_KEY_ID'],
AWSAccessKey: ENV['AWS_ACCESS_KEY'],
Host: "Prod"
Host: ENV['PRODUCTION'] == "true" ? "Prod" : "Sandbox"
}

mturk = Amazon::WebServices::MechanicalTurkRequester.new(options)

1.times do |num|
25.times do |num|
question = create_question(num,story)
puts question

begin
result = mturk.createHIT(:Title => "Write one sentence in a short fiction story",
:Description => "Use your imagination to contribute one complete sentence to a short fictional story written by Mechanical Turk authors, one line at a time.",
:MaxAssignments => 1,
:Reward => { :Amount => 0.15, :CurrencyCode => 'USD' },
:Reward => { :Amount => 0.10, :CurrencyCode => 'USD' },
:Question => question,
:Keywords => "writing, content, creation, fiction, fun")
:Keywords => "writing, content, creation, fiction, fun, easy, fast")
rescue StandardError => e
pp e
exit
fail e.to_s
end

hit_id = result[:HITId]
Expand All @@ -134,13 +100,25 @@ mturk = Amazon::WebServices::MechanicalTurkRequester.new(options)

loop do
result = mturk.getHITResults([{ HITId: hit_id }])

if result.any?
answer = result.first[:Answer]
doc = Nokogiri::XML.parse(answer)
sentence = doc.at_css("FreeText").text
sentence.gsub!(/\*/,"\n")
story << sentence
puts "Received result: #{sentence}"
result = result.first
answer = result[:Answer]
sentence = mturk.simplifyAnswer(answer).values.first.strip
notice = "Received result: \"#{sentence}\""
puts notice
`growlnotify -m #{notice}`

if agree("Accept this sentence? (y/n)")
sentence.gsub!(/\*/,"\n\n")
story += " " + sentence
mturk.approveAssignment(AssignmentId: result[:AssignmentId], RequesterFeedback: "Well Done!")
puts "The story so far:\n\n#{story}"
else
puts "Rejecting this sentence"
mturk.rejectAssignment(AssignmentId: result[:AssignmentId])
end

break
else
puts "No result yet..."
Expand All @@ -150,6 +128,8 @@ mturk = Amazon::WebServices::MechanicalTurkRequester.new(options)
end


# Example response from getHITResults:
#
#[{:Request=>{:IsValid=>"True"},
#:HITId=>"26XE4EGDHDM268HOYVQWDHEZ65RBLM",
#:HITTypeId=>"29P6NG1FS3NYO6K4496ZZZL2XRIZLY",
Expand Down
2 changes: 1 addition & 1 deletion lib/init.rb
@@ -1,5 +1,5 @@
require "rubygems"
require "bundler"
require "ruby-aws"
Bundler.setup(:default)
require "ruby-aws"
$:.unshift(File.dirname(__FILE__))

0 comments on commit da61d91

Please sign in to comment.