#!/usr/bin/perl
$version = 'Time-stamp: "2003-07-22 20:16:16 yamagen"';
$version =~ s/Time-stamp/Version/g;
$version =~ s/\"//g;
# kokin_stat.pl
#
# kokin statistics display
#
######################################################
require "../private/lib/jcode.pl";
require "../private/lib/site-lib.pl";
require "../private/lib/cgi-lib.pl";
&ReadParse;

$appli_name    ='./analysis.pl';   ## 755
$show_name      ='./show_wk.pl';   ## 755
$home_name     ='../kokin.html';   ## 755
$info_name     ='../kokin_info.html';   ## 755
$help_name     ='../kokin_help.html';   ## 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    ='Interactive Analysis of Kokin Waka Shu';
$dic_file      ='../kokin_count.db';       ## readable check!
$flag_dir      ='../image/flags/';  ## readable check!
$CharSet       ='x-euc-jp'; 
#
$|=1;
######################################################
# main
#####################################################

if ($in{'protocol'} eq 'calc') {    calc_html();   }
if ($in{'protocol'} eq '')     {    index_html();   }

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

sub index_html {
    open_config($config_file);
    cgi_header();
    print_tools();
    html_header();
    form();
    html_footer();
}

sub form {
    print <<FO;
<form method='POST' action=$appli_name>
<input type='hidden' name='protocol' value='calc'>
<table><tr><td> Key: <input type='text' name='key' size=30>
<input type='submit' name='submit' value='OK'>
</tr></table>
</form>
FO
}

sub calc_html {
    open_config($config_file);
    cgi_header();
    print_tools();
    html_header();
  jcode::convert(\$in{'key'}, "euc");
    read_dic();
    total_print();
    peripheral_print();
    matrix_print();
    html_footer();
}

sub total_print{

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

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

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

    $step = 0;
    $n = 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}";
	$step += $hin{$i};
	$out .= sprintf "<td align=right>%6.2f%%",$hin{$i}/$total*100;
	$out .= sprintf "<td align=right>%6.2f%%",$step/$total*100;
	$out .= "</tr>\n";	
    }

    print <<TTP;
<table bgcolor=white cellpadding=5 cellspacing=0>
<tr><td bgcolor=skyblue>Key <td>$in{'key'}</tr>
<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>
<p>

<u><i>Author</i></u><br>
<table bgcolor=white cellpadding=0 cellspacing=0>
<tr bgcolor=skyblue><td>\#<td>Author<td>Frequency<td>Ratio<td>Cumulation</tr>
$aut
</table>
<p>

TTP
}

sub peripheral_print{
    my $out = '';
    my $step = 0;
    my $n = 0;
    foreach $i (sort {$y_total{$b} <=> $y_total{$a}} keys %y_total) {
	$n++;
	$out .= "<tr><td align=right>$n<td>$i";
	$out .= "<td align=right>$y_total{$i}";
	$step += $y_total{$i};
	$out .= sprintf "<td align=right>%6.2f%%",$y_total{$i}/$tnow*100;
	$out .= sprintf "<td align=right>%6.2f%%",$step/$tnow*100;
	$out .= "</tr>\n";	
    }

    print <<TTP;
<u><i>Peripheral Frequency of Words</i></u><br>
<table bgcolor=white cellpadding=0 cellspacing=0>
<tr bgcolor=skyblue><td>\#<td>Author<td>Frequency<td>Ratio<td>Cumulation</tr>
$out
</table>
<p>
TTP
}

