Reputation: 3
How do i find a pattern that can define a set of strings? For example if I have these strings
my @single = (
"Hello World my name is Alice",
"Hello World my name is Bob",
"Hello World my name is Charlie"
);
my @multi = (
"That Building is very small",
"That Ladder is very tall"
);
What i currently have tried is to separate the sentences into groups with the exact amount of words. Then build a tree where the nodes are the words, if a node has many branches they will be replaced by a *. But this only works if the * is at the end (@single) but it won't work when the * is not at the end (@multi)
Basically What I want is to output a pattern where the input are an array of strings. How can i generate these patterns below given the strings above?
my $single_pattern = "Hello World my name is *"
my $multi_pattern = "That * is very *"
Upvotes: 0
Views: 180
Reputation: 39158
use 5.010;
use Perl6::Junction qw(all);
sub pattern_from_wordset {
my (@wordset) = @_;
my @transposed;
for my $string (@wordset) {
my @parts = split / /, $string;
while (my ($index, $part) = each @parts) {
push $transposed[$index]->@*, $part;
}
}
my @pattern;
for my $words (@transposed) {
push @pattern, (all($words->@*) eq $words->[0])
? $words->[0]
: '*';
}
return @pattern;
}
my @single = …
my @multi = …
say join ' ', pattern_from_wordset @single;
say join ' ', pattern_from_wordset @multi;
__END__
Hello World my name is *
That * is very *
Upvotes: 1