Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I am stuck at a matching problem. I have an array which contains a list of terms and their translations, and I want to check if a word is contained in a string of text. I loop my array, and if a match is found I print out the translation. This is an example of my array:
my @dic= ( ['animal source food','aliment d’origine animale'], ['balanced diet','régime alimentaire équilibré'], ['food','aliment'], ['nutrition','nutrition'], ['nutrition assessment','évaluation de l’état nutritionnel'] );
I perform my matching with the following basic script (being $string my string of text:
foreach my $row (@glossary){ my $TermDBSource=@$row[0]; my $TermDBTarget=@$row[1]; if ($string=~ /$TermDBSource/){ print "$TermDBTarget\n"; last; } }
So far so good. My requirement is to match the longest n-gram. As I use a loop, the first match found in my array will be displayed. This could be a problem in a case where $string='this is a nutrition assessment' as it will match first 'nutrition' and not 'nutrition assessment' as desired. If I take out last it will match both 'nutrition' and 'nutrition assessment', again no good. The only idea I came up with is to order my array alphabetically (easy to do) and by length of n-gram (need to look at how to solve it, but I found https://perlmonks.com/?node_id=1219394 which may help me).
My question is more on the approach. Do you think this is the right approach, or I am complicated things too much and there are other more straightforward methods to achieve my goal?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Matching wirth ordered array
by choroba (Cardinal) on Mar 18, 2019 at 22:52 UTC | |
|
Re: Matching wirth ordered array
by BillKSmith (Monsignor) on Mar 19, 2019 at 03:43 UTC | |
|
Re: Matching wirth ordered array
by AnomalousMonk (Archbishop) on Mar 19, 2019 at 01:03 UTC | |
|
Re: Matching wirth ordered array
by AnomalousMonk (Archbishop) on Mar 19, 2019 at 09:35 UTC |