Re: Finding matching words in a word game

From: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
To: Alexander Farber <alexander(dot)farber(at)gmail(dot)com>
Cc: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Finding matching words in a word game
Date: 2013-03-05 16:58:34
Message-ID: CAMkU=1wnMHiN6ULsKGSvFGVFufPii5fTime8Y_nSwe879uqAqQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Mar 5, 2013 at 1:29 AM, Alexander Farber <alexander(dot)farber(at)gmail(dot)com
> wrote:

> Hello,
>
> is there maybe a clever way of finding all possible words
> from a given set of letters by means of PostgreSQL
> (i.e. inside the database vs. scanning all database
> rows by a PHP script, which would take too long) -
> if the dictionary is kept in a simple table like:
>
> create table good_words (
> word varchar(16) primary key,
> stamp timestamp default current_timestamp
> );
>
> I could add a column above, where same letters as in "word"
> would be sorted alphabetically... but then I don't know.
>

Yes, that is how I'd do it. Then you'd just build an index on the sorted
column and select on the sorted letters.

You could do the canonicalization (sorting of the letters) in PHP for every
query (and insert) and pass in that value, and so have the database not
know what the meaning of the new column is.

select word from good_words were sorted_letters=?

Or you could create a function in the database that does the sort, and then
your PHP would not have to pass in already sorted letters.

select word from good_words were sorted_letters=sort_function(?),

and also have triggers automatically compute and store the "sorted_letters"
from "word" upon insert or update.

Cheers,

Jeff

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2013-03-05 18:23:12 Re: [HACKERS] Floating point error
Previous Message Alexander Farber 2013-03-05 16:52:58 Re: Finding matching words in a word game