100分求一段perl代码改为python或.NET

menuvb menuvb 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#!/usr/bin/perl

( $words, $pos, $ngram, $lex, $hmm ) = @ARGV;

open WORDS, $words or die "Can not open the $words file!\n";
open POS, $pos or die "Can not open the $pos file!\n";
open NGRAM, $ngram or die "Can not open the $ngram file!\n";
open LEX, $lex or die "Can not open the $lex file!\n";
open HMM, " > $hmm " or die "Can not open the $hmm file\n";

%wordkey = {};
$wnum = 0;
while( <WORDS> )
{
chomp;
$wnum++;
@wk = split;
$wordkey{$wk[1]} = $wk[0];
}
print HMM "M= $wnum\n";
close WORDS;

%poskey = {};
$pnum = 0;
while( <POS> )
{
chomp;
$pnum++;
@pk = split;
$poskey{$pk[1]} = $pk[0];#4 vb
}
print HMM "N= $pnum\n";
close POS;

@unipos = ();
@bipos = ();
while ( <NGRAM> )
{
chmop;
@png = split;
$n = @png;
if ( $n == 2 )
{
$unipos[$poskey{$png[0]}] = $png[1];#vb 100
}
if ( $n == 3 )
{
$bipos[$poskey{$png[0]}][$poskey{$png[1]}] = $png[2];
}
}
close NGRAM;

print HMM "A:\n";
foreach $i ( 1..$pnum )
{
foreach $j ( 1..$pnum )
{
$p = ( $bipos[$i][$j] + 1 ) / ( $unipos[$i] + $pnum ) ;
printf HMM ("%5.4f ", $p);
}
print HMM "\n";
}

print HMM "B:\n";
@wordpos = ();
while ( <LEX> )
{
chmop;
@wpn = split;
$wordpos[$poskey{$wpn[1]}][$wordkey{$wpn[0]}] = $wpn[2];
}
close LEX;

foreach $i ( 1..$pnum )
{
foreach $j ( 1..$wnum )
{
$p = ( $wordpos[$i][$j] + 1 ) / ( $unipos[$i] + $wnum ) ;
printf HMM ("%5.4f ", $p);
}
print HMM "\n";
}

print HMM "pi:\n";
$sum = 0;

shift ( @unipos );
foreach $n ( @unipos )
{
$sum += $n;
}

foreach $n ( @unipos )
{
$p = $n / $sum;
printf HMM ("%5.4f ", $p);
}
print HMM "\n";

大概100行左右的perl,因为需要了解这段代码的意思,可以改为python或.NET。主要不理解perl中%wordkey = {}指什么类型?

$poskey{$pk[1]} = $pk[0];又有什么用处,好像perl的数据也是用[]用表达的。
非常感谢!
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
menuvb
脚本语言 37,737 成员 34,208 内容