]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/spell.pl
no main:: if not needed
[infobot.git] / src / Modules / spell.pl
1 #
2 #   spell.pl: interface to aspell/ispell/spell
3 #        Author: Tim Riker <Tim@Rikers.org>
4 #        Source: extracted from UserExtra
5 #  Licensing: Artistic License (as perl itself)
6 #       Version: v0.1
7 #
8 #  Copyright (c) 2005 Tim Riker
9 #
10
11 package spell;
12
13 use strict;
14
15 sub spell::spell {
16         my $query = shift;
17         my $binary;
18         my @binaries = (
19                 '/usr/bin/aspell',
20                 '/usr/bin/ispell',
21                 '/usr/bin/spell'
22         );
23
24         foreach (@binaries) {
25                 if (-x $_) {
26                         $binary=$_;
27                         last;
28                 }
29         }
30
31         if (!$binary) {
32                 return("no binary found.");
33         }
34
35         if (!&::validExec($query)) {
36                 return("argument appears to be fuzzy.");
37         }
38
39         my $reply = "I can't find alternate spellings for '$query'";
40
41         foreach (`/bin/echo '$query' | $binary -a -S`) {
42                 chop;
43                 last if !length;                # end of query.
44
45                 if (/^\@/) {            # intro line.
46                         next;
47                 } elsif (/^\*/) {               # possibly correct.
48                         $reply = "'$query' may be spelled correctly";
49                         last;
50                 } elsif (/^\&/) {               # possible correction(s).
51                         s/^\& (\S+) \d+ \d+: //;
52                         my @array = split(/,? /);
53
54                         $reply = "possible spellings for $query: @array";
55                         last;
56                 } elsif (/^\+/) {
57                         &::DEBUG("spell: '+' found => '$_'.");
58                         last;
59                 } elsif (/^# (.*?) 0$/) {
60                         # none found.
61                         last;
62                 } else {
63                         &::DEBUG("spell: unknown: '$_'.");
64                 }
65         }
66
67         return($reply);
68 }
69
70 sub spell::query {
71         &::performStrictReply(&spell(@_));
72         return;
73 }
74
75 1;
76 # vim: ts=2 sw=2