【问题标题】:Prolog: generate a term from a list according to DCGProlog:根据 DCG 从列表中生成一个术语
【发布时间】:2016-01-07 22:23:05
【问题描述】:

我有以下 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] 列表中生成术语?

【问题讨论】:

    标签: list prolog dcg parse-tree


    【解决方案1】:

    您只需要扩展您当前的 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
    | ?-
    

    【讨论】:

    • 非常感谢!正是我想要的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-27
    相关资源
    最近更新 更多