3

次の 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]ですか?

4

1 に答える 1

3

現在の 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
| ?-
于 2016-01-07T23:36:37.600 に答える