File: sl_timeflies.t

package info (click to toggle)
libmarpa-r2-perl 12.000000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,660 kB
  • sloc: perl: 42,628; ansic: 23,387; sh: 4,363; makefile: 157
file content (168 lines) | stat: -rwxr-xr-x 4,480 bytes parent folder | download
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/perl
# Copyright 2022 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 parses ambiguous English sentences.  The target annotation
# is Penn Treebank's syntactic bracketing tags.  For details, see
# http://www.cis.upenn.edu/~treebank/

# This example originally came from Ralf Muschall.  Ruslan Shvedov
# reworked my implementation, converting it to the SLIF and
# Penn Treebank.  Ruslan and Ralf clearly know English grammar better than
# most of us native speakers.

# 'time', 'fruit', and 'flies' can be nouns or verbs, 'like' can be
# a preposition or a verb.  This creates syntactic ambiguity shown 
# in the parse results.

# Modifier nouns are not tagged or lexed as adjectives (JJ), because
# "Nouns that are used as modifiers, whether in isolation or in sequences,
# should be tagged as nouns (NN, NNS) rather than as adjectives (JJ)."
# -- ftp://ftp.cis.upenn.edu/pub/treebank/doc/tagguide.ps.gz

# 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.010001;
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;

# Marpa::R2::Display
# name: SLIF "time flies" DSL synopsis

my $grammar = Marpa::R2::Scanless::G->new(
    {   source => \(<<'END_OF_SOURCE'),
        
:default     ::= action => [ name, values ]
lexeme default = action => [ name, value ]

S   ::= NP  VP  period  

NP  ::= NN              
    |   DT  NN          
    |   NN  NNS         

VP  ::= VBP NP          
    |   VBP PP          
    |   VBZ PP          

PP  ::= IN  NP          
    
period ~ '.'

:discard ~ whitespace
whitespace ~ [\s]+

DT  ~ 'a' | 'an'
NN  ~ 'arrow' | 'banana'
NNS ~ 'flies'
VBZ ~ 'flies' 
NN  ~ 'fruit':i
VBP ~ 'fruit':i
IN  ~ 'like'
VBP ~ 'like'
NN  ~ 'time':i
VBP ~ 'time':i

END_OF_SOURCE
    }
);

# Marpa::R2::Display::End

my $expected = <<'EOS';
(S 
  (NP (NN Time)) 
  (VP (VBZ flies) 
    (PP (IN like) 
      (NP (DT an) (NN arrow)))) 
  (. .))
(S 
  (NP (NN Time) (NNS flies)) 
  (VP (VBP like) 
    (NP (DT an) (NN arrow))) 
  (. .))
(S 
  (NP (NN Fruit)) 
  (VP (VBZ flies) 
    (PP (IN like) 
      (NP (DT a) (NN banana)))) 
  (. .))
(S 
  (NP (NN Fruit) (NNS flies)) 
  (VP (VBP like) 
    (NP (DT a) (NN banana))) 
  (. .))
EOS

my $paragraph = <<END_OF_PARAGRAPH;
Time flies like an arrow.
Fruit flies like a banana.
END_OF_PARAGRAPH

# structural tags -- need a newline
my %s_tags = map { $_ => undef } qw{ NP VP PP period }; 

my @actual = ();
for my $sentence (split /\n/, $paragraph){

    my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar  } );
    $recce->read( \$sentence );

    while ( defined( my $value_ref = $recce->value() ) ) {
        my $value = $value_ref ? bracket ( ${$value_ref} ) : 'No parse';
        push @actual, $value;
    }
}

sub bracket   { 
    my ($tag, @contents) = @{ $_[0] };
    state $level++;
    my $bracketed = 
        exists $s_tags{$tag} ? ("\n" . ("  " x ($level-1))) : '';
    $tag = '.' if $tag eq 'period';
    if (ref $contents[0]){
        $bracketed .= 
                "($tag "
            .   join(' ', map { bracket($_) } @contents) 
            .   ")";
    }
    else {
        $bracketed .= "($tag $contents[0])";
    }
    $level--;
    return $bracketed;
}

Marpa::R2::Test::is( ( join "\n", @actual ) . "\n",
    $expected, 'Ambiguous English sentences' );

1;    # In case used as "do" file

# vim: set expandtab shiftwidth=4: