1 # -*- Mode: perl; tab-width: 4; indent-tabs-mode: nil; -*-
2 ################################
3 # Spell Checker Module #
4 ################################
6 package BotModules::Spell;
11 # XXX Ideally we should move to using www.dict.org
17 '' => 'This module checks for spelling errors.',
18 'sp' => 'If you aren\'t sure of the spelling of a word, append \'(sp)\' to the word, and it will be checked for you. '.
19 'For example: \'My speling (sp?) is awful!\''
25 my ($event, $message) = @_;
26 if ($self->checkSpelling($event, $message)) {
27 # we checked the spelling, abort
30 return $self->SUPER::Heard(@_);
35 my ($event, $text) = @_;
36 $self->checkSpelling($event, $text);
37 return $self->SUPER::Heard(@_);
42 my ($event, $text) = @_;
43 while ($text =~ s/^.*? # take everything up to the first word to check
44 \b # look for a word break
45 (\w+) # take the word to spell
46 \s* # look for whitespace following it
47 \(sp\??\) # followed by (sp) or (sp?)
48 //isox) { # and remove everything up to here so we can do another check in a minute
51 $self->getURI($event, "http://www.merriam-webster.com/dictionary/$word", 'word', $1); # XXX should be configurable!
59 my ($event, $query, $result, $command, $word) = @_;
60 if ($command ne 'word') {
61 return $self->SUPER::GorURI(@_);
64 # Determine if page is error or not
65 if (!length($result)) {
66 $self->debug("Waah, failed utterly to get a response for '$word' from the dictionary server.");
67 $reply = "The dictionary service is not accessible right now, sorry.";
68 } elsif ($result =~ / # Match
69 The\ word\ you've\ entered\ # literal string
70 isn't\ in\ the\ dictionary\. # (not very smart),
71 .*? # anything (non-greedy),
73 (.*?) # our suggestions,
77 || $result =~ / # Match
78 The\ word\ you've\ entered\ # literal string
79 isn't\ in\ the\ dictionary\. # (not very smart),
80 .*? # anything (non-greedy),
82 (.*?) # our suggestions,
85 # XXX this is hardcoded to m-w.com!
87 # Strip line numbering and anchor tags
89 $suggestions =~ s/\s+[\d]+\.\s+//go;
90 $suggestions =~ s/<a href.*?>(.*?)<\/a>/$1 /go;
91 $suggestions =~ s/<li>(.*?)<\/li>/$1 /go;
93 # get them in list format
94 my @suggestions = split(' ', $suggestions);
96 # Comma delimit suggestions
98 if (@suggestions > 7) {
99 # lots of suggestions!
100 # 7 is not arbitrary, it's supposed to be the number
101 # of items people can remember at once.
102 @suggestions = @suggestions[0..6];
103 $reply = "Suggestions for '$word': @suggestions[0..6]...";
104 } elsif (@suggestions) {
105 # just a few suggestions
106 $reply = "Suggestions for '$word': @suggestions";
108 # eh? Weird. Some problem on the server probably.
109 $self->debug("Didn't get any suggestions for '$word'!");
110 $reply = "I have no idea what '$word' is supposed to be, sorry.";
114 $reply = "'$word' seems to be the correct spelling.";
116 $self->say($event, $reply);