http://www.perlmonks.org?node_id=300993

Assuming you have a dictionary file, this will work. Give it args like "bed time", and you'll get back "debit me" as well as "bet I'm Ed".
#!/usr/bin/perl use constant DEBUG => 1; use warnings; use strict; my @val = ('\0', map "[\\0-\\$_]", 1 .. 255); (my $chars = lc join "", @ARGV) =~ tr/a-z//cd; my $words = get_words("/usr/dict/words", $chars); add_words($words, qw( a I I'm you're he's she's we're they're I'll you'll he'll she'll we'll they'll I've you've we've they've it's that's what's isn't can't won't don't doesn't )); # this populate %anagrams my %anagrams; anagrams(frequency($chars), \%anagrams); sub get_words { my ($f, $c) = @_; my $l = length $c; my @w; open F, "< $f" or die "can't read $f: $!"; while (<F>) { chomp; next if $l < length or /[^$c]/oi; push @{ $w[length]{(frequency(lc))[0]} }, $_; } close F; return \@w; } sub add_words { my $w = shift; push @{ $w->[length $_->[1]]{(frequency(lc $_->[1]))[0]} }, $_->[0] for map [ $_, do { (my $x = $_) =~ tr/a-zA-Z//cd; $x } ], @_; } sub anagrams { my ($str, $len, $out, $tmp, $prune) = @_; my $rx = freq_to_rx($str); $prune ||= @$words - 1; if ($len == 0) { for (expand(@$tmp)) { warn " > $_\n" if $out->{join " ", sort split ' '}++ == 0 and DEBUG; } return; } for (reverse(1 .. $prune)) { my $l = $words->[$_]; for my $w (grep /$rx/, keys %$l) { my $p = ($_, $len - $_)[$_ > $len/2]; push @$tmp, $l->{$w}; anagrams(remove($str, $w, $len), $out, $tmp, $p); pop @$tmp; } } } sub frequency { my $s = "\0" x 26; my $len = length $_[0]; ++vec($s, ord($_) - ord('a'), 8) for split //, shift; return ($s, $len); } sub remove { my ($s, $r, $l) = @_; my $o = 0; vec($s, $o++, 8) -= ord, $l -= ord for split //, $r; return ($s, $l); } sub freq_to_rx { my $rx = join "", @val[map ord, split //, shift]; qr/^$rx$/; } sub expand { return @{ +shift } if @_ == 1; return map { my $f = $_; map "$f $_", expand(@_) } @{ +shift }; }

_____________________________________________________
Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Replies are listed 'Best First'.
Re: Multi-Word Anagrams
by Anonymous Monk on Oct 21, 2003 at 18:55 UTC
    So I thought I'd see what TMTOWDI might bring about:
    There's more than one to do it demonstration toy: hear the woe thee whore toy: a demonstration
    8^)
Re: Multi-Word Anagrams
by Anonymous Monk on Oct 27, 2003 at 18:36 UTC
    Unfortunately it doesn't quite work:
    Can't use an undefined value as an ARRAY reference at ./multi-word-ana +grams line 99.
      I had the same problem as A.M. above, but changing @{ +shift } to @{ shift @_ } in ln. 99 (last line of expand subroutine) does the trick (@_ is necessary b/c @{ shift } is interpreted as @shift). ;)
        That's odd. @{ +shift } should be the same as @{ shift(@_) }. What version of Perl mistook it for @shift?

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Could you explain how you got that error?

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

        Of course: I copy-and-paste your script, save the file, make it executable and - boom! - as I try to use it, the problem occurs. I don't change anything...