perl coup de main
Bonjour,
comme je suis pas spécialiste en programmation perl,
j'arrive pas à trouver la faute :
Use of uninitialized value in string eq at
~/Bayes/SB-score-files.pl line 97.
Unable to open ~/Bayes/tmp/worddb for read at /home/eric/Bayes/
SB-score-files.pl line17.
Voici le listing:
#!/usr/bin/perl -w
use strict;
use Socket;
sub load_wordscores
{
my ($fname) = @_;
open(FILE, "<$fname") || die "Unable to open $fname for read";
my (%rval);
while (<FILE>) {
chomp;
die "bad word score in $fname:\n$_\n" unless /^(\S+) ([\d.]+)$/;
my ($word, $score) = ($1, $2);
die "word $word duplicated in $fname" if (exists($rval{$word}));
$rval{$word} = $score;
}
close(FILE);
return \%rval;
}
sub prod
{
my ($rval, $x) = 1;
foreach $x (@_) {
$rval *= $x;
}
return $rval;
}
sub file_score
{
my ($fname, $scores) = @_;
xx open(FILE, "<$fname") || die "Unable to open $fname for read";
local $/; undef $/;
my $txt = <FILE>;
close FILE;
my (@words) = grep { $_ ne "" } split(/[^-a-z0-9\'\$]+/i, $txt);
if (0) {
my (%words, $word);
foreach $word (@words) {
$words{$word} = 1;
}
@words = keys %words;
}
# print map { "$_ ".$scores->{$_}."\n" } sort { $scores->{$a} <=>
$scores->{$b} } @words;
my (@scores) = sort { abs(0.5 - $b) <=> abs(0.5 - $a) }
# grep { abs (0.5 - $_) < 0.49 }
map { $scores->{$_} || 0.2 } @words;
if (@scores > 15) {
splice(@scores, 15);
}
# print join(", ", @scores), "\n";
my ($badness) = prod(@scores);
my ($goodness) = prod( map { 1-$_ } @scores);
my ($filescore) = $badness / ($badness + $goodness);
# print "$filescore\n";
return $filescore;
}
sub daemon
{
my ($fd, $scores) = @_;
local $/; undef $/;
while (1) {
if (accept(X, $fd)) {
my $fname = <X>;
chomp ($fname);
print "read $fname .\n";
my ($score) = file_score($fname, $scores);
print X "$score\n";
close(X);
} else {
warn "unable to accept";
sleep 2;
}
}
}
######################################################################
xx if ($ARGV[0] eq "-d") {
shift;
my ($path) = $ENV{HOME}."/.SB-sock";
socket(SOCK, PF_UNIX, SOCK_STREAM, 0) || die "Unable to create
socket"; my $sun = sockaddr_un($path);
my ($score);
if (connect(SOCK, $sun)) {
select SOCK; $|=1; select STDOUT;
print SOCK $ARGV[0];
shutdown SOCK, 1;
$score = <SOCK>;
chomp($score);
} else {
unlink($path);
close(SOCK);
socket(SOCK, PF_UNIX, SOCK_STREAM, 0) || die "Unable to create
socket"; my $unbound;
unless (bind(SOCK, $sun)) {
warn "Unable to bind to $path";
$unbound=1;
}
listen(SOCK, 5);
my ($scores) = load_wordscores("~/Bayes/tmp/worddb");
printf "%.3f\n", ($score = file_score($ARGV[0], $scores) );
unless ($unbound) {
print "I should start a daemon\n";
if (1) {
my ($childpid) = fork;
if ($childpid == 0) {
daemon("SOCK", $scores);
exit(1);
}
}
}
}
# print "$score\n";
exit($score < 0.999);
} else {
my ($scores) = load_wordscores("~/Bayes/tmp/worddb");
my ($fname);
foreach $fname (@ARGV) {
printf "%.3f %s\n", file_score($fname, $scores), $fname;
}
}
J'ai mis (xx) où ça coince.
Merci pour l'aide.
--
Amicalement
mess-mate
Reply to: