#!/usr/bin/perl
#
# dic.pl
#
# dictionary 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    ='Dictionary Statistical Info.';
$dic_file      ='../wk.dic';       ## 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');

%sho2dai = ('名','名詞',    	
	    '代',  '代名',
	    '枕',  '枕詞',

	    '形ク','形容',
	    '形シク','形容',	
	    '形動ナリ','形動',
	    '形動タリ','形動',

	    '感',  '感動',
	    '接詞','接詞',
	    '接頭','接頭',
	    '接尾','接尾',
	    '副',  '副詞',
	    '連語','連語',
	    '連体','連体',

	    '格助','助詞',
	    '感助','助詞',
	    '間助','助詞',
	    '係助','助詞',
	    '終助','助詞',
	    '準助','助詞',
	    '接助','助詞',
	    '副助','助詞',	 

	    'カ四','動詞',
	    'ガ四','動詞',	
	    'サ四','動詞',
	    'タ四','動詞',	 
	    'ハ四','動詞',
	    'バ四','動詞',
	    'マ四','動詞',
	    'ラ四','動詞',

	    'カ変','動詞',	  
	    'サ変','動詞',	  
	    'ナ変','動詞',	  
	    'ラ変','動詞',

	    'カ上一','動詞',
	    'カ上二','動詞',
	    'ガ上二','動詞',	 
	    'タ上二','動詞',
	    'ダ上二','動詞',
	    'ナ上一','動詞',
	    'ハ上一','動詞',
	    'ハ上二','動詞',	
	    'バ上二','動詞',
	    'マ上一','動詞',
	    'マ上二','動詞',
	    'ヤ上一','動詞',
	    'ヤ上二','動詞',
	    'ラ上二','動詞',	
	    'ワ上一','動詞',

	    'カ下一','動詞',

	    'ア下二','動詞',
	    'カ下二','動詞',
	    'ガ下二','動詞',
	    'サ下二','動詞',
	    'ザ下二','動詞',
	    'タ下二','動詞',
	    'ダ下二','動詞',
	    'ナ下二','動詞',	
	    'ハ下二','動詞',
	    'バ下二','動詞',
	    'マ下二','動詞',
	    'ヤ下二','動詞',	
	    'ラ下二','動詞',
	    'ワ下二','動詞',

	    '可',  '助動',
	    '過',  '助動',	
	    '完',  '助動',
	    '希',  '助動',
	    '使',  '助動',
	    '自',  '助動',	
	    '受',  '助動',
	    '消',  '助動',
	    '消意','助動',
	    '消推','助動',
	    '推',  '助動',
	    '接',  '助動',
	    '尊',  '助動',	
	    '断',  '助動',
	    '丁',  '助動',
	    '伝',  '助動',
	    '比',  '助動');

$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 {$big{$b} <=> $big{$a} } keys %big) {
	$n++;
	$out .= "<tr><td align=right>$n<td>$i";
	$out .= "<td align=right>$big{$i}";
	$hin_step += $big{$i};
	$out .= sprintf "<td align=right>%6.2f%%",  $big{$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 ($kana,$kanji,$hinshi) = split(/\s+/,$_);
	    my ($dai,$sho) = split(/\-/,$hinshi);
	    if(!$dai){
		print "OHHH $_<br>\n";
	    }
	    $hin{$dai}++;
	    $big{$sho2dai{$dai}}++;
	    $entry{"$kana $hinshi"}++;
	    $total++;
	}
    }
    close(LG);
}










