在解析字符串列表时,当前规则允许逗号位于句子的末尾,并且不允许逗号在单词之间,这显然是不正确的。如何修改当前规则?
complexsentence --> [] ; simplesentence, complexsentence;
simplesentence, delimiter, complexsentence.
simplesentence --> noun, verb.
delimiter --> [,].
noun --> [sun].
verb --> [shines].
% TEST: phrase(complexsentence, [sun,shines]). %must be TRUE
% TEST: phrase(complexsentence, [sun,',',shines]). %must be TRUE
% TEST: phrase(complexsentence, [sun,shines,',']). %must be FALSE
更新: 试图像这样解决它。它的工作原理
complexsentence --> [] ; simplesentence,complexsentence.
simplesentence --> noun,verb;
noun,verb,delimiter,noun;
noun,verb,delimiter,noun,verb.
delimiter -->[,].
noun-->[sun].
verb-->[shines].
% TEST: phrase(complexsentence, [sun,shines]). % TRUE
% TEST: phrase(complexsentence, [sun,',',shines]). % FALSE
% TEST: phrase(complexsentence, [sun,shines,',']). %FALSE
答案 0 :(得分:3)
对DCG的建议更改:
complexsentence --> simplesentence. %was []
complexsentence --> simplesentence, complexsentence.
complexsentence --> simplesentence, delimiter, complexsentence.
simplesentence --> noun, verb.
delimiter --> [,].
noun --> [sun].
verb --> [shines].
然后:
% TEST: phrase(complexsentence, [sun,shines]). %will be TRUE
% TEST: phrase(complexsentence, [sun,',',shines]). %will be FALSE (as it should)
% TEST: phrase(complexsentence, [sun,shines,sun,shines]). %will be TRUE
% TEST: phrase(complexsentence, [sun,shines,',',sun,shines]). %will be TRUE
% TEST: phrase(complexsentence, [sun,shines,',']). %will be FALSE
我建议第二个测试应该是FALSE,因为问题描述表明, ...并且不允许逗号在单词之间。
尽管原始子句(complexsentence --> [].
)为complexsentence
的递归定义提供了终止案例,但它有两个问题:(1)它将空句定义为最简单的复句,( 2)它允许在有效的复杂句子上使用尾随分隔符(通过第3个子句,因为你可以获得simplesentence, delimiter, []
。使用complexsentence --> simplesentence.
作为基本案例更具逻辑意义并将第二个问题修复为奖励,因为你的其他案例在这种情况下是合乎逻辑的。