如何在序言中将"Saya"翻译为"I",如何将"Saya"翻译为"I" [英] how to translate 'Saya' to ' I ' and 'saya' to ' i ' in prolog

查看:111
本文介绍了如何在序言中将"Saya"翻译为"I",如何将"Saya"翻译为"I"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户输入'saya suka makan pisang'时,该程序将基于事实翻译输入,这意味着它将翻译为我喜欢吃香蕉" . ("i"是小写)

when user input 'saya suka makan pisang' this program will translate the input based on the fact, which means it will translate to 'i like eat banana'.(the 'i' is lower case)

如果程序无法识别输入(输入与现有事实不匹配),则程序将保留无法识别的单词并将其显示为输出.例如,用户输入'saya suka makan Pisang',输出'我喜欢吃Pisang'(事实上,Pisang不在我身边)

If the input is not recognized by the program(the input does not match with the existed fact), the program will remain unrecognized words and display it as output. Example, user input 'saya suka makan Pisang', output 'i like eat Pisang' (Pisang is not in my fact)

现在我打算做的是在用户输入"Saya suka makan pisang" 时, 该程序会将其翻译为我喜欢吃香蕉" (我"是大写字母)

now what I intend to do is when user input ' Saya suka makan pisang', the program will translate it as 'I like eat banana'('I' is upper case)

换句话说,该程序需要能够将所有事实转换为大写和小写形式.

In another words, this program need able to translate all the fact in upper and lower case.

words(saya,i).
words(makan,eat).
words(pisang,banana).
words(oren,orange).
words(minum,drink).
words(suka,like).

:- style_check(-singleton).

translation(X,Y):-
    words(X,Y).

translation(X,Y):-
    words(Y,X).

translation(X,X).

translate([], []).

translate([H|T], [H1|T1]):-
             translation(H, H1),
             translate(T,T1).

prolist([],[]).

prolist(SL,[W|T]):-
    split(SL,WL,R),
    name(W,WL),
    prolist(R,T).

split([],[],[]).

split([32|T],[],T).

split([H|T],[H|T2],R):-
    split(T,T2,R).  

run:-
    nl,write('Enter One sentence or word (English or Malay):'),
    read(X),end(X),
    nl.

end(X):-    
X=q->write('SESSION END. THANK YOU. ');
name(X,SL),prolist(SL,List),translate(List,K), atomic_list_concat(K, ' ', W),
nl,
write('Translated as:'),
write(W),
nl,
run.

输入输出

推荐答案

解决方案"是将大写单词添加到您的words/2表中:

The "solution" is to add the capitalized words to your words/2 table:

words(saya,i).
words('Saya', 'I').
words(makan,eat).
words('Makan', 'Eat').
% and so on

您可以在任何文字周围使用单引号使之成为原子.

You can use single quotes around any literal to make it an atom.

这是目前最便宜的出路.

This is the cheapest way out for you at the moment.

如果单词列表很大,那么您可能需要做一些更复杂的事情.例如,您可以在使用words/2之前转换为小写字母,然后在与翻译后的单词匹配之后,如有必要,将其大写.但是所有这些实际上都取决于用例,并且当单词列表很小时,它将需要更多的代码.

If you had a large word list then you might need to do something more complicated. For example, you could convert to lower case before using words/2, then after you have matched the translated word, capitalize it if necessary. But all this really depends on the use case and it will be more code when the list of words is so small.

这篇关于如何在序言中将"Saya"翻译为"I",如何将"Saya"翻译为"I"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