package Bot::BasicBot::Pluggable::Module::LinkParser;

use base 'Bot::BasicBot::Pluggable::Module::Base';
use Lingua::LinkParser;
use Lingua::Wordnet;
use strict;

our $parser = new Lingua::LinkParser;
our $wordnet = new Lingua::Wordnet;

sub said {
    my ( $self, $msg, $prio ) = @_;

    return unless $msg->{address} and $prio == 1;

    if ($msg->{body} =~ /^!(\S+)\s+([^\.]+)\.(\w)$/o) {
	my $what   = $1;
	my $query  = Lingua::Wordnet::Synset->can($what) or return "WTF dude?";
	my $word   = $2;
	my $pos	   = $3;
	
	my ($synset) = $wordnet->lookup_synset($word, $pos);
	return ($synset->words)[0] . " $what " 
	    . join(", ", map($_ ? $_->words : (), $synset->$query));
    }

    my $sentence = $parser->create_sentence( $msg->{body} );
    my @linkages = $sentence->linkages;
    my @words;

    $words[$_] = $sentence->get_word($_) for 0 .. $sentence->length;

    for my $linkage (@linkages) {
	for my $word ($linkage->words) {
	    $words[$word->position] = [ $1, $2 ]
		if $word->text =~ /^(\S+)\.(\w)+$/o;
	}
	# push @diagram, $parser->get_diagram($linkage);
    }

    for my $n (0 .. $#words) {
	next unless ref $words[$n];
	my @synsets = $wordnet->lookup_synset(@{$words[$n]});
	$words[$n] = "[" . ($synsets[0]->words)[0] . ".$words[$n][1]]" 
	    if @synsets;
    }

    ref($_) and $_ = "[" . join(".",@$_) . "]" for @words;
    return join(" ", grep(!/-WALL$/o, @words)); 
}   

