So i have predicate called palavras_possiveis_esp(Leters,Spaces,Space,Words_pos)
, Leters which is a list of words like [[d,a,y],[n,i,g,h,t],[s,u,n],[m,o,o,n]]
, Spaces is list of lists like [[P11,P12,P13],[P11,P21,P31,P41],[P13,P23,P33]]
, a Space is list with variables like this [P11,P12,P13]
and Words_pos is all the words that can be unified with a certain Space.
Firstly to do this, i did this predicate espacos_pal_uni(Spaces,Leters)
and the purpose of this predicate is to unify all the Esps with any of the words in Ltrs
Example:
?- espacos_pal_uni([[d, P21, P31, P41, P51],[a, P23, P33, P43, P53]],[[a,m,e,n,o],[a,t,o],[d,a,o],[d,r,a,m,a],[m,a,e],[m,a,n,d,e],[s,e,d,e],[s,o,a,r]]).P21 = r,
P31 = P51, P51 = a,
P41 = P23, P23 = m,
P33 = e,
P43 = n,
P53 = o.
So as you can see from the example the Space [d, P21, P31, P41, P51]
was unified with the word drama and the Space [a, P23, P33, P43, P53]
was unified with the word ameno.
Secondly, i created this predicate palavra_possivel_esp(Word, Space, Spaces, Leters)
and the purpose of this predicate is unify a Space with Word, if they are, then they unify Esps that have common variables with Esp with any of the words in Ltrs
Example:
?- Letras = [[a,m,e,n,o],[a,t,o],[d,a,o],[d,i,a],[d,r,a,m,a],[m,a,e],[m,a,n,d,e],[s,e,d,e],[s,o,a,r]], Espacos = [[P11, P12, P13], [P15, P16, P17, P18],
[P23, P24, P25],
[P35, a, P37],
[P41, P42, P43, P44, P45],
[P11, P21, P31, P41, P51],
[P13, P23, P33, P43, P53],
[P15, P25, P35, P45],
[P17, P27, P37]],
palavra_possivel_esp([d,i,a], [P11,P12,P13], Espacos, Letras).
P11 = P35, P35 = d,
P12 = i,
P13 = P31, P31 = P51, P51 = a,
P21 = r,
P23 = P41, P41 = m,
P33 = e,
P37 = P53, P53 = o,
P43 = n,
Letras = [[a, m, e, n, o], [a, t, o], [d, a, o], [d, i, a], [d, r, a, m|...], [m, a, e], [m, a|...], [s|...], [...|...]],
Espacos = [[d, i, a], [P15, P16, P17, P18], [m, P24, P25], [d, a, o], [m, P42, n, P44|...], [d, r, a|...], [a, m|...], [P15|...], [...|...]].
?- Leters = [[a,m,e,n,o],[a,t,o],[d,a,o],[d,i,a],[d,r,a,m,a],[m,a,e],[m,a,n,d,e],[s,e,d,e],[s,o,a,r]], Spaces = [[P11, P12, P13], [P15, P16, P17, P18],
[P23, P24, P25],
[P35, a, P37],
[P41, P42, P43, P44, P45],
[P11, P21, P31, P41, P51],
[P13, P23, P33, P43, P53],
[P15, P25, P35, P45],
[P17, P27, P37]],
palavra_possivel_esp([d,a,o], [P11,P12,P13], Spaces, Leters).
1) So whats happening here is the predicate unifies the word [d,i,a]
with [P11,P12,P13]
, and then it checks the Spaces that have variables in common with that Space, which are [[P11, P21, P31, P41, P51], [P13, P23, P33, P43, P53]]
, then it will se if there are any word in Leters that unifies with [[d, P21, P31, P41, P51], [a, P23, P33, P43, P53]]
which are the words ameno and drama.
2) As for the word dao the predicate unifies the word [d,a,o]
with [P11,P12,P13]
, and then it checks the Esps that have variables in common with that Sapce, which are [[P11, P21, P31, P41, P51], [P13, P23, P33, P43, P53]]
, then it will se if there are any word in Leters that in unifies with [[d, P21, P31, P41, P51], [o, P23, P33, P43, P53]]
, the first word is unifiable with drama but since there's no word starting with o, then it returns False.
Thirdly, i created a predicate palavras_possiveis_esp(Leters, Spaces Space, Words_Pos),
which Words_Pos is a list of all words unifiable with a certain space.
Example:
?- Letras = [[a,m,e,n,o],[a,t,o],[d,a,o],[d,i,a],[d,r,a,m,a],[m,a,e],[m,a,n,d,e],[s,e,d,e],[s,o,a,r]], Espacos = [[P11, P12, P13], [P15, P16, P17, P18],
[P23, P24, P25],
[P35, a, P37],
[P41, P42, P43, P44, P45],
[P11, P21, P31, P41, P51],
[P13, P23, P33, P43, P53],
[P15, P25, P35, P45],
[P17, P27, P37]],
palavras_possiveis_esp(Letras, Espacos,[P11,P12, P13], Pals_Possiveis).
P11 = P35, P35 = d,
P12 = i,
P13 = P31, P31 = P51, P51 = a,
P21 = r,
P23 = P41, P41 = m,
P33 = e,
P37 = P53, P53 = o,
P43 = n,
Letras = [[a, m, e, n, o], [a, t, o], [d, a, o], [d, i, a], [d, r, a, m|...], [m, a, e], [m, a|...], [s|...], [...|...]],
Espacos = [[d, i, a], [P15, P16, P17, P18], [m, P24, P25], [d, a, o], [m, P42, n, P44|...], [d, r, a|...], [a, m|...], [P15|...], [...|...]],
Pals_Possiveis = [[d, i, a]].
The problem here is that if there is more than 1 word that unifies with a Space, the predicate returns a list with the 1st word instead of the list with all the words.
The problem i think is that when it searches for the 1st word and unifies it with the space, since the space will no longer be a list of variables then it will always be different than the rest of the words and it will return the 1st word and i cant see how i fix this.
I think the major problem is in the espacos_pal_uni predicate.
Program:
espacos_pal_uni([],_) :- true.
espacos_pal_uni([E|RE],LP) :- member(E,LP),
espacos_pal_uni(RE,LP).
palavra_possivel_esp(Pal, Esp, Esps, Letras) :-
length(Pal,C1),
length(Esp,C2),
C1 == C2,
Pal = Esp,
espacos_com_posicoes_comuns(Esps,Esp,NEsps),
espacos_pal_uni(NEsps,Letras),!.
npalavra_possivel_esp(P, Esp, Esps, Ltrs) :- \+ palavra_possivel_esp(P, Esp, Esps, Ltrs).
palavras_possiveis_esp(Ltrs,Esps,Esp,Pals) :-
palavras_possiveis_esp(Ltrs,Esps,Esp,Pals,Ltrs,[]).
palavras_possiveis_esp(_,_,_,AC,[],AC) :- !.
palavras_possiveis_esp(Ltrs,Esps,Esp,Pals,[P|R],AC) :-
palavra_possivel_esp(P, Esp, Esps, Ltrs),
append(AC,[P],NAC),
palavras_possiveis_esp(Ltrs,Esps,Esp,Pals,R,NAC).
palavras_possiveis_esp(Ltrs,Esps,Esp,Pals,[P|R],AC) :-
npalavra_possivel_esp(P, Esp, Esps, Ltrs),