#!/usr/bin/perl
use strict;
use warnings;
use Encode qw/decode/;
use Encode qw/encode/;
use utf8;

# perl count_subcluster_word3.pl あらすじデータ シリーズ名データ 行列データ サブクラスターデータ > 出力ファイル

# 元ファイルから書名-出版年データを取得
open REF, "$ARGV[0]" or die;
my %book = ();
my %publish_date = ();
while (<REF>) {
	chomp;
	my $line = decode('UTF-8', $_);
	my @data = split /\t/, $line;
	$publish_date{$data[2]} = $data[0];
	$publish_date{$data[2]} =~ s/^(\d{4}).*/$1/;
}
close REF;

# シリーズ名-出版年データを取得
open SER, "$ARGV[1]" or die;
my %series_publish = ();
while (<SER>) {
	chomp;
	my $line = decode('UTF-8', $_);
	my @data = split /\t/, $line;
	push @{$series_publish{$data[0]}}, $publish_date{$data[1]} if $publish_date{$data[1]};
}
close SER;

# 行列データを取得
# 単語-総使用回数データを取得
open MATRIX, "$ARGV[2]" or die;
my $line_flag = 0;
my @word_list = ();
my @series_list = ();
my %series_noun_count = ();
my %word_total_count = ();
my $total_series_count = 0;
while (<MATRIX>) {
	chomp;
	my $line = decode('UTF-8', $_);
	my @tmp = split /\t/, $line;
	unless ($line_flag) {
		shift @tmp;
		@word_list = @tmp;
		++$line_flag;
		next;
	}
	my $series = shift @tmp;
	$series =~ s/ \(.*?\)$//;
	push @series_list, $series;
	@{$series_noun_count{$series}} = @tmp;
	my $i = 0;
	foreach my $count (@tmp) {
		$word_total_count{$word_list[$i]} += $count;
		++$i;
	}
	++$total_series_count;
}
close MATRIX;

# サブクラスタ-シリーズ名データを取得
open CLUSTER, "$ARGV[3]" or die;
my %cluster_series = ();
my $i = 0;
while (<CLUSTER>) {
	chomp;
	push @{$cluster_series{$_}}, $series_list[$i];
	++$i;
}
close CLUSTER;

my %year_count = ();
foreach my $subcluster (sort { $a <=> $b } keys %cluster_series) {
	print encode('UTF-8', "$subcluster\n\n");

	my @matrix_data = ();
	my $k = 0;
	my $j = 0;
	my @good_noun_list = ();
	my @major_noun_list = ();
	my @sub_major_noun_list = ();
	foreach my $noun (@word_list) {
		my $non_zero_count = 0;
		my $count_sum = 0;
		@{$matrix_data[$k]} = ();
		my $series_sum = 0;
		foreach my $series (@{$cluster_series{$subcluster}}) {
			push @{$matrix_data[$k]}, ${$series_noun_count{$series}}[$j];
			++$non_zero_count if ${$series_noun_count{$series}}[$j] > 0;
			$count_sum += ${$series_noun_count{$series}}[$j];
			++$series_sum;
		}
		if ($non_zero_count >= $series_sum / 2 and $count_sum / $series_sum >= 0.5) {
			if ($count_sum / $word_total_count{$noun} >= ($series_sum / $total_series_count) * 5) {
				push @major_noun_list, $noun;
				push @good_noun_list, $noun;
				++$k;
			} elsif ($count_sum / $series_sum >= 1) {
				push @sub_major_noun_list, $noun;
				push @good_noun_list, $noun;
				++$k;
			}
		}
		++$j;
	}

	# 2次元配列にラベルをつけて出力
	my $midashi = '書名';
	foreach my $noun (@good_noun_list) {
		my @tmp_major = grep { $_ eq $noun } @major_noun_list;
		my $tmp_major = @tmp_major;
		my @tmp_sub = grep { $_ eq $noun } @sub_major_noun_list;
		my $tmp_sub = @tmp_sub;
		if ($tmp_major) {
			$midashi = $midashi . "\t<<${noun}>>";
		} elsif ($tmp_sub) {
			$midashi = $midashi . "\t<${noun}>";
		}
	}
	$midashi = $midashi . "\n";
	print encode('UTF-8', $midashi);

	my $l = 0;
	my $noun_num = @good_noun_list;
	foreach my $series (@{$cluster_series{$subcluster}}) {
		print encode('UTF-8', $series);
			foreach my $year (@{$series_publish{$series}}) {
				++${$year_count{$subcluster}}{$year};
			}
		my $m = 1;
		foreach my $col (@matrix_data) {
			last if $m > $noun_num;
			print encode('UTF-8', "\t${$col}[$l]");
			++$m;
		}
		print encode('UTF-8', "\n");
		++$l;
	}
	print encode('UTF-8', "\n");
}

print encode('UTF-8', "サブクラスター\t2000\t2001\t2002\t2003\t2004\t2005\t2006\t2007\t2008\t2009\n");
foreach my $subcluster (sort { $a <=> $b } keys %year_count) {
	print encode('UTF-8', "$subcluster");
	foreach my $year ((2000,2001,2002,2003,2004,2005,2006,2007,2008,2009)) {
		if (${$year_count{$subcluster}}{$year}) {
			print encode('UTF-8', "\t${$year_count{$subcluster}}{$year}");
		} else {
			print encode('UTF-8', "\t0");
		}
	}
	print encode('UTF-8', "\n");
}
