#!/usr/local/bin/perl -w # # David MacKay Dec 97 # # frequency.p - counts letters (lower case) and writes a huffman code # example usage # frequency.p /usr/doc/FAQ/linux-faq.ascii # $verbose = 0 ; @a = <> ; # read in everything. $b = join ( '' , @a ) ; $b =~ s/\s+/_/g ; $row = 0 ; $Count = 0 ; for $f ('a','b','c','d','e','f','g','h','i','j','k','l','m','n', 'o','p','q','r','s','t','u','v','w','x','y','z','_') { $count = $b =~ s/$f//g; print "$f : $count\n" ; $pr{$f} = $count ; $arr[$row] = $f ; $code{$f} = "" ; $c[$row] = $count ; $ccc[$row] = $count ; # c is destroyed later $row ++ ; $Count += $count ; } $M = $row ; if ( $verbose ) { print "omitted guff->" ; print $b,"<-\n" ; } # make huffman code # @sorted = sort bypr @arr ; print "sorted characters\n" ; print @sorted , "\n" ; sub bypr { $pr{$a} <=> $pr{$b} ; } print @c ; print "\n" ; # print $c[0],"\n" ; &iterate ( $M ) ; # iterate makes the tree, now we need to deduce the # codewords. # now deduce the codewords. Start from the root with the null codeword. $codes[0] = "" ; $lengths[0] = 0 ; for ( $mm = 2 ; $mm <= $M ; $mm ++ ) { $f = $ff[$mm] ; $s = $ss[$mm] ; $co = $codes[$f] ; # this is the one we will expand $le = $lengths[$f] ; # move down everyone below s for ( $m = $mm - 1 ; $m > $s ; $m -- ) { $codes[$m] = $codes[$m-1] ; $lengths[$m] = $lengths[$m-1] ; } $codes[$f] = $co."0" ; $codes[$s] = $co."1" ; $lengths[$f] = $le + 1 ; $lengths[$s] = $le + 1 ; } # write the answer print " codewords : [\n" ; $L = 0.0 ; $H = 0.0 ; for ( $mm = 0 ; $mm < $M ; $mm ++ ) { # print $arr[$mm] , " " , $codes[$mm] , "\t\\\n" ; $prob[$mm] = 1.0 * $ccc[$mm] / $Count ; if ( $ccc[$mm] > 0.0 ) { $logp = log($prob[$mm])*1.0/log(2.0) ; $H += $ccc[$mm] * log( 1.0 * $Count / $ccc[$mm] ) ; } else { print STDERR "WARNING ccc[$mm] = $ccc[$mm]\n" ; $logp = 0.0 ; } print $arr[$mm] , " " ; printf "%-10.3g %-6.1f %2d" , $prob[$mm] ,-$logp , $lengths[$mm] ; print " " , $codes[$mm] , "\n" ; $L += 1.0 * $lengths[$mm] * $ccc[$mm] ; } print "]\n" ; $L = 1.0 * $L / $Count ; $H = $H / ( $Count * log(2.0) ) ; printf "total count %10.5g\n" , $Count ; printf "expected length %10.5g\n" , $L ; printf "entropy %10.5g\n" , $H ; # end sub bynumber { $a <=> $b; } sub iterate { #usage ( $m ) local ( $m ) = @_ ; print "entered iterate, m=$m\n" ; # print $c[0],"\n" ; print join ( ' ' , @c ) , "\n" ; # run through the array, finding the smallest two entries @sorted = sort bynumber @c[0..$m-1] ; $th = $sorted[ 1 ] ; $bot = $sorted[ 0 ] ; # find the first that is below $bot for ( $mm = 0 , $found = 0 ; $mm < $m ; $mm ++ ) { if ( $c[$mm] <= $bot ) { $second = $mm ; $found ++ ; last ; } } # find the first that is below th: for ( $mm = 0 ; $mm < $m ; $mm ++ ) { if ( ($second != $mm) && ($c[$mm] <= $th) ) { $first = $mm ; $found ++ ; last ; } } if ( $found != 2 ) { print STDERR "warning didn't find 2! threshold = !\n" ; } if ( $first > $second ) { $tmp = $first ; $first = $second ; $second = $tmp ; } # throw the weight of the 2nd into the 1st $c[$first] += $c[$second] ; # move everyone else up the list for ( $mm = $second ; $mm < $m-1 ; $mm ++ ) { $c[$mm] = $c[$mm+1] ; } $c[$mm] = "" ; # record what has been done $ff[$m] = $first ; $ss[$m] = $second ; print $first, " " , $second , "\n" ; if ( $m > 2 ) { &iterate ( $m-1 , @c ) ; } }