现在的位置: 首页 > 综合 > 正文

为译文信息打分的程序

2014年01月25日 ⁄ 综合 ⁄ 共 4325字 ⁄ 字号 评论关闭

#!usr/bin/perl

#
use strict;    # always use strict, it's a good habit

######
# Intro
my ($date, $time) = date_time_stamp();
print "MT evaluation run on $date at $time/n";
print "command line:  ", $0, " ", join(" ", @ARGV), "/n";
my $usage = "/n/n Usage: $0 [-h]  [-s score range ] [-r ref FILENAME] [-t translation FILEHANDLE]/n".
"-h                :打印帮助信息/n".
"-c                :打印需要保存的bleu分值范围,“0.8”表示大与0.8, 不给任何参数时全部打印/n".
"-r  ref_file     :参考译文文件路径/n".
"-t  trans_file  :译文信息文件路径/n".
"-o result_file  :结果文件路径/n".
"/n";
use vars qw ($opt_r $opt_t $opt_h $opt_c  $opt_o);
use Getopt::Std;   # see "perldoc Getopt::Std"
my %options;
getopts('r:t:hc:o:', /%options);  # read the options with getopts

# uncomment the following two lines to see what the options hash contains
#use Data::Dumper;
#print Dumper /%options;

$options{h} && usage();   # the -h switch

# use the -t switch, if it's given, or use a default input trans_info  filename
my $trans_info_file = $options{t} || 'trans_info.file';
# use the -r switch, if it's given, or use a default input ref  filename
my $ref_file = $options{r} || 'ref.file';
# use the -c switch, if it's given, or use a default input ref  filename
my $sel_score = $options{c} || '0.0';#默认全部输出
# use the -o switch, if it's given, or use a default input ref  filename
my $result_file = $options{o} || 'out.file';#默认全部输出

print "Translation file is $trans_info_file/n";
print "Ref file is $ref_file/n";
print "Will output the info whose score greater that $sel_score to the file $result_file/n";

#打开输入文件
die $usage if defined($opt_h);
#必须给定
die "Error in command line:  ref_file not defined$usage" unless defined $ref_file;
die "Error in command line:  trans_file not defined$usage" unless defined $trans_info_file;
die "Error in command line:  sel_score not defined$usage" unless defined $sel_score;
open (TRANS_FILE, $trans_info_file) or die "/nUnable to open translation data file $trans_info_file", $usage;

#开始处理输入译文信息文件
my $count =0;
while(!eof(TRANS_FILE)){
          my $line = <TRANS_FILE>;
          $count++;
          if($line=~//#/#/#/#/#/)
          {
           print "A new sentence begin:/n";
          }elsif($line =~//$/$/$/$/$ path_file/)
             {
            print "A path_file begin:/n";
            }
            else{
                my $path = $line  ;
                print "/n path is: $path";
                $count++;
                die "/n在$count 行 发生格式错误/n" unless (!eof(TRANS_FILE));
                my $feature = <TRANS_FILE>;
                print "/n feature is: $feature";
                die "/n在$count 行 发生格式错误/n" unless (!eof(TRANS_FILE));
                my $trans  = <TRANS_FILE>;
                print "/n trans is: $trans";
                   evaluate($path,$feature,$trans,$ref_file,$sel_score,$result_file);
            }
}

########################################

# print out the help and exit
sub usage
{
 print <<EOHELP;
make_corel [-h]  [-s score range ] [-r ref FILENAME] [-t translation FILEHANDLE]
-h                :打印帮助信息
-c                :打印需要保存的bleu分值范围,“0.8”表示大与0.8, 不给任何参数时全部打印
-r ref_file     :参考译文文件路径
-t trans_file  :译文信息文件路径
-o result_file  :结果文件路径
EOHELP
exit;
}

##########################################
## 评测分数

sub evaluate
{
     print "/neval/n";
     my ($path_info,$feature_info,$trans_info,$ref_info,$score_info,$result_file_info) =@_;
     print $path_info,$feature_info,$trans_info,$ref_info,$score_info,$result_file_info;
     my ($Bleu_score);
    
     #打开需要的零时文件,输入必要数据
     open TRANS,">trans.file.temp" or die "can not open file trans.file.temp $!/n";
     open FEATURE,">feature.file.temp" or die "can not open file feature.file.temp $!/n";
     print TRANS $trans_info;
     print FEATURE $feature_info;
     close(TRANS);
     close( FEATURE);
    
     #打开存放最终结果的文件
     open OUT,">>$result_file_info" or die "can not open file $result_file_info $!/n";
    
     #把译文写道$trans_info文件里,调用make_score进行评测,结果在$result_file_info中
     print system("make_score.exe trans.file.temp $ref_info result.file.temp feature.file.temp");
    
     #把结果文件result.file.temp中的结果析取出来
     open RESULT,"result.file.temp" or die "can not open file result.file.temp $!/n";
     my $temp_line = <RESULT>;
     my @temp_scores = split//s+/,$temp_line;
     if($temp_scores[0] >= $score_info)
     {
      print OUT $temp_scores[0],"/n";
      print OUT $path_info;
      print OUT $feature_info;
      print OUT $trans_info;
      close(RESULT);
      close(OUT);
      return ;
     }else
     {
      close(RESULT);
      close(OUT);
      return ;}    
}

#################################

sub date_time_stamp {

    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();
    my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
    my ($date, $time);

    $time = sprintf "%2.2d:%2.2d:%2.2d", $hour, $min, $sec;
    $date = sprintf "%4.4s %3.3s %s", 1900+$year, $months[$mon], $mday;
    return ($date, $time);
}

抱歉!评论已关闭.