大家好
我对我的部分研究项目有问题。我的任务是在prolog中编写一个程序,该程序可以根据用户的输入告诉您您有什么问题。数据库必须从格式由我决定的文件中读取。
建设:
我决定有 2 个动态规则;
:- dynamic (illness/2).
:- dynamic (symptoms/4).
哪里:
illnes(name_of_illness, symptoms(symptom1, symptom2, symptom3, symptom4)
文件:示例.txt:
flu,cough,fever,head_acke, runny_nose.
measles, rash, fever, sore_throat, inflamed_eyes.
问题:
我的主要问题是格式化此数据以使用 asserta predicat,我尝试了很多方法,但没有奏效。
谢谢
所以,根据你的另一个问题,我认为你可以用split_string/4
解析这些字符串,你的问题是结果不是原子,然后你需要正确地构建结构。我认为这是你缺少的部分:
?- split_string("this,that,the,other,thing", ",", " ", X),
maplist(atom_string, [Condition,Sym1,Sym2,Sym3,Sym4], X),
Result = illness(Condition, symptoms(Sym1,Sym2,Sym3,Sym4)).
X = ["this", "that", "the", "other", "thing"],
Condition = this,
Sym1 = that,
Sym2 = the,
Sym3 = other,
Sym4 = thing,
Result = illness(this, symptoms(that, the, other, thing)).
如果您只是asserta(Result)
您已经将正确的东西添加到数据库中。
如果你的症状数量可变,你应该把它放在一个列表中,这将大大简化你的处理(可能是我们的下游代码,因为连续四次做任何事情都有点重复):
?- split_string("this,that,the,other,thing", ",", " ", X),
maplist(atom_string, [Condition|Symptoms], X),
Result = illness(Condition, symptoms(Symptoms)).
X = ["this", "that", "the", "other", "thing"],
Condition = this,
Symptoms = [that, the, other, thing],
Result = illness(this, symptoms([that, the, other, thing])).