我想知道是否有人可以帮助解释以下代码片段中发生的事情,因为我正在尝试翻译成 Java,但我的 Perl 知识很少。
sub burble {
my $cw = "%START%";
my $sentence;
my $nw;
my ( $score, $s, $roll );
while ( $cw ne "." ) # while we're not at the end
# of the sentence yet
{
$score = 0;
# get total score to randomise within
foreach $s ( values %{ $dict{$cw} } ) {
$score += $s;
}
# now get a random number within that
$roll = int( rand() * $score );
$score = 0;
foreach $nw ( keys %{ $dict{$cw} } ) {
$score += ${ $dict{$cw} }{$nw};
if ( $score > $roll ) # chosen a word
{
$sentence .= "$nw " unless $nw eq ".";
$cw = $nw;
last;
}
}
}
return $sentence;
}