| 1 |
#!/usr/bin/perl |
| 2 |
# Copyright 2022 Jeffrey Kegler |
| 3 |
# This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 4 |
# redistribute it and/or modify it under the terms of the GNU Lesser |
| 5 |
# General Public License as published by the Free Software Foundation, |
| 6 |
# either version 3 of the License, or (at your option) any later version. |
| 7 |
# |
| 8 |
# Marpa::R2 is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 |
# Lesser General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU Lesser |
| 14 |
# General Public License along with Marpa::R2. If not, see |
| 15 |
# http://www.gnu.org/licenses/. |
| 16 |
|
| 17 |
# This example parses ambiguous English sentences. The target annotation |
| 18 |
# is Penn Treebank's syntactic bracketing tags. For details, see |
| 19 |
# http://www.cis.upenn.edu/~treebank/ |
| 20 |
|
| 21 |
use 5.010001; |
| 22 |
use strict; |
| 23 |
use warnings; |
| 24 |
use English qw( -no_match_vars ); |
| 25 |
|
| 26 |
use Test::More tests => 3; |
| 27 |
use lib 'inc'; |
| 28 |
use Marpa::R2::Test; |
| 29 |
use Marpa::R2; |
| 30 |
|
| 31 |
my $dsl = <<'END_OF_SOURCE'; |
| 32 |
|
| 33 |
S ::= NP VP period action => do_S |
| 34 |
|
| 35 |
NP ::= NN action => do_NP_NN |
| 36 |
| NNS action => do_NP_NNS |
| 37 |
| DT NN action => do_NP_DT_NN |
| 38 |
| NN NNS action => do_NP_NN_NNS |
| 39 |
| NNS CC NNS action => do_NP_NNS_CC_NNS |
| 40 |
|
| 41 |
VP ::= VBZ NP action => do_VP_VBZ_NP |
| 42 |
| VP VBZ NNS action => do_VP_VP_VBZ_NNS |
| 43 |
| VP CC VP action => do_VP_VP_CC_VP |
| 44 |
| VP VP CC VP action => do_VP_VP_VP_CC_VP |
| 45 |
| VBZ action => do_VP_VBZ |
| 46 |
|
| 47 |
period ~ '.' |
| 48 |
|
| 49 |
:discard ~ whitespace |
| 50 |
whitespace ~ [\s]+ |
| 51 |
|
| 52 |
CC ~ 'and' |
| 53 |
DT ~ 'a' | 'an' |
| 54 |
NN ~ 'panda' |
| 55 |
NNS ~ 'shoots' | 'leaves' |
| 56 |
VBZ ~ 'eats' | 'shoots' | 'leaves' |
| 57 |
|
| 58 |
END_OF_SOURCE |
| 59 |
|
| 60 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 61 |
{ source => \$dsl } ); |
| 62 |
|
| 63 |
my $full_expected = <<'END_OF_OUTPUT'; |
| 64 |
(S (NP (DT a) (NN panda)) |
| 65 |
(VP (VBZ eats) (NP (NNS shoots) (CC and) (NNS leaves))) |
| 66 |
(. .)) |
| 67 |
(S (NP (DT a) (NN panda)) |
| 68 |
(VP (VP (VBZ eats) (NP (NNS shoots))) (CC and) (VP (VBZ leaves))) |
| 69 |
(. .)) |
| 70 |
(S (NP (DT a) (NN panda)) |
| 71 |
(VP (VP (VBZ eats)) (VP (VBZ shoots)) (CC and) (VP (VBZ leaves))) |
| 72 |
(. .)) |
| 73 |
END_OF_OUTPUT |
| 74 |
|
| 75 |
my $sentence = 'a panda eats shoots and leaves.'; |
| 76 |
|
| 77 |
my @actual = (); |
| 78 |
|
| 79 |
my $recce = Marpa::R2::Scanless::R->new( { |
| 80 |
grammar => $grammar, |
| 81 |
semantics_package => 'PennTags' |
| 82 |
} ); |
| 83 |
|
| 84 |
$recce->read( \$sentence ); |
| 85 |
|
| 86 |
while ( defined( my $value_ref = $recce->value() ) ) { |
| 87 |
my $value = $value_ref ? ${$value_ref} : 'No parse'; |
| 88 |
push @actual, $value; |
| 89 |
} |
| 90 |
|
| 91 |
Marpa::R2::Test::is( ( join "\n", sort @actual ) . "\n", |
| 92 |
$full_expected, 'Ambiguous English sentence using value()' ); |
| 93 |
|
| 94 |
my $panda_grammar = Marpa::R2::Scanless::G->new( |
| 95 |
{ source => \$dsl } ); |
| 96 |
my $panda_recce = Marpa::R2::Scanless::R->new( |
| 97 |
{ grammar => $panda_grammar, |
| 98 |
semantics_package => 'PennTags' } ); |
| 99 |
$panda_recce->read( \$sentence ); |
| 100 |
my $asf = Marpa::R2::ASF->new( { slr=>$panda_recce } ); |
| 101 |
my $full_result = $asf->traverse( {}, \&full_traverser ); |
| 102 |
my $pruned_result = $asf->traverse( {}, \&pruning_traverser ); |
| 103 |
|
| 104 |
sub full_traverser { |
| 105 |
|
| 106 |
# This routine converts the glade into a list of Penn-tagged elements |
| 107 |
# by calling semantic action closures fetched from the recognizer. |
| 108 |
# It is called recursively. |
| 109 |
my ($glade, $scratch) = @_; |
| 110 |
my $rule_id = $glade->rule_id(); |
| 111 |
my $symbol_id = $glade->symbol_id(); |
| 112 |
my $symbol_name = $panda_grammar->symbol_name($symbol_id); |
| 113 |
|
| 114 |
# A token is a single choice and we just return it as a literal wrapped |
| 115 |
# to match the rule closures parameter list |
| 116 |
if ( not defined $rule_id ) { |
| 117 |
return [ $glade->literal() ]; |
| 118 |
} ## end if ( not defined $rule_id ) |
| 119 |
|
| 120 |
# Our result will be a list of choices |
| 121 |
my @return_value = (); |
| 122 |
|
| 123 |
CHOICE: while (1) { |
| 124 |
|
| 125 |
# The parse results at each position are a list of choices, so |
| 126 |
# to produce a new result list, we need to take a Cartesian |
| 127 |
# product of all the choices |
| 128 |
|
| 129 |
# Marpa::R2::Display::Start |
| 130 |
# name: ASF all_choices() traverser method example |
| 131 |
|
| 132 |
my @results = $glade->all_choices(); |
| 133 |
|
| 134 |
# Marpa::R2::Display::End |
| 135 |
|
| 136 |
# Special case for the start rule: just collapse one level of lists |
| 137 |
if ( $symbol_name eq '[:start]' ) { |
| 138 |
return [ map { join q{}, @{$_} } @results ]; |
| 139 |
} |
| 140 |
|
| 141 |
# Now we have a list of choices, as a list of lists. Each sub list |
| 142 |
# is a list of parse results, which we need to pass to the rule closures |
| 143 |
# and join into a single Penn-tagged element. The result will be |
| 144 |
# to collapse one level of lists, and leave us with a list of |
| 145 |
# Penn-tagged elements. |
| 146 |
|
| 147 |
# First, we take the semantic action closure of the rule as defined in the |
| 148 |
# recognizer's semantic package. |
| 149 |
my $closure = $panda_recce->rule_closure( $glade->rule_id() ); |
| 150 |
# Note: $glade->rule_id() is used instead of the above $rule_id, because |
| 151 |
# $glade->next() must have been called and the current glade (and thus |
| 152 |
# the rule) might have changed |
| 153 |
|
| 154 |
# Now, we need to check if the semantic action of the rule is defined |
| 155 |
# as a closure. For now, we just die if it is not. |
| 156 |
# |
| 157 |
# However, start, length, lhs, and values builtins can be emulated by |
| 158 |
# using $glade->span(), $glade->symbol_id(), and $glade->rh_values(). |
| 159 |
# Still, defining closures would probably serve you better. |
| 160 |
unless (defined $closure and ref $closure eq 'CODE'){ |
| 161 |
die "The semantics of Rule #" . $glade->rule_id() . "is not defined as a closure."; |
| 162 |
} |
| 163 |
|
| 164 |
push @return_value, map { $closure->( {}, @{$_} ) } @results; |
| 165 |
|
| 166 |
# Look at the next alternative in this glade, or end the |
| 167 |
# loop if there is none |
| 168 |
last CHOICE if not defined $glade->next(); |
| 169 |
|
| 170 |
} ## end CHOICE: while (1) |
| 171 |
|
| 172 |
# Return the list of Penn-tagged elements for this glade |
| 173 |
return \@return_value; |
| 174 |
} ## end sub full_traverser |
| 175 |
|
| 176 |
my $cooked_result = join "\n", (sort @{$full_result}), q{}; |
| 177 |
Marpa::R2::Test::is( $cooked_result, $full_expected, |
| 178 |
'Ambiguous English sentence using ASF' ); |
| 179 |
|
| 180 |
sub pruning_traverser { |
| 181 |
|
| 182 |
# This routine converts the glade into a list of Penn-tagged elements. It is called recursively. |
| 183 |
my ($glade, $scratch) = @_; |
| 184 |
my $rule_id = $glade->rule_id(); |
| 185 |
my $symbol_id = $glade->symbol_id(); |
| 186 |
my $symbol_name = $panda_grammar->symbol_name($symbol_id); |
| 187 |
|
| 188 |
# A token is a single choice, and we know enough to fully Penn-tag it |
| 189 |
if ( not defined $rule_id ) { |
| 190 |
return $glade->literal(); # wrap for the closure call |
| 191 |
} |
| 192 |
|
| 193 |
# Marpa::R2::Display::Start |
| 194 |
# name: ASF rh_values() traverser method example |
| 195 |
|
| 196 |
my @return_value = $glade->rh_values(); |
| 197 |
|
| 198 |
# Marpa::R2::Display::End |
| 199 |
|
| 200 |
if ($symbol_name eq '[:start]'){ |
| 201 |
# Special case for the start rule |
| 202 |
return $return_value[0] . "\n" ; |
| 203 |
} |
| 204 |
else{ |
| 205 |
my $closure = $panda_recce->rule_closure($rule_id); |
| 206 |
die "The semantics of Rule $rule_id is not defined as a closure." |
| 207 |
unless defined $closure and ref $closure eq 'CODE'; |
| 208 |
return $closure->( {}, @return_value ); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
my $pruned_expected = <<'END_OF_OUTPUT'; |
| 213 |
(S (NP (DT a) (NN panda)) |
| 214 |
(VP (VBZ eats) (NP (NNS shoots) (CC and) (NNS leaves))) |
| 215 |
(. .)) |
| 216 |
END_OF_OUTPUT |
| 217 |
|
| 218 |
Marpa::R2::Test::is( $pruned_result, $pruned_expected, |
| 219 |
'Ambiguous English sentence using ASF: pruned' ); |
| 220 |
|
| 221 |
sub PennTags::do_S { "(S $_[1]\n $_[2]\n (. .))" } |
| 222 |
|
| 223 |
sub PennTags::do_NP_NN { "(NP (NN $_[1]))" } |
| 224 |
sub PennTags::do_NP_NNS { "(NP (NNS $_[1]))" } |
| 225 |
sub PennTags::do_NP_DT_NN { "(NP (DT $_[1]) (NN $_[2]))" } |
| 226 |
sub PennTags::do_NP_NN_NNS { "(NP (NN $_[1]) (NNS $_[2]))" } |
| 227 |
sub PennTags::do_NP_NNS_CC_NNS { "(NP (NNS $_[1]) (CC $_[2]) (NNS $_[3]))" } |
| 228 |
|
| 229 |
sub PennTags::do_VP_VBZ_NP { "(VP (VBZ $_[1]) $_[2])" } |
| 230 |
sub PennTags::do_VP_VP_VBZ_NNS { "(VP $_[1] (VBZ $_[2]) (NNS $_[3]))" } |
| 231 |
sub PennTags::do_VP_VP_CC_VP { "(VP $_[1] (CC $_[2]) $_[3])" } |
| 232 |
sub PennTags::do_VP_VP_VP_CC_VP { "(VP $_[1] $_[2] (CC $_[3]) $_[4])" } |
| 233 |
sub PennTags::do_VP_VBZ { "(VP (VBZ $_[1]))" } |
| 234 |
|
| 235 |
1; # In case used as "do" file |
| 236 |
|