-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscale_to_maxdiff.pl
executable file
·163 lines (162 loc) · 3.01 KB
/
scale_to_maxdiff.pl
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/perl
#
#
# scale_to_maxdiff.pl
#
# - given a set of MaxDiff questions and a set of word pairs rated on a scale,
# answer the MaxDiff questions
#
#
#
#
# Peter Turney
# December 19, 2011
#
#
#
#
#
#
# check command line arguments
#
if ($#ARGV != 2) {
print "\n\nUsage:\n\n";
print "scale_to_maxdiff.pl <input file of MaxDiff questions> <input file of scaled pairs> <output file of answers to MaxDiff questions>\n\n";
exit;
}
#
# input file of MaxDiff questions
#
$max_file = $ARGV[0];
#
# input file of scaled pairs
#
$sca_file = $ARGV[1];
#
# output file of answers to MaxDiff questions
#
$out_file = $ARGV[2];
#
#
#
#
#
#
# read in the scaled pairs
#
print "reading file of scaled pairs $sca_file ...\n";
#
%pair2scale = ();
$num_pairs = 0;
#
open(INF, "< $sca_file");
#
while ($line = <INF>) {
#
# typical $line:
#
# 52.0 "album:songs"
# 48.0 "army:soldier"
# 44.0 "book:page"
#
if ($line =~ /^\#/) { next; } # skip comments
if ($line =~ /(\S+)\s+(\S+)/) {
$scale = $1;
$pair = $2;
$pair2scale{$pair} = $scale;
$num_pairs++;
}
}
#
close(INF);
#
print "... read $num_pairs pairs ...\n";
print "... done.\n";
#
#
#
#
#
# read in the MaxDiff questions
#
print "reading file of MaxDiff questions $max_file ...\n";
#
@questions = ();
$num_quest = 0;
#
open(INF, "< $max_file");
#
while ($line = <INF>) {
#
# typical $line:
#
# "school:fish","library:book","flock:sheep","flock:bird"
# "class:student","pride:lion","band:musician","geese:gaggle"
#
if ($line =~ /^\#/) { next; } # skip comments
if ($line =~ /(\".+\")/) {
$question = $1;
push(@questions, $question);
$num_quest++;
}
}
#
close(INF);
#
print "... read $num_quest questions ...\n";
print "... done.\n";
#
#
#
#
#
#
#
# answer the questions
#
print "writing answers to $out_file ...\n";
#
open(OUTF, "> $out_file");
#
print OUTF "#\n";
print OUTF "# Generated by: scale_to_maxdiff.pl\n";
print OUTF "# Scaled Pairs File: $sca_file\n";
print OUTF "# MaxDiff Questions File: $max_file\n";
print OUTF "# MaxDiff Answers File: $out_file\n";
print OUTF "# Number of Unique Pairs: $num_pairs\n";
print OUTF "# Number of MaxDiff Questions: $num_quest\n";
print OUTF "#\n";
print OUTF "# relation1 relation2 relation3 relation4 least_illustrative most_illustrative\n";
print OUTF "#\n";
#
foreach $question (@questions) {
@pairs = split(/\,/, $question);
for ($i = 0; $i < 4; $i++) {
$pair = $pairs[$i];
$scale = $pair2scale{$pair};
if (! defined($scale)) {
die "ERROR: $pair is in $max_file but not in $sca_file\n";
}
if (($i == 0) || ($scale > $max_scale)) {
$max_scale = $scale;
$max_pair = $pair;
}
if (($i == 0) || ($scale < $min_scale)) {
$min_scale = $scale;
$min_pair = $pair;
}
}
push(@pairs, $min_pair);
push(@pairs, $max_pair);
$out_string = join(" ", @pairs);
print OUTF "$out_string\n";
}
#
close(OUTF);
#
print "... done.\n";
#
#
#
#
#