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