Skip to content

Commit

Permalink
Fix regexp for optionals
Browse files Browse the repository at this point in the history
  • Loading branch information
kirsle committed Oct 11, 2015
1 parent cd5c1f6 commit 9624e7d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Revision history for Perl extension RiveScript.

1.40 Oct 10 2015
- Fix the regexp used when matching optionals so that the triggers don't match
on inputs where they shouldn't. (RiveScript-JS issue #46)

1.38 Jul 21 2015
- New algorithm for handling variable tags (<get>, <set>, <add>, <sub>,
<mult>, <div>, <bot> and <env>) that allows for iterative nesting of these
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ defines the standards of RiveScript.

# CHANGES

1.40 Oct 10 2015
- Fix the regexp used when matching optionals so that the triggers don't match
on inputs where they shouldn't. (RiveScript-JS issue #46)

1.38 Jul 21 2015
- New algorithm for handling variable tags (<get>, <set>, <add>, <sub>,
<mult>, <div>, <bot> and <env>) that allows for iterative nesting of these
Expand Down
11 changes: 7 additions & 4 deletions lib/RiveScript.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package RiveScript;
use strict;
use warnings;

our $VERSION = '1.38'; # Version of the Perl RiveScript interpreter.
our $VERSION = '1.40'; # Version of the Perl RiveScript interpreter.
our $SUPPORT = '2.0'; # Which RS standard we support.
our $basedir = (__FILE__ =~ /^(.+?)\.pm$/i ? $1 : '.');

Expand Down Expand Up @@ -2856,10 +2856,9 @@ sub _reply_regexp {
my @parts = split(/\|/, $1);
my @new = ();
foreach my $p (@parts) {
$p = '\s*' . $p . '\s*';
$p = '(?:\s|\b)+' . $p . '(?:\s|\b)+';
push (@new,$p);
}
push (@new,'\s*');

# If this optional had a star or anything in it, e.g. [*],
# make that non-matching.
Expand All @@ -2868,7 +2867,7 @@ sub _reply_regexp {
$pipes =~ s/\(\\d\+\)/(?:\\d+)/ig; # (\d+) --> (?:\d+)
$pipes =~ s/\(\\w\+\)/(?:\\w+)/ig; # (\w+) --> (?:\w+)

my $rep = "(?:$pipes)";
my $rep = "(?:$pipes|(?:\\s|\\b)+)";
$regexp =~ s/\s*\[(.+?)\]\s*/$rep/i;
}

Expand Down Expand Up @@ -3367,6 +3366,10 @@ L<http://www.rivescript.com/> - The official homepage of RiveScript.
=head1 CHANGES
1.40 Oct 10 2015
- Fix the regexp used when matching optionals so that the triggers don't match
on inputs where they shouldn't. (RiveScript-JS issue #46)
1.38 Jul 21 2015
- New algorithm for handling variable tags (<get>, <set>, <add>, <sub>,
<mult>, <div>, <bot> and <env>) that allows for iterative nesting of these
Expand Down
2 changes: 1 addition & 1 deletion lib/RiveScript/WD.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package RiveScript::WD;
use strict;
use warnings;

our $VERSION = '1.38';
our $VERSION = '1.40';

# This is not a real module; it's only a current copy of the RiveScript
# Working Draft. See the latest version at
Expand Down
21 changes: 20 additions & 1 deletion t/RiveScript.t
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# RiveScript Unit Tests
use utf8;
use strict;
use Test::More tests => 145;
use Test::More tests => 156;

binmode(STDOUT, ":utf8");

Expand Down Expand Up @@ -212,6 +212,12 @@ push @tests, sub {
+ [please|can you] ask me a question
- Why is the sky blue?
+ (aa|bb|cc) [bogus]
- Matched.
+ (yo|hi) [computer|bot] *
- Matched.
");
test($rs, 'what are you', 'I am a robot.', 'Alternatives 1.');
test($rs, 'what is you', 'I am a robot.', 'Alternatives 2.');
Expand All @@ -227,6 +233,19 @@ push @tests, sub {
'Optionals 2.');
test($rs, 'please ask me a question', 'Why is the sky blue?',
'Optionals 3.');

test($rs, "aa", "Matched.", "Optionals 4.");
test($rs, "bb", "Matched.", "Optionals 5.");
test($rs, "aa bogus", "Matched.", "Optionals 6.");
test($rs, "aabogus", $MATCH, "Optionals 7.");
test($rs, "bogus", $MATCH, "Optionals 8.");

test($rs, "hi Aiden", "Matched.", "Optionals 9.");
test($rs, "hi bot how are you?", "Matched.", "Optionals 10.");
test($rs, "yo computer what time is it?", "Matched.", "Optionals 11.");
test($rs, "yoghurt is yummy", $MATCH, "Optionals 12.");
test($rs, "hide and seek is fun", $MATCH, "Optionals 13.");
test($rs, "hip hip hurrah", $MATCH, "Optionals 14.");
};

push @tests, sub {
Expand Down

0 comments on commit 9624e7d

Please sign in to comment.