#!/usr/local/bin/perl -w # # David MacKay Dec 97 # # frbigram.p - counts letters and bigram stats # frbigram.p ~/tex/please.tex # frbigram.p /usr/doc/FAQ/linux-faq.ascii $verbose = 1 ; @a = <> ; # read in everything. $b = join ( '' , @a ) ; $b =~ s/[^a-zA-Z]/ /g ; # turn all punctuation to space $b =~ s/\s+/_/g ; # turn all space to _ # decapitalize $b =~ s/([A-Z])/\L$1/g ; $borig = $b ; # print $b ; # monogram stats $row = 0 ; $totcount = 0 ; # remove initial character $b =~ s/^.// ; 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; if ($verbose>=2){ print "\n$f : " ; } if ( !$count ) { $count = 0 ; } print "$count " ; $pr{$f} = $count ; # $arr[$row] = $f ; # $code{$f} = "" ; # $c[$row] = $count ; $row ++ ; $totcount += $count } # $M = $row ; if ( $verbose >= 2 ) { print "omitted guff->" ; print $b,"<-\n" ; } print "\n\n" ; # bigrams $bicount = 0 ; $fi = 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','_') { $fi ++ ; $gi = 0; for $g ('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','_') { #for $f ('_','d','e','q','u'){ # for $g ('_','d','e','q','u'){ $gi ++ ; $b = $borig ; $count = $b =~ s/$f$g/12/g; if ( ($fi == $gi) && ($count) ) { $count += $b =~ s/2[1$g]/\.\./g; # this will catch eee if it happens! } if ( !$count ) { $count = 0 ; } if ($verbose>=2){ print "\n$f$g : " ; } print "$count " ; $bicount += $count ; } print "\n" ; } # eeabae if ($verbose>=2) {print "\n totcount $totcount, bicount $bicount\n" ; } # /home/mackay/itp/bigrams> frbigram.p /usr/doc/FAQ/linux-faq.ascii > mon-bi # tail -27 mon-bi | hintond.p rotate=1 o=hd_bigram.ps zero=0.0 range=1700 rows=27 bellh=27 background=0.0 27 # head -1 mon-bi | hintond.p o=hd_marg.ps zero=0.0 range=18000 rows=1 background=0.0 27 # to get conditional bigram distributions, # cp mon-bi con-bi # see conbigram.p