Sosi has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to search some arrays for the best possible matches. For instance, given the @source and @search lists:
my @source = ("John Ronald Reuel Tolkien","John Ronald S Tolkien","Tre +nt Reznor","Barack Hussein Obama II","Barack Hussein II"); #note that + the second item is wrong and should be discarded! my @search = ("John Ronald Reuel T","Trent Reznor","Barack Hussein II" +,"Barack Hussein Obama II","No match here");
I would like to associate the @search list with the best match in @source list. I'm figuring this can be done with a search pattern with several ORs, but I'm stuck. Please see my example below:
#!/usr/local/bin/perl use strict; use warnings; my @source = ("John Ronald Reuel Tolkien","John Ronald S Tolkien","Tre +nt Reznor","Barack Hussein Obama II","Barack Hussein II"); my @search = ("John Ronald Reuel T","Trent Reznor","Barack Hussein II" +,"Barack Hussein Obama II","No match here"); print "twonames\t\talternativesearch\n"; foreach my $s (@search){ #gets first two names (my $twonames=$s)=~s/^(\w+ \w+).*$/$1/; #gets all other names, if they exist (my $others=$s)=~s/^(\w+ \w+)//; if ($others){ #deletes initial space (my $alternativesearch=$others)=~s/^\s//; $alternativesearch=~s/\s/\|/g; print "$twonames\t\t$alternativesearch\n"; } else { print "$twonames\t\tNO OTHER NAMES PRESENT\n"; } } #prints # twonames alternativesearch # John Ronald Reuel|T # Trent Reznor NO OTHER NAMES PRESENT # Barack Hussein II # Barack Hussein Obama|II
In this search I would like to have an association between @search items and @source items that would yield the best match. Something like:
# search source # John Ronald Reuel T John Ronald Reuel Tolkien # Trent Reznor Trent Reznor # Barack Hussein Obama II Barack Hussein Obama II # No match here
Note that, in the case of Obama it matched the whole array, in the first line it matched the two first words plus something else, and in the last case it found nothing. How would you proceed to find the best match? (i.e. the match where the search sentence would be mostly contained in the source sentence?) Thanks
Edit: this was crossposted on StackOverflow
Edit2: Even though I used people's names in my example, my real case has no people's names in case that matters.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Searching for best match
by Athanasius (Archbishop) on Oct 06, 2014 at 13:21 UTC | |
|
Re: Searching for best match
by Eily (Monsignor) on Oct 06, 2014 at 12:30 UTC | |
by Sosi (Sexton) on Oct 06, 2014 at 12:54 UTC | |
|
Re: Searching for best match
by Anonymous Monk on Oct 06, 2014 at 12:20 UTC | |
by Sosi (Sexton) on Oct 06, 2014 at 12:57 UTC | |
by Sosi (Sexton) on Oct 06, 2014 at 12:47 UTC | |
by Anonymous Monk on Oct 06, 2014 at 15:39 UTC | |
|
Re: Searching for best match
by choroba (Cardinal) on Oct 06, 2014 at 12:13 UTC | |
by Sosi (Sexton) on Oct 06, 2014 at 12:40 UTC | |
|
Re: Searching for best match
by Laurent_R (Canon) on Oct 06, 2014 at 22:20 UTC |