| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Helper Perl script for ulqda |
| 4 |
# A LaTeX package supporting Qualitative Data Analysis |
| 5 |
# |
| 6 |
# Copyright (C) 2009 by Ivan Griffin |
| 7 |
# |
| 8 |
# This file may be distributed and/or modified under the |
| 9 |
# conditions of the LaTeX Project Public License, either |
| 10 |
# version 1.2 of this license or (at your option) any later |
| 11 |
# version. The latest version of this license is in: |
| 12 |
# |
| 13 |
# http://www.latex-project.org/lppl.txt |
| 14 |
# |
| 15 |
# and version 1.2 or later is part of all distributions of |
| 16 |
# LaTeX version 1999/12/01 or later. |
| 17 |
# |
| 18 |
|
| 19 |
use Getopt::Long; |
| 20 |
use Digest::SHA1 qw(sha1 sha1_hex sha1_base64); |
| 21 |
|
| 22 |
sub Display_Version() |
| 23 |
{ |
| 24 |
print "Version: ulqda.pl [2009/06/11 v1.1 Qualitative Data Analysis package]\n\n"; |
| 25 |
exit; |
| 26 |
} |
| 27 |
|
| 28 |
sub Display_Usage() |
| 29 |
{ |
| 30 |
print<<"EOF"; |
| 31 |
Usage: $0 \\ |
| 32 |
<--list>|<--graphflat>|<--graphnet>|<--cloud>|<--help>|<--version> \\ |
| 33 |
[<--filter section>] [<--number-->] infile.csv outfile |
| 34 |
|
| 35 |
--filter: filter based on document section label |
| 36 |
--graphflat: Generate GraphViz .dot output of codes (unconnected) |
| 37 |
--graphnet: Generate GraphViz .dot output of codes (connected) |
| 38 |
--cloud: Generate Cloud output of codes |
| 39 |
--help: Print this help and exit |
| 40 |
--list: Generate LaTeX table of codes (labelled as table:qda_codes) |
| 41 |
--number: Output quantity details |
| 42 |
--version: Dispaly version information |
| 43 |
EOF |
| 44 |
exit; |
| 45 |
} |
| 46 |
|
| 47 |
sub sort_codes_hashvalue_descending { |
| 48 |
$codes{$b} <=> $codes{$a}; |
| 49 |
} |
| 50 |
|
| 51 |
sub sort_codes_alphabetic { |
| 52 |
$a cmp $b; |
| 53 |
} |
| 54 |
|
| 55 |
@colors = ("#d40000", "#e35a24", "#f67c00", "#faa800", "#ffc000", "#ffde00", "#aae900", "#62dc68", "#bbcced", "#a3bbe0", "#8aaad4", "#7299c7", "#5676b5", "#4554a3", "#2e3191", "#000472"); |
| 56 |
|
| 57 |
$result = GetOptions("number" => \$options{'n'}, |
| 58 |
"list" => \$options{'l'}, |
| 59 |
"cloud" => \$options{'c'}, |
| 60 |
"graphflat" => \$options{'g'}, |
| 61 |
"filter=s" => \$options{'f'}, |
| 62 |
"graphnet" => \$options{'G'}, |
| 63 |
"version" => \$options{'v'}, |
| 64 |
"help" => \$options{'h'}); |
| 65 |
|
| 66 |
&Display_Usage() if !$result; |
| 67 |
&Display_Usage() if $options{'h'}; |
| 68 |
|
| 69 |
&Display_Version() if $options{'v'}; |
| 70 |
|
| 71 |
if (!($options{'c'} || $options{'l'} || $options{'g'} || $options{'G'})) |
| 72 |
{ |
| 73 |
print "Requires one of --cloud, --list, --graphflat, --graphnet, --version or --help\n\n"; |
| 74 |
&Display_Usage() |
| 75 |
} |
| 76 |
|
| 77 |
if (($ARGV[0]) && ($ARGV[1])) |
| 78 |
{ |
| 79 |
open(FILEIN, "<$ARGV[0]") or die "Could not open input file $ARGV[0]\n"; |
| 80 |
open(FILEOUT, ">$ARGV[1]") or die "Could not open output file $ARGV[1]\n"; |
| 81 |
} |
| 82 |
else |
| 83 |
{ |
| 84 |
&Display_Usage(); |
| 85 |
} |
| 86 |
|
| 87 |
if ($options{'l'}) |
| 88 |
{ |
| 89 |
print FILEOUT << "EOF"; |
| 90 |
{ |
| 91 |
\\vspace{0.1in} |
| 92 |
\\hrule |
| 93 |
|
| 94 |
\\begin{multicols}{3} |
| 95 |
\\label{table:qda_codes} |
| 96 |
EOF |
| 97 |
} |
| 98 |
elsif ($options{'c'}) |
| 99 |
{ |
| 100 |
# |
| 101 |
print FILEOUT << "EOF"; |
| 102 |
{ |
| 103 |
\\begin{center} |
| 104 |
\\noindent% |
| 105 |
EOF |
| 106 |
} |
| 107 |
elsif ($options{'g'} || $options{'G'}) |
| 108 |
{ |
| 109 |
print FILEOUT << "EOF"; |
| 110 |
digraph codes { |
| 111 |
overlap=scale |
| 112 |
ratio=compress |
| 113 |
smoothType=spring |
| 114 |
repulsiveforce=1.2 |
| 115 |
splines=true |
| 116 |
|
| 117 |
# |
| 118 |
# Nodes |
| 119 |
# |
| 120 |
EOF |
| 121 |
} |
| 122 |
|
| 123 |
<FILEIN>; # gobble first line (it is just header) |
| 124 |
|
| 125 |
# now, for each line, parse into comma separated values |
| 126 |
# codes are further split based on the '!' separator character |
| 127 |
# e.g. risk!business becomes two codes, risk and business, with a |
| 128 |
# connection between risk->business |
| 129 |
GOBBLE_LOOP: while (<FILEIN>) |
| 130 |
{ |
| 131 |
chomp; |
| 132 |
($page, $section, $code_list, $text) = split(/\,/, $_, 4); |
| 133 |
#print"DEBUG: >>$code_list<< $_ \n"; |
| 134 |
|
| 135 |
if (($options{'f'}) && ($options{'f'} != $section)) |
| 136 |
{ |
| 137 |
# filtering selected so if no match, then skip |
| 138 |
next GOBBLE_LOOP; |
| 139 |
} |
| 140 |
|
| 141 |
foreach $code ( split(/\!/, $code_list) ) |
| 142 |
{ |
| 143 |
$code =~ s/^ //g; |
| 144 |
$codes{$code} += 1; |
| 145 |
} |
| 146 |
|
| 147 |
@connections = (@connections, $code_list); |
| 148 |
} |
| 149 |
close(FILEIN); |
| 150 |
|
| 151 |
# |
| 152 |
# Find the maximum number of connections and the minimum number of connections |
| 153 |
# This will be used for scaling subsequently |
| 154 |
# |
| 155 |
$iterationCount = 0; $nodeCount = 0; |
| 156 |
$maxConnections = 0; $minConnections=65535; |
| 157 |
|
| 158 |
if ($options{'c'}) |
| 159 |
{ |
| 160 |
@key_list = (sort sort_codes_alphabetic keys %codes); |
| 161 |
} |
| 162 |
else |
| 163 |
{ |
| 164 |
@key_list = (sort sort_codes_hashvalue_descending keys %codes); |
| 165 |
} |
| 166 |
|
| 167 |
LOOP: foreach $i (sort sort_codes_hashvalue_descending keys %codes) |
| 168 |
{ |
| 169 |
if ($codes{$i} > $maxConnections) { $maxConnections = $codes{$i} } |
| 170 |
if ($codes{$i} < $minConnections) { $minConnections = $codes{$i} } |
| 171 |
} |
| 172 |
|
| 173 |
# |
| 174 |
# For each code, output it as requested |
| 175 |
# |
| 176 |
foreach $i (@key_list) |
| 177 |
{ |
| 178 |
$digest = sha1_hex($i); |
| 179 |
|
| 180 |
if ($options{'l'} && ($iterationCount)) |
| 181 |
{ |
| 182 |
print FILEOUT "\\\\\n"; |
| 183 |
} |
| 184 |
elsif ($options{'c'}) |
| 185 |
{ |
| 186 |
my $base = int(7 + ($codes{$i}/3)); |
| 187 |
my $lead = $base + 3; |
| 188 |
my $raisept = int(($base - 7)/4); |
| 189 |
|
| 190 |
if ($raisept) { print FILEOUT "\\raisebox{-${raisept}pt}";} |
| 191 |
print FILEOUT "{\\fontsize{$base}{$lead}\\selectfont "; |
| 192 |
} |
| 193 |
elsif ($options{'g'} || $options{'G'}) |
| 194 |
{ |
| 195 |
$nodeCount++; |
| 196 |
print FILEOUT "Node$digest [label=\""; |
| 197 |
} |
| 198 |
|
| 199 |
$i =~ s/!/:/g; |
| 200 |
print FILEOUT "$i"; |
| 201 |
|
| 202 |
if ($options{'c'}) |
| 203 |
{ |
| 204 |
print FILEOUT "}\n"; |
| 205 |
} |
| 206 |
elsif ($options{'n'}) |
| 207 |
{ |
| 208 |
print FILEOUT "{\\textcolor{red}{" if ($options{'l'}); |
| 209 |
print FILEOUT "($codes{$i})"; |
| 210 |
print FILEOUT "}}" if ($options{'l'}); |
| 211 |
} |
| 212 |
|
| 213 |
if ($options{'g'} || $options{'G'}) |
| 214 |
{ |
| 215 |
$fontSize = 15 + ((60 * $codes{$i})/$maxConnections); |
| 216 |
print FILEOUT "\", fontsize=$fontSize"; |
| 217 |
#if ($iterationCount <= 5) |
| 218 |
{ |
| 219 |
# heatmap based colors |
| 220 |
# $colorIndex = 6 - int((6*($codes{$i} - $minConnections))/($maxConnections - $minConnections)); |
| 221 |
# index based colors |
| 222 |
$colorIndex = int(($iterationCount*16)/(keys %codes)); |
| 223 |
print FILEOUT ",color=\"$colors[$colorIndex]\",style=filled"; |
| 224 |
} |
| 225 |
print FILEOUT "]\n"; |
| 226 |
} |
| 227 |
elsif (! ($options{'l'} || $options{'c'}) ) |
| 228 |
{ |
| 229 |
print FILEOUT "\n"; |
| 230 |
} |
| 231 |
$iterationCount++; |
| 232 |
} |
| 233 |
|
| 234 |
# |
| 235 |
# For connection graphs, process each connection between points |
| 236 |
# |
| 237 |
if ($options{'G'}) |
| 238 |
{ |
| 239 |
print FILEOUT <<"EOF"; |
| 240 |
|
| 241 |
# |
| 242 |
# Connections |
| 243 |
# |
| 244 |
EOF |
| 245 |
|
| 246 |
for $code_list (@connections) |
| 247 |
{ |
| 248 |
print FILEOUT "// $code_list\n"; |
| 249 |
@code_list_array = split(/\!/, $code_list); |
| 250 |
$previous_code = shift(@code_list_array); |
| 251 |
$previous_code =~ s/^ //g; |
| 252 |
|
| 253 |
if (defined($previous_code)) |
| 254 |
{ |
| 255 |
foreach $code ( @code_list_array ) |
| 256 |
{ |
| 257 |
$code =~ s/^ //g; |
| 258 |
$previous_digest = sha1_hex($previous_code); |
| 259 |
$digest = sha1_hex($code); |
| 260 |
|
| 261 |
# ensure only 1 connection between points |
| 262 |
if (!defined($connection{"$previous_digest-$digest"})) |
| 263 |
{ |
| 264 |
$connection{"$previous_digest-$digest"} = 1; |
| 265 |
$len=0.25+(0.75*$codes{$previous_code}/$maxConnections); |
| 266 |
$w=0.25+(0.75*$codes{$previous_code}/$maxConnections); |
| 267 |
print FILEOUT "Node$previous_digest->Node$digest [label=\"\", w=$w, len=$len]\n"; |
| 268 |
} |
| 269 |
|
| 270 |
$previous_code = $code; |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
# |
| 277 |
# Tidy up |
| 278 |
# |
| 279 |
if ($options{'l'}) |
| 280 |
{ |
| 281 |
print FILEOUT << "EOF"; |
| 282 |
\\end{multicols} |
| 283 |
\\hrule |
| 284 |
EOF |
| 285 |
} |
| 286 |
elsif ($options{'c'}) |
| 287 |
{ |
| 288 |
print FILEOUT << "EOF"; |
| 289 |
\\end{center} |
| 290 |
EOF |
| 291 |
} |
| 292 |
|
| 293 |
print FILEOUT "\n}\n"; |
| 294 |
|
| 295 |
close(FILEOUT); |