This is what I managed to do after reading the various answers. I consider a multi words unit (MWU) to be contained in a bigger one only if the sequence of words is the same (so technology process is contained in automation technology process', but 'automation process is not). I do not have other constraints I can think of.

Be aware, my code is very verbose....

use strict; use warnings; use Data::Dump qw/pp dd/; my %output; my %phrases = ( 'rendition' => '3', 'automation' => '2', 'saturation' => '3', 'mass creation' => 2, 'automation technology' => 2, 'automation technology process' => 3, 'technology process' => 5, 'automation process' => 2, ); foreach my $keyPhrases (sort hashKeyLength (keys(%phrases))) { my $TermToBeRetained=1; print "\nWorking on * $keyPhrases *\n"; foreach my $keyOutput (keys %output){ print "\tChecking if * $keyPhrases * is contained in * $keyOut +put *: "; if ($keyOutput =~/$keyPhrases/){ print "Yes\n"; $TermToBeRetained=0; last; } else{ print "No!\n"; $TermToBeRetained=1; } } if ($TermToBeRetained eq 1){ print "Retaining $keyPhrases\n"; $output{$keyPhrases} = $phrases{$keyPhrases};; } else{ print "Skipping $keyPhrases\n"; } } print "MY RESULT\n"; print pp \%output; sub hashKeyLength { count_units_in_phrase($b) <=> count_units_in_phrase($a) || $b cmp +$a } sub count_units_in_phrase { my $string=shift; my $num; $num++ while $string =~ /\S+/g; return $num }

What do you think of it?

Discipuls added code tags 28 July 2018


In reply to Re: retain longest multi words units from hash by Anonymous Monk
in thread retain longest multi words units from hash by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.