#KWIC形式の出力をする #製作 T. Takahashi #kwic.pl ############### #メインルーチン #ファイル名の指定 print "開くファイル名を指定してください:"; $open_file = ; print "保存するファイル名を指定してください:"; $save_file = ; print "探索する文字列を指定してください:"; $word = ; chop ($open_file, $save_file, $word); #ファイルのオープン open(F, $open_file) || die "$open_fileをオープンできません:$!\n"; open(F2, ">$save_file") ||die "$save_fileを作成できません:$!\n"; #デフォルトの設定 $length = 30; $allcontextlength = $length * 2; $space = ' '; $linenumber = "line:"; $add = length($word); for ($i = 0; $i <$length; $i++) { $allcontext = $space . $allcontext; } &input; while (1) { $target = substr($allcontext, $length, $length); if (index($target, $word) == 0) { &output; } #1文字ずらす $allcontext = substr($allcontext, 1); if (length($allcontext) < $length) { exit (0); } if (length($allcontext) < $length * 2) { &input; } } close(F); close(F2); #メインルーチン終わり ##################### #行の読み込み sub input { while (length($allcontext) < $allcontextlength) { if ($line = ) { #loci markerを削除 $line =~ s/<.+>//g; #単語の区切りをピリオドからスペースへ変更 $line =~ s/\./ /g; #改行を
に変換 $line =~ s/\n/
/g; $count++; $allcontext = $allcontext . $line; } else { return (0); } } } #結果の出力 sub output { $pre = substr($allcontext, 0, $length); $post = substr($allcontext, $length, $length+$add); printf F2 "%s%-5s %s%s\n", $linenumber, $count, $pre, $post; }