Debian Codegraph @

t/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 is from Ralf Muschall, who clearly knows English
18 # grammar better than most native speakers. I've reworked the
19 # terminology to follow _A Comprehensive Grammar of the English
20 # Language_, by Quirk, Greenbaum, Leech and Svartvik. My edition
21 # was the "Seventh (corrected) impression 1989".
22 #
23 # When it is not a verb, I treat "like"
24 # as a preposition in an adjunct of manner,
25 # as per 8.79, p. 557; 9.4, pp. 661; and 9.48, pp. 698-699.
26 #
27 # The saying "time flies like an arrow; fruit flies like a banana"
28 # is attributed to Groucho Marx, but there is no reason to believe
29 # he ever said it. Apparently, the saying
30 # first appeared on the Usenet on net.jokes in 1982.
31 # I've documented this whole thing on Wikipedia:
32 # http://en.wikipedia.org/wiki/Time_flies_like_an_arrow
33 #
34 # The permalink is:
35 # http://en.wikipedia.org/w/index.php?title=Time_flies_like_an_arrow&oldid=311163283
36
37 use 5.010001;
38 use strict;
39 use warnings;
40 use English qw( -no_match_vars );
41
42 use Test::More tests => 1;
43 use lib 'inc';
44 use Marpa::R2::Test;
45 use Marpa::R2;
46
47 ## no critic (Subroutines::RequireArgUnpacking)
48
49 sub do_sva_sentence { return "sva($_[1];$_[2];$_[3])" }
50 sub do_svo_sentence { return "svo($_[1];$_[2];$_[3])" }
51 sub do_adjunct { return "adju($_[1];$_[2])" }
52 sub do_adjective { return "adje($_[1])" }
53 sub do_qualified_subject { return "s($_[1];$_[2])" }
54 sub do_bare_subject { return "s($_[1])" }
55 sub do_noun { return "n($_[1])" }
56 sub do_verb { return "v($_[1])" }
57 sub do_object { return "o($_[1];$_[2])" }
58 sub do_article { return "art($_[1])" }
59 sub do_preposition { return "pr($_[1])" }
60
61 ## use critic
62
63 my $grammar = Marpa::R2::Grammar->new(
64 { start => 'sentence',
65 actions => 'main',
66 rules => [
67 [ 'sentence', [qw(subject verb adjunct)], 'do_sva_sentence' ],
68 [ 'sentence', [qw(subject verb object)], 'do_svo_sentence' ],
69 [ 'adjunct', [qw(preposition object)], 'do_adjunct' ],
70 [ 'adjective', [qw(adjective_noun_lex)], 'do_adjective' ],
71 [ 'subject', [qw(adjective noun)], 'do_qualified_subject' ],
72 [ 'subject', [qw(noun)], 'do_bare_subject' ],
73 [ 'noun', [qw(adjective_noun_lex)], 'do_noun' ],
74 [ 'verb', [qw(verb_lex)], 'do_verb' ],
75 [ 'object', [qw(article noun)], 'do_object' ],
76 [ 'article', [qw(article_lex)], 'do_article' ],
77 [ 'preposition', [qw(preposition_lex)], 'do_preposition' ],
78 ],
79 }
80 );
81
82 my $expected = <<'EOS';
83 sva(s(n(fruit));v(flies);adju(pr(like);o(art(a);n(banana))))
84 sva(s(n(time));v(flies);adju(pr(like);o(art(an);n(arrow))))
85 svo(s(adje(fruit);n(flies));v(like);o(art(a);n(banana)))
86 svo(s(adje(time);n(flies));v(like);o(art(an);n(arrow)))
87 EOS
88 my @actual = ();
89
90 $grammar->precompute();
91
92 my %lexical_class = (
93 'preposition_lex' => 'like',
94 'verb_lex' => 'like flies',
95 'adjective_noun_lex' => 'fruit banana time arrow flies',
96 'article_lex' => 'a an',
97 );
98 my %vocabulary = ();
99 for my $lexical_class (keys %lexical_class) {
100 my $words = $lexical_class{$lexical_class};
101 for my $word ( split q{ }, $words ) {
102 push @{ $vocabulary{$word} }, $lexical_class;
103 }
104 } ## end for my $lexical_class (%lexical_class)
105
106 for my $data ( 'time flies like an arrow', 'fruit flies like a banana' ) {
107
108 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
109 die 'Failed to create recognizer' if not $recce;
110
111 for my $word ( split q{ }, $data ) {
112
113 # Marpa::R2::Display
114 # name: Recognizer exhausted Synopsis
115
116 $recce->exhausted() and die 'Recognizer exhausted';
117
118 # Marpa::R2::Display::End
119
120 for my $type ( @{ $vocabulary{$word} } ) {
121 $recce->alternative( $type, \$word, 1 )
122 or die 'Recognition failed';
123 }
124 $recce->earleme_complete();
125 } ## end for my $word ( split q{ }, $data )
126
127 # Marpa::R2::Display
128 # name: Recognizer end_input Synopsis
129
130 $recce->end_input();
131
132 # Marpa::R2::Display::End
133
134 while ( defined( my $value_ref = $recce->value() ) ) {
135 my $value = $value_ref ? ${$value_ref} : 'No parse';
136 push @actual, $value;
137 }
138 } ## end for my $data ( 'time flies like an arrow', ...)
139
140 Marpa::R2::Test::is( ( join "\n", sort @actual ) . "\n",
141 $expected, 'Ambiguous English sentences' );
142
143 1; # In case used as "do" file
144