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

# perl get_matrix_by_series.pl あらすじデータ シリーズ名データ count_noun_with_sub.pl出力ファイル get_noun_list.pl出力ファイル 無視する単語の頻度数 > 出力ファイル

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

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

# count_noun_with_sub.pl出力ファイルから名詞-使用回数データを取得
# 名詞-複合名詞データを取得
open NOUN, "$ARGV[2]" or die;
my $min_num = "$ARGV[4]";
my %count = ();
my %noun = ();
while (<NOUN>) {
	chomp;
	my $line = decode('UTF-8', $_);
	my @data = split /\t/, $line;
	$count{$data[0]} = $data[2] if $data[2] > $min_num;

	my @sub_nouns = ();
	@sub_nouns = split /, /, $data[3] if $data[3];
	foreach my $sub_noun (@sub_nouns) {
		$sub_noun =~ s/\((\d+)\)$//;
		push @{$noun{$data[0]}}, $sub_noun if $1 > $min_num;
	}
}
close NOUN;

# get_noun_list.pl出力ファイルからシリーズ名-名詞-使用回数データを取得
open DATA, "$ARGV[3]" or die;
my %data = ();
while (<DATA>) {
	chomp;
	my $line = decode('UTF-8', $_);
	my @data = split /\t/, $line;
	if ($data[2]) {
		my @nouns = split /, /, $data[2];
		foreach my $noun (@nouns) {
			++${$data{$series{$book{$data[0]}}}}{$noun};
		}
	}
}
close DATA;


# 本ごとに出版年をキーワードに追加
my @noun_list = keys %count;
#push @noun_list, ('2000年', '2001年', '2002年', '2003年', '2004年', '2005年', '2006年', '2007年', '2008年', '2009年');
#foreach my $series (keys %data) {
#	foreach my $year (@{$series_publish{$series}}) {
#		++${$data{$series}}{"${year}年"};
#		++$count{"${year}年"};
#	}
#}

# 2次元配列を作りながら、1シリーズでしか登場しない名詞を除去
# 全体での名詞使用頻度が下限値以下の名詞を除去
my @series_list = keys %data;
my @matrix_data = ();
my $i = 0;
my @good_noun_list = ();
my @bad_noun_list = ();

my $count = 0;

foreach my $noun (@noun_list) {
	my @tmp = grep { $_ eq $noun } @bad_noun_list;
	next if $tmp[0];

	my $non_zero_count = 0;
	my $total_count = 0;
	@{$matrix_data[$i]} = ();
	foreach my $series (@series_list) {
		my $count = 0;
		$count += ${$data{$series}}{$noun} if ${$data{$series}}{$noun};

		# サブカテゴリの単語をカウント
		foreach my $sub_noun (@{$noun{$noun}}) {
			my @tmp_sub = grep { $_ eq $sub_noun } @bad_noun_list;
			next if $tmp_sub[0];

			# 単一シリーズの単語でないかチェック
			my $non_zero_count_sub = 0;
			foreach my $series2 (@series_list) {
				++$non_zero_count_sub if (${$data{$series2}}{$sub_noun} and ${$data{$series2}}{$sub_noun} > 0);
				last if $non_zero_count_sub > 1;
			}
			$count += ${$data{$series}}{$sub_noun} if (${$data{$series}}{$sub_noun} and $non_zero_count_sub > 1);
			push @bad_noun_list, $sub_noun if $non_zero_count_sub <= 1;
		}
		push @{$matrix_data[$i]}, $count;
		$total_count += $count;
		++$non_zero_count if $count > 0;
	}
	if ($non_zero_count > 1 and $total_count > $min_num) {
		push @good_noun_list, $noun;
		++$i ;
	}
}

# 2次元配列にラベルをつけて出力
my $midashi = '書名';
foreach my $noun (@good_noun_list) {
	$midashi = $midashi . "\t$noun";
}
$midashi = $midashi . "\n";
print encode('UTF-8', $midashi);

my $j = 0;
foreach my $series (@series_list) {
	print encode('UTF-8', $series);

	# シリーズ名に出版期間を追加
	my @year = sort @{$series_publish{$series}};
	my $year = '';
	if ($year[0] == $year[-1]) {
		$year = " ($year[0])";
	} else {
		$year = " ($year[0]" . '-' . "$year[-1])";
	}
	print encode('UTF-8', $year);

	foreach my $col (@matrix_data) {
		print encode('UTF-8', "\t${$col}[$j]");
	}
	print encode('UTF-8', "\n");
	++$j;
}
