Menu

[r91]: / trunk / samples / reviewer / Reviewer.java  Maximize  Restore  History

Download this file

147 lines (116 with data), 5.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
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
/*
* Copyright 2007-2012 Amazon Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://aws.amazon.com/apache2.0
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and
* limitations under the License.
*/
package reviewer;
import java.util.List;
import com.amazonaws.mturk.addon.HITDataCSVReader;
import com.amazonaws.mturk.addon.HITDataCSVWriter;
import com.amazonaws.mturk.addon.HITDataInput;
import com.amazonaws.mturk.addon.HITTypeResults;
import com.amazonaws.mturk.dataschema.QuestionFormAnswers;
import com.amazonaws.mturk.dataschema.QuestionFormAnswersType;
import com.amazonaws.mturk.requester.Assignment;
import com.amazonaws.mturk.requester.AssignmentStatus;
import com.amazonaws.mturk.service.axis.RequesterService;
import com.amazonaws.mturk.util.PropertiesClientConfig;
/**
* The Reviewer sample application will retrieve the completed assignments for a given HIT,
* output the results and approve the assignment.
*
* mturk.properties must be found in the current file path.
* You will need to have the HIT ID of an existing HIT that has been accepted, completed and
* submitted by a worker.
* You will need to have the .success file generated from bulk loading several HITs (i.e. Site Category sample application).
*
* The following concepts are covered:
* - Retrieve results for a HIT
* - Output results for several HITs to a file
* - Approve assignments
*/
public class Reviewer {
private RequesterService service;
/**
* Constructor
*
*/
public Reviewer() {
service = new RequesterService(new PropertiesClientConfig("samples/mturk.properties"));
}
/**
* Prints the submitted results of HITs when provided with a .success file.
* @param successFile The .success file containing the HIT ID and HIT Type ID
* @param outputFile The output file to write the submitted results to
*/
public void printResults(String successFile, String outputFile) {
try {
//Loads the .success file containing the HIT IDs and HIT Type IDs of HITs to be retrieved.
HITDataInput success = new HITDataCSVReader(successFile);
//Retrieves the submitted results of the specified HITs from Mechanical Turk
HITTypeResults results = service.getHITTypeResults(success);
results.setHITDataOutput(new HITDataCSVWriter(outputFile));
//Writes the submitted results to the defined output file.
//The output file is a tab delimited file containing all relevant details
//of the HIT and assignments. The submitted results are included as the last set of fields
//and are represented as tab separated question/answer pairs
results.writeResults();
System.out.println("Results have been written to: " + outputFile);
} catch (Exception e) {
System.err.println("ERROR: Could not print results: " + e.getLocalizedMessage());
}
}
@SuppressWarnings("unchecked")
/**
* Prints the submitted results of a HIT when provided with a HIT ID.
* @param hitId The HIT ID of the HIT to be retrieved.
*/
public void reviewAnswers(String hitId) {
Assignment[] assignments = service.getAllAssignmentsForHIT(hitId);
System.out.println("--[Reviewing HITs]----------");
System.out.println(" HIT Id: " + hitId);
for (Assignment assignment : assignments) {
//Only assignments that have been submitted will contain answer data
if (assignment.getAssignmentStatus() == AssignmentStatus.Submitted) {
//By default, answers are specified in XML
String answerXML = assignment.getAnswer();
//Calling a convenience method that will parse the answer XML and extract out the question/answer pairs.
QuestionFormAnswers qfa = RequesterService.parseAnswers(answerXML);
List<QuestionFormAnswersType.AnswerType> answers =
(List<QuestionFormAnswersType.AnswerType>) qfa.getAnswer();
for (QuestionFormAnswersType.AnswerType answer : answers) {
String assignmentId = assignment.getAssignmentId();
String answerValue = RequesterService.getAnswerValue(assignmentId, answer);
if (answerValue != null) {
System.out.println("Got an answer \"" + answerValue
+ "\" from worker " + assignment.getWorkerId() + ".");
}
}
//Approving the assignment.
service.approveAssignment(assignment.getAssignmentId(), "Well Done!");
System.out.println("Approved.");
}
}
System.out.println("--[End Reviewing HITs]----------");
}
/**
* @param args
*/
public static void main(String[] args) {
Reviewer app = new Reviewer();
if (args.length == 1 && !args[0].equals("")) {
app.reviewAnswers(args[0]);
} else if (args.length == 2 && !args[0].equals("") && !args[1].equals("")) {
app.printResults(args[0], args[1]);
}
}
}