Find all possible combinations of words in given format

Viewed 132

I have 100 000 lines long dictionary:

w([w,o,r,d]).
w([h,a,p,p,y]).
w([q,u,e,s,t,i,o,n]).
...

Now I am working on a script that will return all possible words, that meet given format.

For example:

w([A,B,C]), w([B,C]), A \== B, A \== C, B \== C.

I've found the source that makes all variables different:

alldif([]).
alldif([E|Es]) :-
   maplist(dif(E), Es),
   alldif(Es).

So now I call:

w([A,B,C]), w([B,C]), alldif([A,B,C]).

Now I want that the variable A is one of [a,e,i,o,t,l]. I can do that using:

member(A, [a,e,i,o,t,l]).

But is it faster (?) using constraint programming with:

A in [a,e,i,o,t,l]

and

all_different([A,B,C]).

I am kind of stuck right now. The idea's been to generate all possible options in .txt file line by line.

I managed to concatenate words into statement using:

g([A,B,C], W1), g([B,C], W2), alldif([A,B,C]), buildStat([W1,W2], Statement).

Where:

g(Format, Word):-
    list_to_set(Format, Uniques),
    alldif(Uniques),
    w(Format),
    atomic_list_concat(Format, '', Atom), atom_string(Atom, Word).

insertSpaces([A], [A]).
insertSpaces([Word | Rest], OutWords):-
    insertSpaces(Rest, OutWordsRest),
    OutWords = [Word, " " | OutWordsRest].


buildStat(Words, Statement):-
    insertSpaces(Words, OutWords),
    with_output_to(atom(Statement), maplist(write, OutWords)).

But I don't know how to save all possibible Statements into file line by line. Help would be appreciated.

1 Answers
Related