MechanicalTurk/Command/RetrieveResults.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 150 CC 18 SCAR 38.7
CC
18
Cov
0%
retrieveResults
110.0
progressBlock
20.0
createOutputBlock
20.0
CRAP 47.9
1 package Net::Amazon::MechanicalTurk::Command::RetrieveResults;
2 use strict;
3 use warnings;
4 use Carp;
5 use Net::Amazon::MechanicalTurk::RowData;
6 use Net::Amazon::MechanicalTurk::DelimitedWriter;
7  
8 our $VERSION = '1.00';
9  
10 =head1 NAME
11  
12 Net::Amazon::MechanicalTurk::Command::RetrieveResults - Retrieves answer data.
13  
14 =head1 SYNOPSIS
15  
16 $mturk->retrieveResults(
17 input => "loadhits-success.csv",
18 output => "loadhits-results.csv",
19 progress => \*STDOUT
20 );
21  
22 =head1 C<retrieveResults>
23  
24 retrieveResults
25  
26 Retrieves the available answers for each HITId in input and stores the answers
27 in output.
28  
29 input - The input parameter is something which should produce rows of data
30 (hashes) with at least one column named HITId. The case does not
31 matter, however all rows should use the same column name. Row 1
32 should not have hitid, if row 2 has HITId. This parameter may be
33 of the following types:
34
35 - Net::Amazon::MechanicalTurk::RowData
36 - An array of hashes.
37 (This is internally converted into an object of type:
38 Net::Amazon::MechanicalTurk::RowData::ArrayHashRowData)
39 - A reference to a subroutine. When the retrieveResults
40 method asks for row data, the subroutine will be called and
41 passed a subroutine reference, which should be called
42 for every row generated by the input. The generated row
43 should be a hash reference.
44 (This is internally converted into an object of type
45 Net::Amazon::MechanicalTurk::RowData::SubroutineRowData)
46 - The name of a file. The file should be either a CSV or
47 tab delimited file. If the file name ends with '.csv',
48 it will read as a CSV, otherwise it is assumed to be
49 tab delimited. The first row in the file should contain
50 the column names. Each subsequence row becomes a hash
51 reference based on the column names.
52 (This is internally converted into an object of type
53 Net::Amazon::MechanicalTurk::RowData::DelimitedRowData)
54
55 output - Used to write answer information back to some form of data
56 storage. This parameter may be of the following types:
57 - A filename. The answers for an assignment will be
58 written to the file name as a delimited file. It
59 will be a CSV file if the file name ends with .csv,
60 otherwise it will be a tab delimited file.
61 The file will have the following named columns:
62
63 AssignmentId
64 AssignmentStatus
65 HITId
66 WorkerId
67
68 There will be unnamed columns after those. These columns will
69 come in pairs. A pair will have a question id and then
70 a textual form of the answer, for that question.
71
72 - A subroutine. The subroutine will be called back with
73 a hash containing the following values:
74 - mturk - A handle to the mturk client.
75 - HITId - The HITId for the assignment.
76 - row - The row from the input.
77 - assignment - An assignment object as returned by
78 GetAssignmentsForHITAll.
79  
80 =cut
81  
82 sub retrieveResults {
83 my $mturk = shift;
84 my %params = @_;
85
86 foreach my $required (qw{ input output }) {
87 if (!exists $params{$required}) {
88 Carp::croak("Missing required parameter $required.");
89 }
90 }
91  
92 my $progress = progressBlock($params{progress});
93 my $input = Net::Amazon::MechanicalTurk::RowData->toRowData($params{input});
94 my $output = outputBlock($params{output});
95 my $start = time();
96
97 if ($progress) {
98 $progress->("--[Initializing] " . scalar localtime() . " ---");
99 $progress->(" URL: " . $mturk->serviceUrl);
100 $progress->(" Input: $params{input}");
101 $progress->(" Output: $params{output}");
102 }
103
104 my $hitIdKey;
105 my $hits = 0;
106 my $results = 0;
107 my $rowno = 0;
108
109 $progress->("--[Retrieving results] " . scalar localtime() . " ---") if ($progress);
110
111 $input->each(sub {
112 my ($_self, $row) = @_;
113
114 # Need to find the column name of the HITId key
115 # The loop is for case insensitive matching and ignoring white space.
116 if (!defined $hitIdKey) {
117 while (my ($key,$value) = each %$row) {
118 if ($key =~ /^\s*HITId\s*$/i) {
119 $hitIdKey = $key;
120 last;
121 }
122 }
123 if (!defined $hitIdKey) {
124 Carp::croak("Couldn't find HITId column. Row has the following columns: " .
125 join(", ", sort keys (%$row)));
126 }
127 }
128
129 $rowno++;
130 if (!exists $row->{$hitIdKey}) {
131 Carp::croak("Couldn't find HITId column on row $rowno.");
132 }
133 my $hitId = $row->{$hitIdKey};
134 $hitId =~ s/^\s+//;
135 $hitId =~ s/\s+$//;
136  
137 $progress->(" Getting assignments for HIT $hitId.") if $progress;
138 my $assignments = $mturk->GetAssignmentsForHITAll( HITId => $hitId );
139 while (my $assignment = $assignments->next) {
140 $output->(
141 HITId => $hitId,
142 mturk => $mturk,
143 row => $row,
144 assignment => $assignment
145 );
146 $results++;
147 }
148
149 $hits++;
150 });
151
152 if ($progress) {
153 $progress->(" Examined $hits hits.");
154 $progress->(" Downloaded $results answers.");
155 $progress->("--[Done retrieving results] " . scalar localtime() . " ---");
156 $progress->(" Total load time: " . (time() - $start) . " seconds.");
157 }
158 }
159  
160 sub progressBlock {
161 my ($progress) = @_;
162 if (!defined($progress)) {
163 return $progress;
164 }
165 elsif (UNIVERSAL::isa($progress, "CODE")) {
166 return $progress;
167 }
168 elsif (UNIVERSAL::isa($progress, "GLOB")) {
169 return sub {
170 print $progress @_, "\n";
171 };
172 }
173 else {
174 Carp::croak("The progress parameters should be an IO handle or a subroutine.");
175 }
176 }
177  
178 sub outputBlock {
179 my ($output) = @_;
180 if (UNIVERSAL::isa($output, "CODE")) {
181 return $output;
182 }
183 else {
184 return createOutputBlock($output);
185 }
186 }
187  
188 sub createOutputBlock {
189 my ($file) = @_;
190 my $out;
191 my $rowNumber = 0;
192 if (UNIVERSAL::isa($file, "GLOB")) {
193 $out = Net::Amazon::MechanicalTurk::DelimitedWriter->new(
194 output => $file,
195 fieldSeparator => "\t"
196 );
197 }
198 elsif (UNIVERSAL::isa($file, "Net::Amazon::MechanicalTurk::DelimitedWriter")) {
199 $out = $file;
200 }
201 else {
202 my $fs = ($file =~ /\.csv$/i) ? "," : ".";
203 $out = Net::Amazon::MechanicalTurk::DelimitedWriter->new(
204 lazy => 1,
205 file => $file,
206 fieldSeparator => $fs,
207 autoflush => 1
208 );
209 }
210 my $row = 0;
211 return sub { return writeOutput(out => $out, rowNumber => ++$row, @_); };
212 }
213  
214 sub writeOutput {
215 my %params = @_;
216
217 my $out = $params{out};
218 my $rowNumber = $params{rowNumber};
219 my $mturk = $params{mturk};
220 my $assignment = $params{assignment};
221
222 if ($rowNumber == 1) {
223 $out->write(qw{ AssignmentId AssignmentStatus HITId WorkerId Answers });
224 }
225
226 my $answers = $mturk->parseAssignmentAnswer($assignment);
227 my @row = (
228 $assignment->{AssignmentId}[0],
229 $assignment->{AssignmentStatus}[0],
230 $assignment->{HITId}[0],
231 $assignment->{WorkerId}[0]
232 );
233
234 $answers->eachAnswerValue(sub {
235 my ($questionId, $answerText) = @_;
236 push(@row, $questionId, $answerText);
237 });
238
239 $out->write(@row);
240 }
241  
242 return 1;