]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/spell.pl
705854dfdde8913266c9c61d6cd7e46697c665ec
[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         if ($query =~ m/[^[:alpha:]]/) {
18                 return('only one word of alphabetic characters supported');
19         }
20
21         my $binary;
22         my @binaries = (
23                 '/usr/bin/aspell',
24                 '/usr/bin/ispell',
25                 '/usr/bin/spell'
26         );
27
28         foreach (@binaries) {
29                 if (-x $_) {
30                         $binary=$_;
31                         last;
32                 }
33         }
34
35         if (!$binary) {
36                 return('no binary found.');
37         }
38
39         if (!&::validExec($query)) {
40                 return('argument appears to be fuzzy.');
41         }
42
43         my $reply = "I can't find alternate spellings for '$query'";
44
45         foreach (`/bin/echo '$query' | $binary -a -S`) {
46                 chop;
47                 last if !length;                # end of query.
48
49                 if (/^\@/) {            # intro line.
50                         next;
51                 } elsif (/^\*/) {               # possibly correct.
52                         $reply = "'$query' may be spelled correctly";
53                         last;
54                 } elsif (/^\&/) {               # possible correction(s).
55                         s/^\& (\S+) \d+ \d+: //;
56                         my @array = split(/,? /);
57
58                         $reply = "possible spellings for $query: @array";
59                         last;
60                 } elsif (/^\+/) {
61                         &::DEBUG("spell: '+' found => '$_'.");
62                         last;
63                 } elsif (/^# (.*?) 0$/) {
64                         # none found.
65                         last;
66                 } else {
67                         &::DEBUG("spell: unknown: '$_'.");
68                 }
69         }
70
71         return($reply);
72 }
73
74 sub spell::query {
75         &::performStrictReply(&spell(@_));
76         return;
77 }
78
79 1;
80 # vim: ts=2 sw=2