#!/usr/bin/perl
#
# kokin_stat.pl
#
# kokin statistics display
#
######################################################
require "../private/lib/site-lib.pl";
$appli_name    ='./dic.pl';   ## 755
$config_file   ='../private/lib/site.conf';           ## 666

use POSIX qw(strftime);
$ddate       = strftime "%a %b %e %H:%M:%S %Y", localtime;
$fdate       = strftime "%Y/%m/%d %a %H:%M:%S", localtime;

$this_title    ='Kokinshu Statistical Info.';
$dic_file      ='../kokin_count.db';       ## readable check!
$flag_dir      ='../image/flags/';  ## readable check!
$CharSet       ='x-euc-jp'; 
$max_num       = 10; # ベスト10
$max_hrs       = 24;
$iu = '../image/bluu.gif';
$ie = '../image/blue.gif';
$firstdate = '';
#
$|=1;
######################################################
# main
#####################################################
@hrs = (
 '0000bb','0000cc','0000dd','0000ee','0000ff','00aaff',
 '00ccff','00ffff','aaffff','ccffff','ddffff','eeffff',
 'ffffff','ffffdd','ffffcc','ffffbb','ffdd00','ffa000',
 'ff0000','a000f0','0000dd','0000dd','0000bb','000088');

@chr = (
 'white','white','white','white','white','white',
 'black','black','black','black','black','black',
 'black','black','black','black','black','black',
 'white','white','white','white','white','white');

$protocol = $in{'submit'};

if($protocol eq '')   {    index_html();   }

######################################################
# sub 
######################################################


sub index_html{
    open_config($config_file);
    cgi_header();
    html_header();
    read_dic();
    total_print();
    html_footer();
}

sub total_print{

    my $noentry = 0;
    my $out = '';
    my $n = 0;

    foreach $i (keys %entry) {
	$noentry++;
    }

    my $hin_step = 0;
    foreach $i (sort {$hin{$b} <=> $hin{$a}} keys %hin) {
	$n++;
	$out .= "<tr><td align=right>$n<td>$i";
	$out .= "<td align=right>$hin{$i}";
	$hin_step += $hin{$i};
	$out .= sprintf "<td align=right>%6.2f%%",$hin{$i}/$total*100;
	$out .= sprintf "<td align=right>%6.2f%%",$hin_step/$total*100;
	$out .= "</tr>\n";
	
    }

    print <<TTP;
<table bgcolor=white cellpadding=5 cellspacing=0>
<tr><td bgcolor=skyblue>Calculated <td>$fdate</tr>
<tr><td bgcolor=skyblue>The number of entry
<td><font color=red> $noentry </font> words
<tr><td bgcolor=skyblue>The number of data
<td><font color=red> $total </font> lines
</tr></table>
<p>
<u><i>Part of Speech Count</i></u><br>
<table bgcolor=white cellpadding=0 cellspacing=0>
<tr bgcolor=skyblue><td>\#<td>POS<td>Frequency<td>Ratio<td>Cumulation</tr>
$out
</table>
TTP
}

sub read_dic{
    $total = 0;
    open(LG, $dic_file) or die ("can't open $dic_file.");
    while (<LG>) {
	chomp;
	if (!/^\#/) {
	    my ($iid,$seg,$ooc,$au,$dai,$kn,$kj,$sho) = split(/\s+/,$_);
	    $hin{$dai}++;
	    $entry{"$kn $dai"}++;
	    $total++;
	}
    }
    close(LG);
}








