0
我想知道是否有人可以帮助解释下面的代码片段中发生了什么,因为我正在尝试翻译成Java,但我的Perl知识很小。Perl到Java的翻译
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;
}
看一眼,这似乎构建[马尔可夫链](http://en.wikipedia.org/wiki/Markov_chain)。如果这是在java中做到这一点的最好方法是有争议的。 – 2012-07-26 13:57:25
%dict的定义在哪里? – 2012-07-26 13:57:38
是的,它是一个马尔可夫链。原始脚本可以在这里找到http://www.lab6.com/old/niall-perl.html – veryphatic 2012-07-26 14:13:06