|
Lingua::Treebank::Const - Object modeling constituent from a treebank |
Lingua::Treebank::Const - Object modeling constituent from a treebank
use Lingua::Treebank::Const;
my $text = <<EOTREE
(S
(NP-SBJ (DT this) )
(VP (VBZ is)
(NP-PRD (NNP Lisa) ))
(. .) )
TREE
my $utt = Lingua::Treebank::Const->new->from_penn_string($text)
print $utt->as_penn_text(), "\n";;
Results:
(S
(NP-SBJ
(DT this))
(VP
(VBZ is)
(NP-PRD
(NNP Lisa) ))
(. .))
This is configurable (TO DO: document how so).
module defines methods for accessing syntactic constituents; it identifies its parents and its children, and can write itself out in a variety of formats (currently Penn treebank style).
Module for describing simple constituents of the Penn Treebank. Recursive behaviors are implied.
Note assumption that terminal nodes (those with defined word
values) will not have children, and vice versa. This assumption is
currently unchecked by the code.
For a number of these methods, the jargonish notion of domination plays a large role, so for those who might not know:
a node A dominates another node B if B is a descendant of
A.
from_penn_string initialization method, as
below:
my $text = <<EOTREE
(S
(NP-SBJ (DT this) )
(VP (VBZ is)
(NP-PRD (NNP Lisa) ))
(. .) )
TREE
my $utt = Lingua::Treebank::Const->new->from_penn_string($text)
Otherwise, resulting new unit will have no values (parent,
children, tag or word set by default.
These methods help to populate the fields of these objects from external data.
(S
(NP-SBJ (DT this) )
(VP (VBZ is)
(NP-PRD (NNP Lisa) ))
(. .) )
populates the current node with tag S and the children field
with new objects (tag NP, tag VP, and tag .). This
method recurses on new and from_penn_string to do its job.
annot, not the tag).
TO DO: example here.
word should contain the
lexical item that is represented.
Lingua::Treebank::Const objects
that are the children of the current node.
Currently does not check whether word is populated.
These methods ask questions about the dominating ancestors and direct children of the current node. Think of them as navigating up-and-down the tree.
Returns whether self is a leaf. Does not check whether children
are populated; if automatically generated from the from_penn_string
method then this will always be correct.
Returns the root node for the instance in question (might be itself)
Returns a list of all the nodes (distal first) between the instance and the root.
Returns undefined and carps when the given node is not an
ancestor of the instance.
Returns whether the ancestor is indeed an ancestor of the current instance.
Returns whether current instance is an ancestor of the presumed descendant.
Returns whether current instance shares an immediate parent with the presumed sibling.
Returns the farthest distance from the current node to a terminal node.
Returns the distance from the instance to the root.
These methods ask questions about siblings, and left-right movement in the tree. Think of them as moving left-and-right around in the tree.
Returns the index of the daughter in the instance's children
list. Zero-based, of course.
returns the lowest ancestor the instance and the cousin share (or undefined if they do not share an ancestor)
The expectation is that the sub will not modify the node.
These methods are ways of exposing and comparing regions of local structure.
Removes the DAUGHTER from the children list of the current
instance. DAUGHTER node will still be a valid node, but it will no
longer have a parent; it will be a root.
Note that detach may leave a degenerate tree: it may have no
terminal node (one with words) at the end of a branch. To avoid this,
use wither instead.
Detaches self from parent. self will become an independent root. If
the parent has no other children, will recursively call
parent-wither>, making a possibly zero-length list of degenerate roots
above it until an ancestor has a different child than the one in this
line of descent.
A A
/ \ C B |
B X / \ X
/ \ => D E |
C Y Y
/ \
D E
Before After
calling C->wither()
children list.
children of the current instance.
A->flatten()
/ /
A ==> A__
/ \ /|\ \
X B C F D G
/|\ \
C F D E
\
G
A->retract(X)
/ /
A ==> A
/ \ /|\
X B C D B
/ \ \ / \ \
C D E F G E
/ \
F G
carp
if there is no daughter at INDEX.
The daughter at INDEX remains well-formed, though if you do not maintain your own pointer to it, it will probably be collected by the garbage collector.
children.
These methods are methods that may (or not) be useful in programming with these objects. These methods are used internally, but are exposed for the programmer who might need them.
stringify overloading is certainly helpful in debugging, since the perl
debugger representation of these objects is complicated by their
up-reference to parents.
To do: document additional parameters to this, and the possible effects of changing them
$Lingua::Treebank::Const::CHILD_PROLOG$Lingua::Treebank::Const::INDENT_CHAR$Lingua::Treebank::Const::CHILD_EPILOGperldoc overload).
Depending on the value of $Lingua::Treebank::Const::STRINGIFY (see
below), the string representation of the object varies. The default
behavior is as_penn_text, above.
Note that like any object-ref, copying its stringification does NOT
work to retain all its behaviors. Nor does an identical string
representation necessarily mean the two objects are the same
object; merely, that they have the same structure. (see equiv_to).
== or !=).
Returns an integer representing the unique object. Identity on this method does indicate identity of the objects.
Rarely used in client code. The numeric inequality operators are unlikely to have any useful meaning on these objects, though they should behave consistently (you should get consistent answers given any two objects, regardless of methods called on those objects).
&action argument is required, others are optional.
Calls &action (a subroutine ref) as a method on node and its
children, recursively, passing the node under consideration and the
$state value (if provided).
If &stop_crit is defined, calls it on each node; when &stop_crit
returns true, children of that node are not pursued.
For both action and stop_crit commands, if a string is passed,
it will be called if a method by that name can be found in the object.
$state is passed into each of the child method calls. This is
convenient for things like pushing interesting elements onto a list,
or updating a counter. It must be a scalar, but can be a reference.
Passing a true value as $bf_traversal tells walk() to explore
the tree breadth-first rather than depth-first. passing a false (but
defined) value forces depth-first. Undefined values default to the
value of $Lingua::Treebank::Const::BF_TRAVERSAL, which is undef
(false) -- and thus depth-first by default.
# find out how many children each NP has, but don't count anything
# inside an EDITED node
my $action = sub {
my ($self, $state) = @_;
return unless $self->tag() eq 'NP';
# just print it
print scalar @{$self->children}, "\n";
# or store it in the state variable
push @{$state}, scalar @{$self->children()};
};
my $stop_crit = sub {$_[0]->tag() eq 'EDITED'};
$tree->walk( $action, $stop_crit, \@counts );
use List::Util 'sum';
print "there were ", sum (@counts),
" total children of NP nodes\n";
walk() method to be breadth-first rather than depth-first.
e.g.
(S
(NP
(NNP Joe)
)
(VP
(VB likes)
(NP
(NNP Bach)
)
)
(. .)
)
Joe likes Bach .
NNP VB NNP .
check that destroy doesn't leak (undo parent links?)
dump as latex tree
read in other treebank formats (latex trees?)
None by default.
-CAX
Lingua::Treebank::Const
((FRAG (FOO bar))
critically, earlier versions failed when the tag was empty and not followed by whitespace
walk() method defaults
Documentation for Penn treebank http://www.cis.upenn.edu/~treebank/.
Jeremy Gillmor Kahn, <kahn@cpan.org>
Copyright 2003 by Jeremy Gillmor Kahn
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
|
Lingua::Treebank::Const - Object modeling constituent from a treebank |