From 0e17dea2711c3a1af4d2153b2c1f9f26b7018aed Mon Sep 17 00:00:00 2001 From: Ryan Voots Date: Fri, 30 Oct 2020 22:52:04 -0700 Subject: [PATCH] looks ok --- cpanfile | 1 + plugins/factoids.pm | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/cpanfile b/cpanfile index 2a20261..fc23cae 100644 --- a/cpanfile +++ b/cpanfile @@ -61,6 +61,7 @@ requires "POE::Session"; requires "POE::Wheel::ReadWrite"; requires "POE::Wheel::Run"; requires "POE::Wheel::SocketFactory"; +requires "PPI"; requires "Regexp::Assemble"; requires "Regexp::Optimizer"; requires "Socket"; diff --git a/plugins/factoids.pm b/plugins/factoids.pm index 9bc8d06..2a1535c 100644 --- a/plugins/factoids.pm +++ b/plugins/factoids.pm @@ -10,6 +10,8 @@ use Text::Metaphone; use strict; use Encode qw/decode/; use JSON::MaybeXS qw/encode_json/; +use PPI; +use PPI::Dumper; use Data::Dumper; use List::Util qw/min max/; @@ -553,13 +555,20 @@ sub get_fact_substitute ($self, $subject, $name, $said) { my ($aliasserver, $aliasnamespace) = $self->get_alias_namespace($said); my ($server, $namespace) = $self->get_namespace($said); - if ( ($said->{body} =~ m{^(?:\s*substitute)?\s*(.*?)\s*=~\s*s /([^/]+ ) /([^/]* )/([gi]*)\s*$}ix) - || ($said->{body} =~ m{^(?:\s*substitute)?\s*(.*?)\s*=~\s*s\|([^|]+ ) \|([^|]* )\|([gi]*)\s*$}ix) - || ($said->{body} =~ m{^(?:\s*substitute)?\s*(.*?)\s*=~\s*s\{([^\}]+)\}\{([^\}]*?)\}([gi]*)\s*$}ix) - || ($said->{body} =~ m{^(?:\s*substitute)?\s*(.*?)\s*=~\s*s\(([^)]+ )\)\(([^)]*? )\)([gi]*)\s*$}ix) - || ($said->{body} =~ m{^(?:\s*substitute)?\s*(.*?)\s*=~\s*s <([^>]+ ) > <([^>]*? ) >([gi]*)\s*$}ix)) + if ($said->{body} =~ m{^(?:\s*substitute)?\s*(.*?)\s*=~\s*s(.*)$}ix) { - my ($subject, $match, $subst, $flags) = ($1, $2, $3, $4); + my ($subject, $regex) = ($1, $2); + my $pdoc = PPI::Document->new(\$regex); + return "Failed to parse $regex" unless $pdoc; + + # TODO handle tr|y/// + my $token = $pdoc->find(sub {$_[1]->isa('PPI::Token::Regexp::Substitute')})->[0]; + + return "Couldn't find s/// in $regex" unless $token; + + my $match = $token->get_match_string; + my $subst = $token->get_substitute_string; + my $flags = join '', keys +{$token->get_modifiers()}->%*; # TODO does this need to be done via the ->get_fact() instead now? my $fact = $self->_db_get_fact(_clean_subject($subject), 0, $server, $namespace); @@ -572,11 +581,6 @@ sub get_fact_substitute ($self, $subject, $name, $said) { #moving this to its own function for cleanliness $result = $self->_fact_substitute($pred, $match, $subst, $flags); - # my( $self, $body, $name, $said ) = @_; - - # $body =~ s/^\s*learn\s+//; - # my( $subject, $predicate ) = split /\s+as\s+/, $body, 2; - # TODO why is this calling there? # let this fail for now my $ret = $self->get_fact_learn("learn $subject as $result", $name, $said, $subject, $result);