How do you program a translator with lists in prolog?
-
26-10-2019 - |
Question
What I want to do is a translator in Prolog. I've done something like this to translate one word:
traducir(X,Y) :- traduccion( X, Y ).
traduccion(gato,cat).
And when you ask Prolog traducir(X,cat)
, it answers X=gato
What I want to do is a translator where you ask something like traducir(X,[Hola,mi,nombre,es,Juan]).
and Prolog's answer should be X=[Hello,my,name,is,John]
.
Solution
Here is another one :
traducir(Xs, Ys) :- maplist(traduccion, Xs, Ys).
OTHER TIPS
traducir([],[]).
traducir([Hin|Tin], [Hout|Tout]) :-
traduccion(Hin, Hout),
traducir(Tin,Tout).
Also add a catchall predicate
tranduccion(X,X)
at the bottom of your logicbase, if you want to include names of people, that are not translated.
To translate several languages into several others, you can write a predicate like this one:
translate(Lang1,X,Lang2,Y) :-
Translations = [
[
[spanish,perro],
[english,dog],
[french,chien],
[german,hund],
[italian,cane]
],
[
[french,chat],
[spanish,gato],
[english,cat]
]
%You can add more lists of translations here if necessary.
],
member(Translation,Translations),
member([Lang1,X],Translation),
member([Lang2,Y],Translation).
Now you can define the traducir
predicate like this:
traducir(X,Y) :- translate(spanish,X,english,Y).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow