I'm programming this bot response scripting language called RiveScript, and one of its features is to substitute first- and second-person pronouns in the bot's reply.
For instance:
So when you would say "do you think i should talk to you?" it would reply "Yes I think you should talk to me", swapping the first- and second-person pronouns.
The code I currently have splits the text to be substituted into an array at the spaces, then substitutes each word one at a time, then joins it back together. This is to get past the possible error of:
That code would, of course, convert all "you's" to "me's" and then back again, cancelling itself out.
The problem is, it would be helpful if two-word pairs could be substituted around. But since the code currently splits it into words, this isn't possible.
For an example of why you'd want two-word pairs to be substituted is because, sometimes "you" should translate into "me" and sometimes into "I"
Here's the code I currently have for running these substitutions, to prevent it from contradicting itself:
So long story short, I'm looking for a way to substitute multiple-word items for other multiple-word items.
For instance:
Code:
+ do you *
- Yes I {person}<star>{/person}
So when you would say "do you think i should talk to you?" it would reply "Yes I think you should talk to me", swapping the first- and second-person pronouns.
The code I currently have splits the text to be substituted into an array at the spaces, then substitutes each word one at a time, then joins it back together. This is to get past the possible error of:
Code:
$var =~ s/you/me/ig;
$var =~ s/me/you/ig;
That code would, of course, convert all "you's" to "me's" and then back again, cancelling itself out.
The problem is, it would be helpful if two-word pairs could be substituted around. But since the code currently splits it into words, this isn't possible.
For an example of why you'd want two-word pairs to be substituted is because, sometimes "you" should translate into "me" and sometimes into "I"
Code:
you are => i am
i am => you are
Here's the code I currently have for running these substitutions, to prevent it from contradicting itself:
Code:
# where $self->{person}->{you} = me
# etc.
sub person {
my ($self,$msg) = @_;
# Lowercase the string.
$msg = lc($msg);
# Get the words and run substitutions.
my @words = split(/\s+/, $msg);
my @new = ();
foreach my $word (@words) {
if (exists $self->{person}->{$word}) {
$word = $self->{person}->{$word};
}
push (@new, $word);
}
# Reconstruct the message.
$msg = join (' ',@new);
return $msg;
}
So long story short, I'm looking for a way to substitute multiple-word items for other multiple-word items.