Debian Codegraph @

t/sl_timeflies.t

raw
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 # This example originally came from Ralf Muschall. Ruslan Shvedov
22 # reworked my implementation, converting it to the SLIF and
23 # Penn Treebank. Ruslan and Ralf clearly know English grammar better than
24 # most of us native speakers.
25
26 # 'time', 'fruit', and 'flies' can be nouns or verbs, 'like' can be
27 # a preposition or a verb. This creates syntactic ambiguity shown
28 # in the parse results.
29
30 # Modifier nouns are not tagged or lexed as adjectives (JJ), because
31 # "Nouns that are used as modifiers, whether in isolation or in sequences,
32 # should be tagged as nouns (NN, NNS) rather than as adjectives (JJ)."
33 # -- ftp://ftp.cis.upenn.edu/pub/treebank/doc/tagguide.ps.gz
34
35 # The saying "time flies like an arrow; fruit flies like a banana"
36 # is attributed to Groucho Marx, but there is no reason to believe
37 # he ever said it. Apparently, the saying
38 # first appeared on the Usenet on net.jokes in 1982.
39 # I've documented this whole thing on Wikipedia:
40 # http://en.wikipedia.org/wiki/Time_flies_like_an_arrow
41 #
42 # The permalink is:
43 # http://en.wikipedia.org/w/index.php?title=Time_flies_like_an_arrow&oldid=311163283
44
45 use 5.010001;
46 use strict;
47 use warnings;
48 use English qw( -no_match_vars );
49
50 use Test::More tests => 1;
51 use lib 'inc';
52 use Marpa::R2::Test;
53 use Marpa::R2;
54
55 # Marpa::R2::Display
56 # name: SLIF "time flies" DSL synopsis
57
58 my $grammar = Marpa::R2::Scanless::G->new(
59 { source => \(<<'END_OF_SOURCE'),
60
61 :default ::= action => [ name, values ]
62 lexeme default = action => [ name, value ]
63
64 S ::= NP VP period
65
66 NP ::= NN
67 | DT NN
68 | NN NNS
69
70 VP ::= VBP NP
71 | VBP PP
72 | VBZ PP
73
74 PP ::= IN NP
75
76 period ~ '.'
77
78 :discard ~ whitespace
79 whitespace ~ [\s]+
80
81 DT ~ 'a' | 'an'
82 NN ~ 'arrow' | 'banana'
83 NNS ~ 'flies'
84 VBZ ~ 'flies'
85 NN ~ 'fruit':i
86 VBP ~ 'fruit':i
87 IN ~ 'like'
88 VBP ~ 'like'
89 NN ~ 'time':i
90 VBP ~ 'time':i
91
92 END_OF_SOURCE
93 }
94 );
95
96 # Marpa::R2::Display::End
97
98 my $expected = <<'EOS';
99 (S
100 (NP (NN Time))
101 (VP (VBZ flies)
102 (PP (IN like)
103 (NP (DT an) (NN arrow))))
104 (. .))
105 (S
106 (NP (NN Time) (NNS flies))
107 (VP (VBP like)
108 (NP (DT an) (NN arrow)))
109 (. .))
110 (S
111 (NP (NN Fruit))
112 (VP (VBZ flies)
113 (PP (IN like)
114 (NP (DT a) (NN banana))))
115 (. .))
116 (S
117 (NP (NN Fruit) (NNS flies))
118 (VP (VBP like)
119 (NP (DT a) (NN banana)))
120 (. .))
121 EOS
122
123 my $paragraph = <<END_OF_PARAGRAPH;
124 Time flies like an arrow.
125 Fruit flies like a banana.
126 END_OF_PARAGRAPH
127
128 # structural tags -- need a newline
129 my %s_tags = map { $_ => undef } qw{ NP VP PP period };
130
131 my @actual = ();
132 for my $sentence (split /\n/, $paragraph){
133
134 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
135 $recce->read( \$sentence );
136
137 while ( defined( my $value_ref = $recce->value() ) ) {
138 my $value = $value_ref ? bracket ( ${$value_ref} ) : 'No parse';
139 push @actual, $value;
140 }
141 }
142
143 sub bracket {
144 my ($tag, @contents) = @{ $_[0] };
145 state $level++;
146 my $bracketed =
147 exists $s_tags{$tag} ? ("\n" . (" " x ($level-1))) : '';
148 $tag = '.' if $tag eq 'period';
149 if (ref $contents[0]){
150 $bracketed .=
151 "($tag "
152 . join(' ', map { bracket($_) } @contents)
153 . ")";
154 }
155 else {
156 $bracketed .= "($tag $contents[0])";
157 }
158 $level--;
159 return $bracketed;
160 }
161
162 Marpa::R2::Test::is( ( join "\n", @actual ) . "\n",
163 $expected, 'Ambiguous English sentences' );
164
165 1; # In case used as "do" file
166
167 # vim: set expandtab shiftwidth=4:
168