我有以下 DCG:
s --> np, vp.
np --> det, n.
vp --> v.
det --> [the].
n --> [cat].
v --> [sleeps].
我可以验证类似的句子s([the,cat,sleeps], [])
,我得到回复“ yes
”。
但我需要这句话作为一个术语,比如:s(np(det(the),n(cat)),vp(v(sleeps)))
。
如何从列表中生成术语[the,cat,sleeps]
?
我有以下 DCG:
s --> np, vp.
np --> det, n.
vp --> v.
det --> [the].
n --> [cat].
v --> [sleeps].
我可以验证类似的句子s([the,cat,sleeps], [])
,我得到回复“ yes
”。
但我需要这句话作为一个术语,比如:s(np(det(the),n(cat)),vp(v(sleeps)))
。
如何从列表中生成术语[the,cat,sleeps]
?
您只需要扩展您当前的 DCG 以包含一个定义您所追求的术语的参数:
s(s(NP, VP)) --> np(NP), vp(VP).
np(np(Det, Noun)) --> det(Det), n(Noun).
vp(vp(Verb)) --> v(Verb).
det(det(the)) --> [the].
n(n(cat)) --> [cat].
v(v(sleeps)) --> [sleeps].
然后你用它来调用它phrase
:
| ?- phrase(s(X), [the, cat, sleeps]).
X = s(np(det(the),n(cat)),vp(v(sleeps)))
代码可能看起来有点混乱,因为您想要的术语名称恰好与您选择的谓词名称相匹配。重命名谓词,使其更清晰:
sentence(s(NP, VP)) --> noun_part(NP), verb_part(VP).
noun_part(np(Det, Noun)) --> determiner(Det), noun(Noun).
verb_part(vp(Verb)) --> verb(Verb).
determiner(det(the)) --> [the].
noun(n(cat)) --> [cat].
verb(v(sleeps)) --> [sleeps].
| ?- phrase(sentence(X), [the, cat, sleeps]).
X = s(np(det(the),n(cat)),vp(v(sleeps)))
如果您想通过例如包含更多名词来增加它,您可以这样做:
noun(n(N)) --> [N], { member(N, [cat, dog]) }.
使用一般查询结果:
| ?- phrase(sentence(X), L).
L = [the,cat,sleeps]
X = s(np(det(the),n(cat)),vp(v(sleeps))) ? a
L = [the,dog,sleeps]
X = s(np(det(the),n(dog)),vp(v(sleeps)))
(1 ms) yes
| ?-