#!/usr/bin/perl
$version = 'Time-stamp: "2004-02-09 17:28:14 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    ='./analysis03.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    ='Summary - Vocabulary of Original Poem';
$dic_file      ='../tagged-kokin.dat'; ## readable check!
$tab_file      ='../bunrui-category.dat';
$sak_file      ='/home/yamagen/kokin_dir/sakuin-kokin.dat';
$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();
    read_tab();
    read_sak();
    read_dic();
    print_result();
    html_footer();
}

sub read_dic{
    open(LG, $dic_file) or die ("can't open $dic_file.");
    while (<LG>) {
	chomp;
	my ($ok,$else) = split(/:/,$_);
	my ($iid,$pos,$shu,$hir,$kan,$meta) = split(/\s+/,$else);
	if ($meta ne '----') {
	    my @metaline = split(/\,/,$meta);
	    foreach $i (@metaline) {
		$op{wider($i)}++;
		$op{first($i)}++;
		$dsum{first($i)}++;
		$op{firstpone($i)}++;
		$dsum{firstpone($i)}++;
	    }
	}
    }
    close(LG);
}

sub print_result {

    print "<pre>\n";

    print "=-=-=-=-= S U M M A R Y =-=-=-=-=\n";
    foreach $i (sort keys %dsum) {
	$num2des{$i} = 'Α΄ΒΞ' if (!$i);
	printf("%-10s %10d %10d: %s\n" ,$i,$op{$i},$num2frq{$i},$num2des{$i});
    }

    print "\n- - - - - C A T E G O R Y - - - - -\n";
    foreach $i (sort keys %num2des) {
	$num2des{$i} = 'Α΄ΒΞ' if (!$i);
	printf("%-10s %10d %10d: %s\n" ,$i,$op{$i},$num2frq{$i},$num2des{$i});
    }

    print "</pre>\n";
}

sub read_tab{
    open(LG, $tab_file) or die ("can't open $tab_file.");
    while (<LG>) {
	chomp;
	my ($num,$des) = split(/\s+/,$_);
	$num2des{firstpone($num)} = $des if (!$num2des{firstpone($num)});
	$num2des{first($num)} = $des if (!$num2des{first($num)});
	$num2des{$num} = $des;
    }
    close(LG);
}

sub read_sak{
    open(LG, $sak_file) or die ("can't open $sak_file.");
    while (<LG>) {
	chomp;
	my ($hir,$kan,$num,$line,$item) = split(/\,/,$_);
	$num2frq{$num}++;
	$num2frq{wider($num)}++;
	$num2frq{first($num)}++;
	$num2frq{firstpone($num)}++;
    }
    close(LG);
}

sub wider {
    return $1 if ($_[0] =~ /(\d\.\d+)\-\d+/);
}

sub first {
    return $1 if ($_[0] =~ /(\d\.).+\d+/);
}

sub firstpone {
    return $1 if ($_[0] =~ /(\d\..)+\d+/);
}







