[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Dict for 0.44.3



Attached is the diff from Statement.pl.  This is the new version which
allows using any of the dictionaries, and support for close matches if the
word isn't found.  I also added in a splitit() function which splits it
under the IRC message limit (I think I hardcoded 476), with trailing (and
prepended) "..." to denote multi-line response.

In addition to this you also have to modify the infobot BEGIN section to
"use Net::Dict".

I noticed that this version is MUCH slower than the previous version,
there are more function calls, and a lot of time spent "doing the right
thing", but the outcome is worth it in my opinion.  

Jay
(teckle)

On Tue, 28 Dec 1999, bounty wrote:

> can i use this on my infobot 0.44.3..and what/where would i put it so that
> it works..i already installed Net::Dict. please advise..thanks for the
> help.
> 
> john
> 
> 
34,79d33
<     if ($in=~/^define (\S+)(\s+using\s+)?(\S+)?/i) {
<       my $word = $1;
<       my $dict = $3;
<       $dict = "wn" if (! defined $dict);
<       my $server = Net::Dict->new("dict.org");
<       my $h = $server->define($word);
<       my ($i, $Ret, $dct, $def);
<       my $found = 0;
<       my @other_matches;
<       if (! defined @{$h}) {
<         # $self->status(3, "No match Found at all, looking up alternatives");
<         $Ret = findmatch($server, $dict, $word);
<       } else {
< 	foreach $i (@{$h}) {
< 	  ($dct, $def) = @{$i};
< 	  if ($dct eq $dict) {
< 	    foreach (split(/\n/, $def)) {
< 	      $_=~s/\s{3,}//g;
< 	      $Ret.=$_." ";
< 	    }
< 	  } else {
< 	    push(@other_matches, $dct);
< 	  }
< 	}
<         if (! $Ret) {
<           # $self->status(3, "match found in other dict, looking up alternatives");
<           $Ret = findmatch($server, $dict, $word, @other_matches);
<         }
<       }
<       my @jout = splitit($Ret);
<       if ($msgType eq 'public') {
<         for (0..($#jout-1)) {
<           &say($jout[$_]);
<           sleep 2;
<         }
<         &say($jout[$#jout]);
<       } else {
<         for (0..($#jout-1)) {
<           &msg($who, $jout[$_]);
<           sleep 2;
<         }
<         &msg($who, $jout[$_]);
<       }
<       return 'NOREPLY';                                                   
<     }
< 
252,315d205
< sub splitit {
<   my $msg = shift;
<   my $c;
<   my @out;
<   my $org = length($msg);
<   while (length($msg) > 473) {
<     for ($c=473; $c>1; $c--) {
<       if (substr($msg, $c, 1) eq " ") {
<         push(@out, substr($msg, 0, $c)."...");
<         $msg="...".substr($msg, ($c+1));
<         last;
<       }
<     }
<     if (length($msg) == $org) {
<       push(@out, substr($msg, 0, 473)."...");
<       $msg="...".substr($msg, 473);
<     }
<     $org = length($msg);
<   }
<   push(@out, $msg);
<   return (@out);
< }
< 
< sub findmatch {
<   my $server = shift;
<   my $dict = shift;
<   my $word = shift;
<   my @other_matches = @_;
<   my @others;
<   my ($h, $i, $dct, $def);
<   $h = $server->match($word, "lev");
<   foreach $i (@{$h}) {
<     ($dct, $def) = @{$i};
<     push (@others, $def) if ($dct eq $dict);
<   }
<   if (@others) {
<     my $str = gimmePretty(@others);
<     if (@other_matches) {
<       $str.="; or try using: ".(gimmePretty(@other_matches));
<     }
<     return("$word: No Match. Perhaps you mean: $str");
<   } elsif (@other_matches) {
<     return("$word: No match, try using ".(gimmePretty(@other_matches)).".");
<   } else {
<     return "$word: No matches found";
<   }
< }
< 
< sub gimmePretty {
<   my @words = @_;
<   my $str = "";
<   if ($#words>=1) {
<     for (0..($#words-1)) {
<       $words[$_]=~s/"//g;
<       $str .= " $words[$_],";
<     }
<     chop($str);
<     $str .= " or ";
<   }
<   $words[$#words]=~s/"//g;
<   $str .= $words[$#words];
<   return($str);
< }
<