in reply to retain longest multi words units from hash

why not: Hint: you can also delete hash slices.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

update

OK I see the problem, this is only partially solving your case:

use strict; use warnings; use Data::Dump qw/pp dd/; my %hash = ( 'rendition' => '3', 'automation' => '2', 'saturation' => '3', 'mass creation' => 2, 'automation technology' => 2, # why did you post broken code with a missing comma? 'automation technology process' => 3 ); delete @hash{ map { split / / } grep{/ /} keys %hash }; print pp \%hash;
{ "automation technology" => 2, "automation technology process" => 3, "mass creation" => 2, "rendition" => 3, "saturation" => 3, }

Replies are listed 'Best First'.
Re^2: retain longest multi words units from hash
by Corion (Patriarch) on Jul 28, 2018 at 11:41 UTC

    Another hint - sort the keys in order of ascending length, so you have all the short words ready for deletion in the hash when the longer words get examined.

      The other way round, yes. (delete short words)

      But it's more complicated, he want's to delete 2 word groups if they are contained in a 2+n word group.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: retain longest multi words units from hash
by LanX (Saint) on Jul 28, 2018 at 14:45 UTC
    OK supposing you have at most 4 words in a group (otherwise expand $max_del)

    use strict; use warnings; use Data::Dump qw/pp dd/; use feature 'say'; my %hash = ( 'rendition' => '3', 'automation' => '2', 'saturation' => '3', 'mass creation' => 2, 'automation technology' => 2, 'automation technology process' => 3 ); # prepare sub-slices my @slices; my $max_del = 3; for my $del ( 0 .. $max_del ) { for my $i (0 .. $del) { for my $l ( $i .. $del) { push @{ $slices[$del] }, [$i .. $l] unless $i==0 and $l == $del; # exclude identity } } } sub partitions { my $group =shift; my @words = split / /,$group; return map { join " ", @words[@$_] } @{$slices[$#words]} } delete @hash{ partitions($_) } for keys %hash ; say "Result: ", pp \%hash; say "# --- tests"; say '@slices = ', pp \@slices; say "partitions('automation technology process limit') = " , pp partitions('automation technology process limit');

    Result: { "automation technology process" => 3, "mass creation" => 2, "rendition" => 3, "saturation" => 3, } # --- tests @slices = [ [], [[0], [1]], [[0], [0, 1], [1], [1, 2], [2]], [[0], [0, 1], [0, 1, 2], [1], [1, 2], [1, 2, 3], [2], [2, 3], [3]], ] partitions('automation technology process limit') = ( "automation", "automation technology", "automation technology process", "technology", "technology process", "technology process limit", "process", "process limit", "limit", )

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re^2: retain longest multi words units from hash
by Anonymous Monk on Jul 28, 2018 at 12:02 UTC

    Thank you for the suggestion (PS: sorry for having missed the comma!). This approach is interesting, still doesn't solve the problem of recursiveness, i.e. deleting 2 units words in a 3 multi words unit. Or, for what it matters, of a 3 multi words unit in a 4 multi words unit.

    Probably:

    • sort keys small to big
    • loop over the keys
    • check each key if it is contained in all other keys, if yes delete it, if not retain it

    This approach should work, but I am not sure if it make sense in terms of performances with big hashes

      > This approach should work, but I am not sure if it make sense in terms of performances with big hashes

      I had the same idea, performance depends on the nature of your data. After sorting you'd have O(n²) i.e. (n**2 -n)/2  n *(n+1) * 1/2 == (n**2 + n)/2 comparisons from smaller in bigger keys.*

      NB: you need to specify what words "are" and be careful not to delete "automation tech" because of "automation technology"

      In my approach you'd calculate all subgroups of key (5 for "automation technology process") and delete them.

      Performance depends then on the number of long groups you have.

      As already mentioned, I don't know the nature of your data ²...

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      *) this can be improved by preparing a lookup table for single words, "automation technology" would listed for "automation" and "technology", so as soon you see "automation technology process" you only need to check the entries in the lookup table.

      ²) when looking for algorithms check the mathematics of Posets

      UPDATE

      I think we had the same question here already, better check the archives.