0

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 *"
1
  • I'm not clear what you mean by "define a set of strings". Do you mean find the capitalized word that is next to "is"? Do you mean find the subject of the sentence? Do you mean find the last noun in the sentence? Do you mean find the first word that is different from all the others in the sentence? In any event, this sounds like a natural language processing task. Please clarify what you're trying to accomplish, exactly. Thanks. Commented Jul 18, 2019 at 14:44

1 Answer 1

1
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 *
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.