Hey,
How would the regular expression look that would swap or substitute multiple-word pairs, but not undo itself?
For example:
Because, s/I am/you are/ig and s/you are/I am/ig will contradict each other. Is there a way to just swap them without undoing itself on a reverse substitution? Maybe something like this?
I wanna find out how to do this because I'm working on an A.I. system for chatterbots that has substitutions (things like "what's = what is, you're = you are") and person substitutions (swap 1st and 2nd person pronouns), and the way I'm currently doing it is to split the string into an array of words, and replace individual words, like...
But that only works on single-word substitutions. Ideally it should work for multi-word ones too.
Any help will be appreciated.
-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
How would the regular expression look that would swap or substitute multiple-word pairs, but not undo itself?
For example:
Code:
# Swap 1st and 2nd person pronouns
my %person = (
'I am' => 'you are',
'you are' => 'I am',
'I\'m' => 'you\'re',
'you\'re' => 'I\'m',
'your' => 'my',
'my' => 'your',
);
my $str = "I think you want me to say I'm not your scalar.";
foreach my $key (keys %person) {
$str =~ s/$key/$person{$key}/ig;
}
# The output SHOULD be...
you think I want you to say you're not my scalar
# What it probably WOULD be...
I think you want me to say I'm not your scalar
Because, s/I am/you are/ig and s/you are/I am/ig will contradict each other. Is there a way to just swap them without undoing itself on a reverse substitution? Maybe something like this?
Code:
$str =~ tr/(I am)(you are)/(you are)(I am)/;
I wanna find out how to do this because I'm working on an A.I. system for chatterbots that has substitutions (things like "what's = what is, you're = you are") and person substitutions (swap 1st and 2nd person pronouns), and the way I'm currently doing it is to split the string into an array of words, and replace individual words, like...
Code:
my @words = split(/\s+/, $str);
foreach my $w (@words) {
foreach my $key (keys %person) {
if ($w eq $key) {
$w = $person{$key};
}
push (@new, $w);
}
}
$str = join(" ",@new);
But that only works on single-word substitutions. Ideally it should work for multi-word ones too.
Any help will be appreciated.
-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog