| 1 | # emacs, this is really -*-Perl-*- | ||||||||||
| 2 | |||||||||||
| 3 | # Before `make install' is performed this script should be runnable with | ||||||||||
| 4 | # `make test'. After `make install' it should work as `perl 50walking.t' | ||||||||||
| 5 | |||||||||||
| 6 | ######################### | ||||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Test::More tests => 12; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # Test 1 | ||||||||||
| 12 | 1 | ▶ | BEGIN { use_ok('Lingua::Treebank::Const') }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | ######################### | ||||||||||
| 15 | |||||||||||
| 16 | # my $node; | ||||||||||
| 17 | |||||||||||
| 18 | 1 | my $empty_root = <<EOTREE; | |||||||||
| 19 | ( | ||||||||||
| 20 | (INTJ | ||||||||||
| 21 | (UH Okay) | ||||||||||
| 22 | (. .) | ||||||||||
| 23 | (-DFL- E_S))) | ||||||||||
| 24 | EOTREE | ||||||||||
| 25 | |||||||||||
| 26 | 1 | my $path = <<EOTREE; | |||||||||
| 27 | (NP | ||||||||||
| 28 | (NP | ||||||||||
| 29 | (VP | ||||||||||
| 30 | (N dog)))) | ||||||||||
| 31 | EOTREE | ||||||||||
| 32 | |||||||||||
| 33 | 1 | my $children = <<EOTREE; | |||||||||
| 34 | (no | ||||||||||
| 35 | (yes_1 term_1) | ||||||||||
| 36 | (no | ||||||||||
| 37 | (no term_2) | ||||||||||
| 38 | (yes_2 term_3))) | ||||||||||
| 39 | EOTREE | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | 1 | my $sibling = <<EOTREE; | |||||||||
| 43 | (S | ||||||||||
| 44 | (NP | ||||||||||
| 45 | (D the) | ||||||||||
| 46 | (N boy)) | ||||||||||
| 47 | (VP | ||||||||||
| 48 | ran)) | ||||||||||
| 49 | EOTREE | ||||||||||
| 50 | |||||||||||
| 51 | 1 | my $npcount = <<EOTREE; | |||||||||
| 52 | (S | ||||||||||
| 53 | (CONJP | ||||||||||
| 54 | (NP (D the) (N boy)) | ||||||||||
| 55 | (C and) | ||||||||||
| 56 | (NP (D a) (A tall) (N boy))) | ||||||||||
| 57 | (NP | ||||||||||
| 58 | (D a) | ||||||||||
| 59 | (N girl) | ||||||||||
| 60 | (PP | ||||||||||
| 61 | (EDITED | ||||||||||
| 62 | (P in ) | ||||||||||
| 63 | (NP (PN Naples) (PN On) (PN East) (PN Thames)) | ||||||||||
| 64 | (UH uh)) | ||||||||||
| 65 | (P in) | ||||||||||
| 66 | (NP (PN Paris))))) | ||||||||||
| 67 | EOTREE | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | # Test 2 | ||||||||||
| 71 | { | ||||||||||
| 72 | 1 | my $empty_root_node = Lingua::Treebank::Const->new(); | |||||||||
| 73 | 1 | $empty_root_node = $empty_root_node->from_penn_string($empty_root); | |||||||||
| 74 | 1 | ok($empty_root_node->is_empty_root(), "Detect empty root"); | |||||||||
| 75 | } | ||||||||||
| 76 | # Tests 3-4 | ||||||||||
| 77 | { | ||||||||||
| 78 | 1 | my $path_node = Lingua::Treebank::Const->new(); | |||||||||
| 79 | 1 | $path_node->from_penn_string($path); | |||||||||
| 80 | 1 | my @terms = $path_node->get_all_terminals(); | |||||||||
| 81 | 1 | my $child = shift @terms; | |||||||||
| 82 | 1 | ▶ | my @ancestors = $child->select_ancestors(sub{$_[0]->tag() eq "NP"}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 83 | 1 | ok($ancestors[1] eq $path_node, "Select ancestors 1"); | |||||||||
| 84 | 1 | ok($ancestors[0] eq @{$path_node->children()}[0], "Select ancestors 2"); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | |||||||||||
| 88 | # Test 5 | ||||||||||
| 89 | { | ||||||||||
| 90 | 1 | my $children_node = Lingua::Treebank::Const->new(); | |||||||||
| 91 | 1 | $children_node->from_penn_string($children); | |||||||||
| 92 | 1 | ▶ | my @children = $children_node->select_children(sub{$_[0]->tag() =~ /yes/}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 93 | 1 | @children = map($_->word(), @children); | |||||||||
| 94 | 1 | ok(eq_array(\@children, ['term_3', 'term_1']), "Select children"); | |||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | |||||||||||
| 98 | # Tests 6-8 | ||||||||||
| 99 | { | ||||||||||
| 100 | 1 | my $sibling_node = Lingua::Treebank::Const->new(); | |||||||||
| 101 | 1 | $sibling_node->from_penn_string($sibling); | |||||||||
| 102 | 1 | my @child = @{$sibling_node->children()}; | |||||||||
| 103 | 1 | my $np = $child[0]; | |||||||||
| 104 | 1 | my $vp = $child[1]; | |||||||||
| 105 | 1 | my $det = @{$np->children()}[0]; | |||||||||
| 106 | 1 | ok($np->is_sibling($vp), "NP-VP sibling"); | |||||||||
| 107 | 1 | ok(not ($det->is_sibling($vp)), "DET-VP not sibling"); | |||||||||
| 108 | 1 | ok(not ($np->is_sibling($det)), "NP-DET not sibling"); | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | # test 9-12 | ||||||||||
| 112 | { | ||||||||||
| 113 | 1 | my $npcount_node = Lingua::Treebank::Const->new(); | |||||||||
| 114 | 1 | $npcount_node->from_penn_string($npcount); | |||||||||
| 115 | |||||||||||
| 116 | # find out how many children each NP has, but don't count anything | ||||||||||
| 117 | # inside an EDITED node | ||||||||||
| 118 | 86 | ▶ | my $action = sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 119 | 86 | my ($self, $state) = @_; | |||||||||
| 120 | 86 | ▶ | return unless $self->tag() eq 'NP'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | |||||||||||
| 122 | # just print it | ||||||||||
| 123 | |||||||||||
| 124 | # print scalar @{$self->children}, "\n"; | ||||||||||
| 125 | |||||||||||
| 126 | # or store it in the state variable | ||||||||||
| 127 | 18 | push @{$state}, scalar @{$self->children()}; | |||||||||
| 128 | 1 | }; | |||||||||
| 129 | |||||||||||
| 130 | 1 | ▶ | my $stop_crit = sub {$_[0]->tag() eq 'EDITED'}; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 131 | |||||||||||
| 132 | { | ||||||||||
| 133 | 1 | my @counts; | |||||||||
| 134 | 1 | $npcount_node->walk( $action, $stop_crit, \@counts ); | |||||||||
| 135 | # the-boy, a-tall-boy, a-girl-PP, Paris | ||||||||||
| 136 | 1 | ok(eq_array(\@counts, [2,3,3,1]), "count"); | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | { | ||||||||||
| 140 | 1 | my @counts; | |||||||||
| 141 | 1 | $npcount_node->walk( $action, $stop_crit, \@counts, 'breadthfirst' ); | |||||||||
| 142 | # a-girl-PP, the-boy, a-tall-boy, Paris | ||||||||||
| 143 | 1 | ok(eq_array(\@counts, [3,2,3,1]), "count"); | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | { | ||||||||||
| 147 | 1 | my @counts; | |||||||||
| 148 | 1 | $npcount_node->walk( $action, undef, \@counts ); | |||||||||
| 149 | # the-boy, a-tall-boy, a-girl-PP, Naples-on-east-thames, Paris | ||||||||||
| 150 | 1 | ok(eq_array(\@counts, [2,3,3,4,1]), "count"); | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | { | ||||||||||
| 154 | 1 | my @counts; | |||||||||
| 155 | 1 | $npcount_node->walk( $action, undef, \@counts, 'breadthfirst' ); | |||||||||
| 156 | # a-girl-PP, the-boy, a-tall-boy, Paris, Naples-on-east-thames | ||||||||||
| 157 | 1 | ok(eq_array(\@counts, [3,2,3,1,4]), "count"); | |||||||||
| 158 | } | ||||||||||
| 159 | |||||||||||
| 160 | # use List::Util 'sum'; | ||||||||||
| 161 | # print "there were ", sum (@counts), | ||||||||||
| 162 | # " total children of NP nodes\n"; | ||||||||||
| 163 | |||||||||||
| 164 | } | ||||||||||