直接在Prolog中對語法進行編碼是一個非常繁瑣的過程。是的,你可以做到這一點,但如果你剛剛開始學習Prolog,那麼你並不處於最佳狀態。事實上,科學花了很多年才提出了一個特別高效的編碼–,並且當了解這種編碼時,Prolog就誕生了!
你需要的是語法– definite clause grammarsdcg。不要試圖在Prolog(現在)中瞭解他們的編碼,只需使用phrase/2
就可以了!
這是程序3.11從Prolog and Natural-Language Analysis由 Fernando C. N. Pereira和Stuart M. Shieber。佩雷拉設計了我們今天使用的DCG規則的風格。這本書是最被低估的Prologbooks之一。它是免費的!
s(s(NP,VP)) --> np(NP), vp(VP).
np(np(Det,N,Rel)) -->
det(Det),
n(N),
optrel(Rel).
np(np(PN)) --> pn(PN).
vp(vp(TV,NP)) --> tv(TV), np(NP).
vp(vp(IV)) --> iv(IV).
optrel(rel(epsilon)) --> [].
optrel(rel(that,VP)) --> [that], vp(VP).
pn(pn(terry)) --> [terry].
pn(pn(shrdlu)) --> [shrdlu].
iv(iv(halts)) --> [halts].
det(det(a)) --> [a].
n(n(program)) --> [program].
tv(tv(writes)) --> [writes].
如果你要代表詞典作爲Prolog的事實,代替
n(n(program)) --> [program].
而
n(n(W)) --> [W],{noun(W)}.
noun(program).
所以使用,讓我們使用它:
?- phrase(s(P), Xs).
P = s(np(det(a),n(program),rel(epsilon)),vp(tv(writes),np(det(a),n(program),rel(epsilon)))),
Xs = [a,program,writes,a,program] ;
P = s(np(det(a),n(program),rel(epsilon)),vp(tv(writes),np(det(a),n(program),rel(that,vp(tv(writes),np(det(a),n(program),rel(epsilon))))))),
Xs = [a,program,writes,a,program,that,writes,a,program] ...
什麼是自我參照話語!但現在,所有的句子了長度是一個比較有啓發性:
?- length(Xs, N), phrase(s(P), Xs).
Xs = [terry,halts],
N = 2,
P = s(np(pn(terry)),vp(iv(halts))) ;
Xs = [shrdlu,halts],
N = 2,
P = s(np(pn(shrdlu)),vp(iv(halts))) ;
Xs = [a,program,halts],
N = 3,
P = s(np(det(a),n(program),rel(epsilon)),vp(iv(halts))) ;
Xs = [terry,writes,terry],
N = 3,
P = s(np(pn(terry)),vp(tv(writes),np(pn(terry)))) ;
Xs = [terry,writes,shrdlu],
N = 3,
P = s(np(pn(terry)),vp(tv(writes),np(pn(shrdlu)))) ;
Xs = [shrdlu,writes,terry],
N = 3,
P = s(np(pn(shrdlu)),vp(tv(writes),np(pn(terry)))) ...
請提供你想要實現的文法的一些編碼(EBNF)或輸入/輸出的一些例子。順便說一句:檢查明文句法,他們會簡化代碼並避免很多樣板 –