/
dfw_spellerbot.pl
executable file
·109 lines (76 loc) · 3.21 KB
/
dfw_spellerbot.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env perl
# ------------------------------------------------------------
# THE SETTINGS (adjust as desired)
# ------------------------------------------------------------
# IRC server to which the bot will connect
our $server = 'irc.perl.org';
# Channels on the server which the bot will join
our @channels = qw( #bot-test );
# IRC server username
our $user = undef, # no auth at irc.perl.org
# desired IRC nicknames, which will be tried in the order they appear
our @nicks = qw( spellcheck spellerbot speller_bot dfw_spellerbot );
# IRC name
our $name = 'DFW Perl Monger Spellchecker Bot';
# ------------------------------------------------------------
# THE SETUP
# ------------------------------------------------------------
use Modern::Perl; # turn on strict, warnings, and goodies
package DFWpm::SpellerBot; # this is a spellchecker IRC bot
use Moo; extends 'Bot::BasicBot'; # the proper way to subclass
use Try::Tiny; # save the bot if the bot short circuits! (with try {} catch {})
use Lingua::Ispell qw( spellcheck ); # you need aspell on your system first
Lingua::Ispell::allow_compounds( 0 ); # << doesn't work in modern aspell anyway
# ------------------------------------------------------------
# THE BOT
# ------------------------------------------------------------
# run the bot, connect to the irc server, use the nicks above in the order
# they appear, join channels, listen for commands. See perldoc Bot::BasicBot
DFWpm::SpellerBot->new(
server => $server,
channels => \@channels,
nick => shift @nicks,
alt_nicks => \@nicks,
username => $user,
name => $name,
)->run();
exit;
# ------------------------------------------------------------
# THE GEARS
# ------------------------------------------------------------
sub said
{
my ( $self, $said ) = @_;
my ( $who, $what, $to ) = @{ $said }{ qw( who body address ) };
# don't speak unless spoken to:
return unless $to && $to eq $self->nick;
# just check one word. If there's more than one, refuse to check:
return "I only check one word at a time, $who"
if $what =~ /[[:space:]]/;
try # Lingua::Ispell can produce strange errors sometimes...
{
my ( $check ) = spellcheck( $what );
# ^ ^ # apparently depends on list context. That's weird.
# see perldoc Lingua::Ispell for info on the way these lines
# work below
return "'$what' looks perfectly cromulent to me. Nice spelling, $who"
unless $check || ( $check && $check->{type} eq 'ok' );
my $suggest = join ', ',
map { qq("$_") }
@{ $check->{misses} },
@{ $check->{guesses} };
return "Did you mean one of these? $suggest"
if $check->{type} eq 'miss' or $check->{type} eq 'guess';
return "'$check->{term}' is an embiggened compound word."
if $check->{type} eq 'compound';
return "'$check->{term}' can be formed from root '$check->{root}'."
if $check->{type} eq 'root';
return "I have no idea what to do with that nonsense, $who"
if $check->{type} eq 'none';
}
catch # ... so we can catch the errors with Try::Tiny
{
return "Something wicked happened: '$_'"
}
}
1;