sub matrix_print{

    my $out = '';
    my $N = $L = 0;

    foreach $i (keys %x_total) {
	$N++;
	$v[$N]=$N;
	$v_label[$N]=$i;
    }

    foreach $i (keys %y_total) {
	$L++;
	$h[$L]=$L;
	$h_label[$L]=$i;
    }

    while ($stopfl == 0 && $rt <= 50) {

	$fh = 0;
	$fv = 0;
	
	for ($i = 1; $i <= $N; $i++) {
	    $s = 0;
	    $t = 0;
	    $mx = 0;
	    for ($j = 1; $j <= $L; $j++) {
		if ($word{"$v_label[$v[$i]] $h_label[$h[$j]]"}) {
		    $s += $j * $j;
		    $t++;
		    if ($j > $mx) {
			$mx = $j;
		    }
		}
	    }
	    if ($t > 0) {
		$mh[$v[$i]] = sqrt($s / $t) + ($t + $mx) / ($N * 100);
	    }
	    else {
		$mh[$v[$i]] = 0;
	    }
	}

	for ($i = 1; $i <= $N - 1; $i++) {
	    for ($j = $i + 1; $j <= $N; $j++) {
		if ($mh[$v[$j]] < $mh[$v[$i]]) {
		    $fh = 1;
		    $w = $v[$i];
		    $v[$i] = $v[$j];
		    $v[$j] = $w;
		}
	    }
	}

	for ($i = 1; $i <= $L; $i++) {
	    $s = 0;
	    $t = 0;
	    $mx = 0;
	    for ($j = 1; $j <= $N; $j++) {
		if ($word{"$v_label[$v[$j]] $h_label[$h[$i]]"}) {
		    $s += $j * $j;
		    $t++;
		    if ($j > $mx) {
			$mx = $j;
		    }
		}
	    }
	    if ($t > 0) {
		$mv[$h[$i]] = sqrt($s / $t) + ($t + $mx) / ($L * 100);
	    }
	    else {
		$mv[$h[$i]] = 0;
	    }
	}

	for ($i = 1; $i <= $L - 1; $i++) {
	    for ($j = $i + 1; $j <= $L; $j++) {
		if ($mv[$h[$j]] < $mv[$h[$i]]) {
		    $fv = 1;
		    $w = $h[$i];
		    $h[$i] = $h[$j];
		    $h[$j] = $w;
		}
	    }
	}
	
	if (($fh + $fv) == 0) {
	    $stopfl = 1;
	}
	$rt++;
    } # eof while

    printf("Reiteration = %3d<br>\n",$rt);

    $n = $N;

    if ($L > 100) {
	$out .= "                   | ";
	for ($i = 1; $i <= $L; $i++) {
	    if ($i % 100) {
		$out .= ' ';
	    } else {
		$out .= int($i / 100);
	    }
	}
	$out .= "\n";
    }

    $out .= "                   | ";
    for ($i = 1; $i <= $L; $i++) {
	if ($i % 10) {
	    $out .= ' ';
	} else {
	    $out .= substr(sprintf("%d", $i/10),-1,1);
	}
    }
    $out .= "\n";

    $ent = '';
    $out .= "-------------------| ";
    for ($i = 1; $i <= $L; $i++) {
	$out .= $i % 10;
	$ent .= "<tr><td align=right>$i<td align=right>";
	$ent .= "$h[$i]<td>$h_label[$h[$i]]";
	$ent .= "<td>$y_total{$h_label[$h[$i]]}</tr>\n";
    }
    $out .= "\n";

    $mat = "$N $L\n";
    for ($i = 1; $i <= $N; $i++) {
	$tmp=$v_label[$v[$i]];
	$out .= sprintf("%3d %3d ",$i,$v[$i]);
	$out .= "<a href=$show_name?iid=$tmp target=\"_blank\">$tmp</a> | ";
	for ($j = 1; $j <= $L; $j++) {
	    if ($word{"$v_label[$v[$i]] $h_label[$h[$j]]"}) {
		$out .= "1";
		$mat .= "1";
	    } else {
		$out .= "0";
		$mat .= "0";
	    }
	    $mat .= " ";
	}
	$mat .= "\n";
	$out .= sprintf(": %6.2f\n",$i,$mh[$v[$i]]);
    }

    print <<TTP;
<br><u><i>Matrix</i></u>
<pre>
<small>
$out
</small>
</pre>
<p>
<u><i>Entry</i></u><br>
<table bgcolor=white cellpadding=0 cellspacing=0>
<tr bgcolor=skyblue><td>\#<td>Entry<td>Frequency<td>Ratio<td>Cumulation</tr>
$ent
</table>
<u><i>Raw Data</i></u><br>
<pre>
$mat
</pre>
TTP


}

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

    my $n=0;

    $tnow = 0;
    open(LG, $dic_file) or die ("can't open $dic_file.");
    while (<LG>) {
	chomp;
	my ($iid,$seg,$ooc,$au,$dai,$ko,$kn,$kj,$sho) = split(/\s+/,$_);
#	if ($iidstack{$iid} && $dai ne '助詞' && $dai ne '助動') {
	if ($iidstack{$iid} && ($dai eq '名詞' || $dai eq '動詞')) {
	    $wordstack{"$kj"}=1;
	    $word{"$iid $kj"}=1;
	    $x_total{$iid}++;
	    $y_total{"$kj"}++;
	    $tnow++;
	}
    }
    close(LG);
}














