#!/usr/local/bin/jperl
#
# tscsgml.pl
#
# tscsgml ver 1.12
# 2000.08.7 coded by Hashimoto
# last modified by motizuki (Aug. 23. 2000)
#
# ex.
# ./tscsgml.pl < mai94.sgml > mai94.tsc
#
# ./tscsgml.pl -L artsid.94 < mai94.sgml
# ./tscsgml.pl -L artsid.95 < mai95.sgml
# ./tscsgml.pl -D artsid.94 < mai94.sgml > artsid.94.tsc
#
# Options
#  -L <file>
#      file内で指定された記事のみをタグ付けして，別ファイルに出力．
#  -D <file>
#      file内で指定された記事のみをタグ付けして，標準出力．
#
#     <file>は，<DOCNO>9999999</DOCNO> の形で記述すること．
#
#  文区切りルール
#  ◯ 読点で文を区切る
#  ◯ ▽で文を区切る
#  ◯ 但し、引数-sが指定されていれば(されていなくても，に変更)、
#     カギ括弧内の句点は無視する
#
#  パラグラフ区切りルール
#  ◯ 改行があればパラグラフの終了と見なす
#  ◯ ▲もパラグラフの区切りと見なす
#
#  その他のルール
#  ◯ 行頭の空白は削除する
#  ◯ ◇だけの行は削除する
#  ◯ (？面に関連記事)という個所は必ず一つのパラグラフにする
#
#  ○ （？面に関連？）という個所は必ず一つのパラグラフにする
#
#

#$argv = $ARGV[0] ; # 引数を受け取る

require "getopts.pl";
&Getopts('shD:L:');

$dct=0;

#helpの処理;
if ($opt_h) {
    &usage;
}
#Lでファイルが指定されている時;
if ($opt_L||$opt_D) {
    if ($opt_L) {
	$rfile=$opt_L;
	$exFlag=1;
    } else {
	$rfile=$opt_D;
	$exFlag=2;
    }
    open(LF, $rfile) || die "cannot open $rfile for read\n";
    while($line=<LF>) {
	chomp($line);
	@a = split(/</, $line);
	@b = split(/>/, $a[1]);
	$exFile{"$line"}=$b[1];
	$dct++;
    }
    close(LF);
    $activeArt=0;
} else {
    $exFlag=0;
}

