私は、aがテキストを処理して辞書に単語の頻度を与え、次に辞書を並べ替えるべきperlスクリプトを書いています。テキストはEdgar Poeの "Golden Bug"の抜粋です。目的はすべての単語の頻度を計算することです。しかし、私は出力が得られないので間違っています。いつ私は間違っているのですか?ありがとう。単語の頻度を数えて並べ替えます。
open(TEXT, "goldenbug.txt") or die("File not found");
while(<TEXT>)
{
chomp;
$_=lc;
s/--/ /g;
s/ +/ /g;
s/[.,:;?"()]//g;
@word=split(/ /);
foreach $word (@words)
{
if(/(\w+)'\W/)
{
if($1 eq 'bug')
{
$word=~s/'//g;
}
}
if(/\W'(\w+)/)
{
if(($1 ne 'change') and ($1 ne 'em') and ($1 ne 'prentices'))
{
$word=~s/'//g;
}
}
$dictionary{$word}+=1;
}
}
foreach $word(sort byDescendingValues keys %dictionary)
{
print "$word, $dictionary{$word}\n";
}
sub byDescendingValues
{
$value=$dictionaty{$b} <=> $dictionary{$a};
if ($value==0)
{
return $a cmp $b
}
else
{
return $value;
}
}
小さな単語リストも投稿できますか?あなたはどこでも%辞書を宣言していません... –