現在の 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
| ?-