hodge-podge has asked for the wisdom of the Perl Monks concerning the following question:

As practice I am trying to write a simply anagram solver. My basic idea about the structure is that the script loads a dictionary, and finds words that have the exact same letters as the scrambled word. Simple.

The problem that I am afraid I will run into though is parsing and searching a large dictionary file. What is the most efficient way to do this?

Replies are listed 'Best First'.
Re: Parsing Large files
by zwon (Abbot) on Apr 30, 2010 at 18:12 UTC

    Well, here's my solution:

    use strict; use warnings; use 5.010; open my $dfd, '<', '/usr/share/dict/british-english' or die $!; my %dict; while (<$dfd>) { next unless /^[a-z]+$/; push @{$dict{hash($_)}}, $_; } close $dfd; while (<>) { exit if /^$/; say join '', @{$dict{hash($_)}} if $dict{hash($_)}; } sub hash { join '', sort split //, shift; }
      Wow, well thanks a lot, your solution is a lot more eloquent than mine would have been. Surprisingly fast to. I follow it for the most part but I get tripped up in a couple of places. If I am not mistaken you are opening the dictionary as output, loads all the words into a hash. Then gets input...but what exactly are you doing here?  say join '', @($dict{hash($_)}} if $dict{hash($_)}; Thanks for the help.

        If you don't understand a data structure, you should use Data::Dumper.

        If the dictionary contained

        act cat dog god zebra

        It builds the following hash

        %dict = ( act => [ 'act', 'cat' ], dgo => [ 'dog', 'god' ], aberz => [ 'zebra' ], );

        If the user enters dog, it searches for dgo (hash("dog")) and lists what it finds (dog and god).

Re: Parsing Large files (anagram)
by toolic (Bishop) on Apr 30, 2010 at 19:23 UTC
Re: Parsing Large files
by Marshall (Canon) on Apr 30, 2010 at 20:16 UTC
    I don't think that there is such a thing as a "simple" anagram solver! The letters of "George Bush" are "beegghorsu". I don't see a computer algorithm that could come up with the anagram: "He bugs Gore". I am curious as to how you propose to do that?

      Try Bugs Gore He It leaves it to the human to permute the words.

      I don't see a computer algorithm

      It doesn't mean it doesn't exist

      (Oops, redundant post)