Lingua-LinkParser-FindPath

 view release on metacpan or  search on metacpan

lib/Lingua/LinkParser/FindPath.pm  view on Meta::CPAN

package Lingua::LinkParser::FindPath;


use strict;

our $VERSION = '0.01';

use fields qw(parser sentence);
use Lingua::LinkParser;
sub new {
    my $class = shift;
    my %arg = @_;
    if( ! ref $arg{parser} ){
        require Lingua::LinkParser;
        $arg{parser} = Lingua::LinkParser->new;
    }
    bless { parser => $arg{parser}, sentence => undef } => $class;
}

sub sentence {
    my $self = shift;
    $self->{sentence} = ref $_[0] ? shift : $self->{parser}->create_sentence(shift);
    return $self;
}

sub clean_word {
    $_[0] =~ s/(\[.\])?\..$//o;
    $_[0];
}


sub find_start {
    my $linkage = shift;
    my $pattern = shift;
    foreach ($linkage->words){
	my $text = $_->text;
	next if $text eq 'LEFT-WALL' || $text eq 'RIGHT-WALL';
	$text = clean_word $text;
#	print $text,$/;
	if($text eq $pattern){
	    return $_;
	}
    }
}

sub sig {
    local $_ = shift;
    if(ref($_) =~ /link$/i){
	my $w = clean_word $_->linkword();
	return $_->linkposition().':'.$w
    }
    else {
	my $w = clean_word $_->text();
	return $_->position().':'.$w
    }
}



sub find {
    my $self = shift;
    my ($start, $stop) = @_;
    my $linkage = ($self->{sentence}->linkages)[0];
#    print $self->{parser}->get_diagram($linkage);
    my $found;
    my @path;
    my @stack;
    my $link;
    my $cur_ptr;
    my $start = find_start($linkage, $start);
    return unless ref $start;
    push @stack, $start;
    my %visited_word;
    while(@stack and not $found){
	if(not $cur_ptr){
	    $cur_ptr = $stack[-1];
#	    print "LINKS ", Dumper $cur_ptr;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.918 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )