#!/usr/bin/perl

# split_sentence.plの出力ファイルの第2カラムのセンテンスについてMeCabによる形態素解析を行う。
# 連続する名詞については連結して複合名詞を生成し、センテンスから取得した名詞・複合名詞のリストを入力ファイルに追加して出力する。

use strict;
use warnings;
use Text::MeCab;
use Encode qw/decode/;
use Encode qw/encode/;
use utf8;

my $m = Text::MeCab->new();

open IN, "$ARGV[0]" or die;
while (<IN>) {
	chomp;
	my $line = decode('UTF-8', $_);
	my @data = split /\t/, $line;
	my $sentence = $data[1];
	next unless $sentence;

	my @nouns = ();
	my @noun_list = ();
	my $last_feature;
	my @last_word = ('','','');
	for (my $node = $m->parse($sentence); $node; $node = $node->next) {
		next unless $node->surface;

		my $surface = decode('UTF-8', $node->surface);
		my $feature = decode('UTF-8', $node->feature);
		my @feature = split /,/, $feature;

		# 複合名詞の生成
		if ($feature[0] eq '名詞' and !($feature[1] eq 'サ変接続' and $feature[6] eq '*') and $feature[1] ne '非自立') {
			if ($last_word[1] eq '動詞' and $last_word[2] eq '体言接続特殊２') {
				push @nouns, ($last_word[0], $surface);
			} elsif ($last_word[1] eq '接頭詞') {
				push @nouns, ($last_word[0], $surface);
			} elsif ($last_word[1] eq '動詞' and $last_word[2] eq '連用形' and $last_word[0] ne 'し') {
				if ($nouns[0]) {
					push @nouns, ($last_word[0], $surface);
				} else {
					push @nouns, $surface;
				}
			} else {
				push @nouns, $surface;
			}
		} elsif (!($feature[0] eq '動詞' and ($feature[5] eq '体言接続特殊２' or $feature[5] eq '連用形'))) {
			if ($nouns[0]) {
				if ($last_word[1] eq '動詞' and $last_word[2] eq '体言接続特殊２') {
					push @nouns, $last_word[0];
				} elsif ($last_word[1] eq '動詞' and $last_word[2] eq '連用形' and $last_word[0] ne 'し') {
					push @nouns, $last_word[0];
				}
			}
			if ($#nouns == 0) {
				unless ($last_feature eq '非自立') {
					push @noun_list, $nouns[0];
				}
			} elsif ($#nouns > 0) {
				my $joint_noun = join ' ', @nouns;
				push @noun_list, $joint_noun;
			}
			@nouns = ();
		}
		$last_feature = $feature[1];
		@last_word = ($surface, $feature[0], $feature[5]);

	}
	if ($#nouns == 0) {
		unless ($last_feature eq '非自立') {
			push @noun_list, $nouns[0];
		}
	} elsif ($#nouns > 0) {
		my $joint_noun = join ' ', @nouns;
		push @noun_list, $joint_noun;
	}

	my $noun_list = join ', ', @noun_list;
	print encode('UTF-8', "$line\t$noun_list\n");
}
close IN;