Skip to content

Instantly share code, notes, and snippets.

@rns
Created February 1, 2015 08:49
Show Gist options
  • Save rns/f2a87e24b7bdcc695389 to your computer and use it in GitHub Desktop.
Save rns/f2a87e24b7bdcc695389 to your computer and use it in GitHub Desktop.
sl_ambiguity_ranking.t
#!perl
# Copyright 2015 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/.
# Tests of ambiguity_metric() anb ambiguous() across all ranking methods
use 5.010;
use strict;
use warnings;
use Test::More tests => 7;
use English qw( -no_match_vars );
use lib 'inc';
use Marpa::R2::Test;
use Marpa::R2;
my $source = \(<<'END_OF_SOURCE');
lexeme default = latm => 1
top ::= unchoice rank => 1
top ::= choice
unchoice ::= choice1
choice ::= choice1 | choice2
choice1 ::= A1 B1
choice2 ::= A2 B2
A1 ~ 'a'
A2 ~ 'a'
B1 ~ 'b'
B2 ~ 'b'
:discard ~ ws
ws ~ [\s]+
END_OF_SOURCE
my $slg = Marpa::R2::Scanless::G->new({ source => $source });
my $input = q{a b};
for my $ranking_method ('none', 'rule', 'high_rule_only'){
my $slr = Marpa::R2::Scanless::R->new({
grammar => $slg,
ranking_method => $ranking_method,
} );
$slr->read(\$input);
if ($ranking_method eq 'high_rule_only'){
# count parses and test that there is only one
my $parse_count = 0;
while (defined $slr->value()) { ++$parse_count }
is $parse_count, 1, "$ranking_method ranking, single parse";
# reset recognizer and test ambiguity methods
$slr->series_restart();
is $slr->ambiguous(), '', "$ranking_method ranking, single parse, ambiguous status is empty";
is $slr->ambiguity_metric(), 1, "$ranking_method ranking, single parse, ambiguity metric is 1";
}
else{
isnt $slr->ambiguous(), '', "$ranking_method ranking, many parses, ambiguous status isn't empty";
ok $slr->ambiguity_metric() > 1, "$ranking_method ranking, many parses, ambiguity metric > 1";
}
} ## end for my $ranking_method ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment