gpt4 book ai didi

prolog - 使用 DCG 规则进行拆分

转载 作者:行者123 更新时间:2023-12-02 08:53:02 27 4
gpt4 key购买 nike

我需要使用 swi-prolog 中的规则拆分二氯戊烷这就是我尝试做的

stem--> [meth]|[eth]|[prop]|[but]|[pent].

suf --> [ane]|[ene]|[yne].

pre --> [di]|[tri]|[tetre].

hal--> [floro]|[chloro]|[bromo].

?- split_list(['2','3', dichloropentane],['2','3', di,chloro,pent,ane]).

在英语中,通过使用 Prolog,我需要将二氯戊烷拆分为二,氯,戊烷,如上所示。

我是 Prolog 的真正新手,请任何 Prolog 专家帮助我编写代码。

最佳答案

尝试

any(S, K) --> {member(S,K)}, S.

stem(S) --> any(S, ["meth","eth","prop","but","pent"]).
suf(S) --> any(S, ["ane","ene","yne"]).
pre(S) --> any(S, ["di","tri","tetre"]).
hal(S) --> any(S, ["floro","chloro","bromo"]).

split_list(S,[A,B,C,D]) :- phrase((pre(A),hal(B),stem(C),suf(D)),S).

你应该得到

?- split_list(`dichloropentane`,P).
P = ["di", "chloro", "pent", "ane"]

注意引号的种类:由于SWI-Prolog在版本7中引入了type字符串,DCG编码需要更加小心......

编辑

?- atom_codes(dichloropentane,Cs), split_list(Cs,L).
Cs = [100, 105, 99, 104, 108, 111, 114, 111, 112|...],
L = ["di", "chloro", "pent", "ane"]

关于prolog - 使用 DCG 规则进行拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32200658/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com