while( <stdin> ) {
    if ($exFlag>=1 && $activeArt==0) {
	chomp;
	if ($exFile{"$_"}=~/./) {
	    $activeArt=1;
	    if ($exFlag==1) {
		$wfile=$exFile{"$_"} .".tsc";
		open(W, ">$wfile") || die "cannot open $wfile\n";
		printf (W "<DOC>\n");
	    } else {
#		printf ("FILENAME=%s.tsc\n", $exFile{"$_"});
		printf ("<DOC>\n");
	    }
	    $_ = $_ . "\n";
	}
    }
    if ($exFlag==0 || $activeArt==1) {

	if( /<TEXT>/ ) { # <TEXT>を含む行がきたら...
	    if ($exFlag==0||$exFlag==2) {
		print ; # まず、<TEXT>を出力し、
	    } else {
		printf (W "%s", $_);
	    }
	    while( <stdin> ) { # 次の行を読み、
		unless( /<\/TEXT>/ ) { # </TEXT>が来るまで以下の処理をする
		    # 一行中の行頭に対して、0個以上の全角空白を削除し、
		    # <PARAGRAPH>\n<SENTENCE>を挿入
		    s/^　*/<PARAGRAPH>\n<SENTENCE>/ ;

		    # 読点の後に</SENTENCE>\n<SENTENCE>を挿入。
		    s/。/。<\/SENTENCE>\n<SENTENCE>/g ;

#		    # ▽の後に</SENTENCE>\n<SENTENCE>を挿入。
#		    s/▽/▽<\/SENTENCE>\n<SENTENCE>/g ;
		    # ▽の前に</SENTENCE>\n<SENTENCE>を挿入に変更;
		    s/▽/<\/SENTENCE>\n<SENTENCE>▽/g ;

		    # 引数-sが指定されていれば、カギ括弧内の読点は無視する
#		    if( $argv eq "-s" ) {
		    if( $opt_s || !$opt_s) {
			if( /[「」]/ ) {
			    @parts = split /([「」])/ , $_ ;
			    foreach $part (@parts) {
				if( $part eq '「' ) {
				    $depth++ ;
				}
				elsif( $part eq '」' ) {
				    $depth-- ;
				}
				elsif( $part=~m/。<\/SENTENCE>\n<SENTENCE>/g and $depth > 0 ) {
				    $part =~ s/。<\/SENTENCE>\n<SENTENCE>/。/g ;
				}
				$line .= $part ;
			    }
			    $_ = $line ;
			    $line = "" ;
			}
		    }

		    # 行末に</SENTENCE>\n</PARAGRAPH>\nを挿入。
		    s/\n$/<\/SENTENCE>\n<\/PARAGRAPH>\n/ ;

		    # ▲もパラグラフの区切りと見なす
#		    s/▲/▲<\/SENTENCE>\n<\/PARAGRAPH>\n<PARAGRAPH>\n<SENTENCE>/g ;
		    # ▲を文頭に持っていくように変更;
		    s/▲/<\/SENTENCE>\n<\/PARAGRAPH>\n<PARAGRAPH>\n<SENTENCE>▲/g ;

		    # 行末の"(？面に関連記事)"を必ず一つのパラグラフにする
#		    s/<\/SENTENCE>\n<SENTENCE>（(.+)面に関連記事）<\/SENTENCE>\n<\/PARAGRAPH>/<\/SENTENCE>\n<\/PARAGRAPH>\n<PARAGRAPH>\n<SENTENCE>（$1面に関連記事）<\/SENTENCE>\n<\/PARAGRAPH>/ ;
		    s/<\/SENTENCE>\n<SENTENCE>（(.+)面に関連(.+)）<\/SENTENCE>\n<\/PARAGRAPH>/<\/SENTENCE>\n<\/PARAGRAPH>\n<PARAGRAPH>\n<SENTENCE>（$1面に関連$2）<\/SENTENCE>\n<\/PARAGRAPH>/ ;

		    # "...の話　ほにゃらら，ほにゃらら.." のパターンに対応
		    s/話　/話<\/SENTENCE>\n<SENTENCE>/ ;

		    # 行末が読点の場合、<SENTENCE></SENTENCE>という行が
		    # 出来てしまうので、これを削除(汚い)
		    s/<SENTENCE><\/SENTENCE>\n//g ;

		    # ◇だけの行を削除
		    s/<PARAGRAPH>\n<SENTENCE>◇<\/SENTENCE>\n<\/PARAGRAPH>\n// ;
		    #このパターンも削除
		    s/<PARAGRAPH>\n<\/PARAGRAPH>// ;

		    if ($exFlag==0||$exFlag==2) {
			print ;
		    } else {
			printf (W "%s", $_);
		    }
		}
		else { # </TEXT>が来たら出力し、whileを抜ける
		    if ($exFlag==0||$exFlag==2) {
			print ;
		    } else {
			printf (W "%s", $_);
		    }
		    last ;
		}
	    }
	} else { # その他の行はそのまま出力
	    if ($exFlag==0||$exFlag==2) {
		print ;
	    } else {
		printf (W "%s", $_);
	    }
	    if ($exFlag>=1 && $_ =~/<\/DOC>/) {
		if ($exFlag==1) {
		    close(W);
		}
		$activeArt=0;
		$dct--;
		if ($dct==0) {
		    exit;
		}
	    }
	}
    }
}
exit;

sub usage {
    print <<"USAGE";
usage : $0 [-sh -L<listfile>]
  -L<file> Only the articles specified in the <file>
           is tagged and output each tagged article to each `*.tsc' file.
  -D<file> Only the articles specified in the <file>
           is tagged and output to standard output.
  -h     help
USAGE
    exit;
}
