I was playing with the widgetwords konfabulator widget and wrote a script that calculates the top scoring words for each round. The way the game is played is you get a set of letters and have to come up with as many dictionary words as possible with those letters. Of course, to calculate the top scoring words requires a search like you are requesting. My main constraint is that the word has to be between 3 and 8 characters.
#!/usr/bin/perl # authored by Josh Goldberg <josh@3io.com> # Jan 10, 2004 # # ARGV[0] is a string of letters, ARGV[1] is a dictionary file. # the script searches the dictionary for all combinations/permutations # of the ARGV[0] that have at least three characters and no character +is # repeated. # Originally authored for use with the widgetWords konfabulator widget +. use Algorithm::Permute; $|=1; $resultstoprint = 7; $defaultDictionary = "joshWordList.txt"; @tilePoints = ( 100,300,300,200,100,400,200,400,100,800,500,100,300,10 +0,100,300,1000,100,100,100,100,400,400,800,400,1000 ); $vowels = ( 65,69,73,79,85,89 ); $common = ( 84,78,83,72,82,68,76 ); sub combinations { my @list= @_; my @pick= (0) x @list; return sub { my $i= 0; while( 1 < ++$pick[$i] ) { $pick[$i]= 0; return if $#pick < ++$i; } return @list[ grep $pick[$_], 0..$#pick ]; }; } $file = $ARGV[1] || $defaultDictionary; die "missing dictionary" unless -e $file; @letters = split //,lc $ARGV[0]; # permute all combinations of 3-8 letters $combinations = combinations(@letters); while (@comb = $combinations->() ) { next unless scalar @comb > 2; $p = new Algorithm::Permute(\@comb); while (@res = $p->next) { local $"=''; $wordlist{"@res"} = 1; } } open LIST, "<$file"; while (<LIST>) { chomp; $dict{$_} = 1; } foreach $word (keys %wordlist) { if (exists $dict{$word}) { push @matches, $word; @res = split //,$word; $score = 0; for (@res) { $score += $tilePoints[ord(uc $_)-65]; } $len = scalar @res; $score += $len * 50; $score += 400 if $len == 8; $wordlist{$word} = $score; } } close LIST; print "top Eight Words:\n"; @sorted = reverse sort {$wordlist{$a} <=> $wordlist{$b} } @matches; for (@sorted) { if ($resultstoprint > 0) { last if $top++ > $resultstoprint; } @res = split //,$_; print "$_: $wordlist{$_} points\n"; } exit 0;

In reply to Re: Finding words within a character set by bageler
in thread Finding words within a character set by ruhk

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.