@SuppressWarnings("unchecked") public void testParseAnswers() throws ServiceException { QuestionFormAnswers qfa = RequesterService.parseAnswers(DEFAULT_ANSWER_XML); List<QuestionFormAnswersType.AnswerType> answers = (List<QuestionFormAnswersType.AnswerType>) qfa.getAnswer(); for (int i=0; i< answers.size(); i++) { QuestionFormAnswersType.AnswerType answer = answers.get(i); assertNotNull(null, RequesterService.getAnswerValue(null, answer)); // check order String result = RequesterService.getAnswerValue("TEST_ASSIGNMENT_ID", answer, true); if (i == 0) { assertTrue(result.equals("freeText\tfreeText_answer")); } else if (i == 1) { assertTrue(result.equals("otherSelection\totherSelection_answer")); } else if (i == 2) { assertTrue(result.startsWith("url\t")); } else { assertTrue(result.equals("selectionIdentifier\tselection_answer|selection_answer2")); } } }
@SuppressWarnings("unchecked") private String getAnswers(Assignment assignment) { String result = EMPTY; AssignmentStatus status = assignment.getAssignmentStatus(); if (status == null) { return NO_ANSWER; } String answerXML = assignment.getAnswer(); 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, true); if (answerValue != null) { result += answerValue + DELIMITER; } } return result; }
public static void main(String[] args) { QuestionFormAnswers qfa = RequesterService.parseAnswers(DEFAULT_ANSWER_XML); List<QuestionFormAnswersType.AnswerType> answers = (List<QuestionFormAnswersType.AnswerType>) qfa.getAnswer(); TestRunner.run(TestRequesterService.class); }
/** * Extracts the answer values from the given AnswerType object. When the answer type is * Selections, this method returns the selections separated by the pipe character. When the answer * type is UploadedFileKey, this method returns the S3 file key followed by the file's size in bytes. * * @param assignmentId The assignment for which the answers are extracted. If null, * the upload URL might be invalid. * @param answer an AnswerType structure * @param includeQuestionId specifies whether to prepend the answer with the associated * QuestionIdentifier and a tab * @return a String representation of the answer */ public static String getAnswerValue(String assignmentId, QuestionFormAnswersType.AnswerType answer, boolean includeQuestionId) { String result = includeQuestionId ? result = answer.getQuestionIdentifier() + "\t" : ""; String val = ""; if (answer.getFreeText() != null) { val = answer.getFreeText(); } else if (answer.getOtherSelectionText() != null) { val = answer.getOtherSelectionText(); } else if (answer.getSelectionIdentifier() != null && answer.getUploadedFileKey() == null) { Iterator iter = answer.getSelectionIdentifier().iterator(); while (iter.hasNext()) { val += iter.next() + HITResults.MULTI_ANSWER_DELIMITER; } if (val.length() > 0) { val = val.substring(0, val.length() - 1); } } else { try { String url = "http://requester.mturk.com/mturk/downloadAnswer?assignmentId=" + assignmentId + "&questionId=" + URLEncoder.encode(answer.getQuestionIdentifier(), "UTF-8"); result += url; } catch (UnsupportedEncodingException e) { result += answer.getUploadedFileKey() + HITResults.MULTI_ANSWER_DELIMITER + answer.getUploadedFileSizeInBytes(); } } if (val.length()==0) { result += HITResults.EMPTY_ANSWER; // Feature 1816806 (missing columns when value is NULL) } else { result += val; } return result; }
@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]----------"); }
@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(ReviewHIT hit) { Assignment[] assignments = service.getAllAssignmentsForHIT(hit.id); System.out.println("--[Reviewing HITs]----------"); System.out.println("author:review=" + hit.author + ":" + hit.review); System.out.println(" HIT Id: " + hit.id); for (Assignment assignment : assignments) { String path = Utils.getTranslatedFixedFilename(filenames.get(hit.author), hit.review); System.out.println(path.substring(path.lastIndexOf('/')+1)); //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) { if (answerValue.equals("emptyanswer") || answerValue.equals("fair") || answerValue.equals("nice") || answerValue.equals("poor")) { // service.rejectAssignment(assignmentId, ""); System.out.println("BAD ANSWER"); } else { // service.rejectAssignment(assignmentId, ""); System.out.println("Got an answer \"" + answerValue + "\" from worker " + assignment.getWorkerId() + "."); size += 1; } } // ReviewWriter.writeTranslatedFixedReview(answerValue, hit.author, hit.review); /** Reject bad workers here. */ // if (false) { // System.out.println("BAD WORKER"); // service.rejectAssignment(assignmentId, "Do not follow the submission instructions: Submit a result(explained below) followed by the explanation of differences."); // } } // Approving the assignment. // service.approveAssignment(assignment.getAssignmentId(), "Well Done!"); // System.out.println("Approved."); // service.disposeHIT(hit.id); // service.disableHIT(hit.id); // } } System.out.println("--[End Reviewing HITs]----------"); }
/** * Extracts the answer values from the given AnswerType object. When the answer type is * Selections, this method returns the selections separated by the pipe character. When the answer * type is UploadedFileKey, this method returns the S3 file key followed by the file's size in bytes. * * @param assignmentId The assignment for which the asnwers are extracted. If null, the upload * URL might be invalid. * @param answer the type of the answer * @return a String representation of the answer * @throws ServiceException */ public static String getAnswerValue(String assignmentId, QuestionFormAnswersType.AnswerType answer) { return getAnswerValue(assignmentId, answer, false); }