Use Google as a Spellchecker with Perl

Try googling for “recieve”, and you will receive the following messages above the results:

Did you mean: receive

This feature can be used to check texts for commonly misspelled words, and doing so is especially easy since the correctly spelled version is always highlighted with B and I elements. The following perl code uses LWP to accomplish the task.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/perl
 
use strict;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
$ua->agent('Perl 5');
my $text = 'You will recieve good resullts.';
my @newtext;
foreach my $word ( split / /, $text ) {
    my $req = HTTP::Request->new( GET => 'http://google.com/search?q=' . $word );
    my $res = $ua->request( $req );
    if ( $res->content =~ m/<b><i>(.*?)<\/i><\/b>/i ) {
        push @newtext, $1;
    } else {
        push @newtext, $word;
    }
    sleep 1;
}
print "@newtext\n";

The output of the above script is: you will receive good results. Note, however, that this script is quite useless since abusing the Google servers like this is against their terms of service and there are better spell checkers available.

Maybe Related?

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

FireStats iconAnvänder FireStats