All pastes #618290 Raw Edit

Mine

public text v1 · immutable
·published 2007-07-13 21:30 UTC
rendered paste body
#!/usr/bin/perl
########################################NEED libwww-perl libxml-parser-perl INSTALLED##################
#* Copyright (c) 2007, Ed Holland
#* All rights reserved.
#*
#* Redistribution and use in source and binary forms, with or without
#* modification, are permitted provided that the following conditions are met:
#*     * Redistributions of source code must retain the above copyright
#*       notice, this list of conditions and the following disclaimer.
#*     * Redistributions in binary form must reproduce the above copyright
#*       notice, this list of conditions and the following disclaimer in the
#*       documentation and/or other materials provided with the distribution.
#* THIS SOFTWARE IS PROVIDED BY ED HOLLAND ``AS IS'' AND ANY
#* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
#* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#* DISCLAIMED. IN NO EVENT SHALL ED HOLLAND BE LIABLE FOR ANY
#* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
########################################
#Example XML data returned from server (with added newlines)
#<?xml version="1.0"?>
#<spellresult error="0" clipped="0" charschecked="12">
#<c o="0" l="3" s="1">This  Th's    Thus    Th      HS</c>
#<c o="9" l="3" s="1">test      tat     ST      St      st</c>
#</spellresult>
#########################################
use LWP::UserAgent;
use XML::Parser;
use Irssi qw(signal_add_last);
use vars qw($VERSION %IRSSI);

$VERSION = '0.10';
%IRSSI = (
	authors     => 'Ed Holland',
	contact     => 'edholland@ntlworld.com',
	name        => 'Google Spell Check',
	description => 'This script preforms ' .
			'a google spell check' .
			'operation on your text.',
	license     => 'New BSD',
);

sub sig_complete {
	my ($complist, $window, $word, $linestart, $want_space) = @_;
	$line = $word;
	process();
	push @$complist, $line;
}


sub process {
	$ua = LWP::UserAgent->new;
	
	my $req = HTTP::Request->new(POST => 'https://www.google.com/tbproxy/spell?lang=en&amp;hl=en');
	$req->content_type('text/xml');
	$req->content('<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="1" ignoredups="0" ignoredigits="1" ignoreallcaps="0"><text>'.$line.'</text></spellrequest>');

	my $res = $ua->request($req);
	$string = $res->as_string;
	@data = split('\n\n', $string);

	# initialize parser
	$xp = new XML::Parser();
	# set callback functions
	$xp->setHandlers(Start => \&start, Char => \&cdata);
	# parse XML
	$xp->parse($data[1]);
}

sub start { 
	# extract variables
	my ($parser, $name, @attr) = @_;
	$currentTag = lc($name); 
	$skip = 0;
	if (($currentTag eq 'c') && ($attr[5] == 0)) { 	$skip = 1; }
}
sub cdata { 
	my ($parser, $cdata) = @_;
	@cdata = split('\t',$cdata);	
	if ($skip != 1) { $line = $cdata[0] }
}

signal_add_last 'complete word' => \&sig_complete;