#!/usr/bin/perl
use 5.001 ; use strict ; use warnings ;
getopts "b:e=" , \my%o ;
# Reads the Inputs
my $tmp = <> if $o{'='} ;
my %strcnt ;
my $allcnt = 0 ;
while ( <> ) {
chomp ;
$strcnt { $_ } ++ ;
$allcnt ++ ;
}
# Calculates the entropy
my $entropy = 0 ;
for ( values %strcnt ) {
my $p = $_ / $allcnt ;
$entropy -= $p * log $p ;
}
# Outputs the result.
my @out ;
unshift @out , exp $entropy if $o{e} ;
$o{b} //= 2 ;
unshift @out , $entropy / log $o{b} ;
print join "\t" , @out ;
print "\n" ;
## ヘルプとバージョン情報
BEGIN {
$Getopt::Std::STANDARD_HELP_VERSION = 1 ;
our $VERSION = 0.21 ;
# 最初は 0.21 を目安とする。
# 1.00 以上とする必要条件は英語版のヘルプをきちんと出すこと。
# 2.00 以上とする必要条件はテストコードが含むこと。
}
sub HELP_MESSAGE {
use FindBin qw[ $Script ] ;
$ARGV[1] //= '' ;
open my $FH , '<' , $0 ;
while(<$FH>){
s/\$0/$Script/g ;
print $_ if s/^=head1// .. s/^=cut// and $ARGV[1] =~ /^o(p(t(i(o(ns?)?)?)?)?)?$/i ? m/^\s+\-/ : 1;
}
close $FH ;
exit 0 ;
}
=encoding utf8
=head1
entropy
Calculates the entropy. Input data values are assumed to be separated by "\n".
Options:
-b num : changes the base number to num. Default : 2
-e : shows the 2 ** ( entropy ). This is independent from the value specified by -b.
--help : Shows this help.
--version : Shows the version information of this program.
=cut