- 论坛徽章:
- 0
|
11楼
发表于 2014-08-07 14:10
帮我看看中英文每句对齐程序怎么改,另外你的代码有些我没看懂,还有你的思路是什么,可以讲讲看吗 ?
use IO;
use 5.10.1;
use warnings;
use strict;
my $usage = "Bilingual_aligned.pl input1_text input2_text output_test \n";
(@ARGV == 3) || die $usage;
my ($fn1_IN,$fn2_IN,$fn_OUT) = @ARGV;
chomp($fn1_IN);
chomp($fn2_IN);
chomp($fn_OUT);
my( $INCH,$INEN,$OUT);
my($lines,@EN,@CH);
my %dic = qw[
government 政府 Islamist 伊斯兰 eastern 东部 killed 丧生 city 城市 leader 领导人 war 内战 United Nations 联合国 Turkey 土耳其
diplomatic staff 外交人员 recommended 建议 commercial 商业 Britain 英国 Egypt 埃及 state 国家 media 电台 reporting 报道 deaths 丧生
Egyptians 埃及人 Libyan 利比亚 capital 首都 Tripoli 的里波里 rocket 火箭 hit 击中 home 住所 near 附近 airport 机场
fighting 激战 rival 对立 militias 民兵 entering 进入 third 第三 week 星期
];
open($INCH,"< $fn1_IN ") || die " failed to open the input file1";
open($INEN,"< $fn2_IN ") || die " failed to open the input file2";
open($OUT, "> $fn_OUT") || die " failed to open the output file";
while($lines=<$INCH>){
$/ = '。';
@CH=$lines;
#print @CH;
}
while($lines=<$INEN>){
$/ = '.';
@EN=$lines;
#print @EN;
}
close($INCH);
close($INEN);
my $i = 0;
for my $e (@EN) {
my $begin = $i;
my @sentence = map { [/\w+/g] } split /,/, $e; #将读入的每句话按照逗号进行切分然后赋给数组
for my $ws (@sentence) {
my $index = $#{$ws} >= 3 ? [ 0, $#{$ws} / 2, -1 ] : [ 0, -1 ];
my @word = @$ws[@$index];
my $ok = 0;
say "CHECK: [ ", join( '|', @word ) . ' ]';
NEXT: for my $w (@word) {
++$ok and next unless exists $dic{$w}; #如果在dic中存在中文就继续执行否则跳出
my $c = $dic{$w}; #将字典中的值保存在$c中
if ( $CH[$i] =~ /$c/ ) { #如果中文中的句子与字典里的中文相匹配
say "$w\t$c = ok"; #字典中的键 值 =ok ,说明成功匹配
$ok++;
}
}
unless ( $ok > @word * 2 / 3 ) {
say "FAIL !!\tcheck next sentence";
++$i and goto NEXT;
}
sleep 1;
}
print '=' x 24;print "\n";
print "match\t\@CH[ $begin .. $i ]";print "\n";
print '-' x 24;print "\n";
print $e;print "\n";
print @CH[ $begin .. $i++ ];print "\n";
print '-' x 24;print "\n";
}
回复 8# pitonas
|
|