【问题标题】:Prolog - DCG - random sentenceProlog - DCG - 随机句子
【发布时间】:2013-04-28 17:19:09
【问题描述】:

我是 Prolog 的新手,我正在尝试编写一个小程序,它会从 DCG 中随机给出一个句子。

我之前的想法是使用findall/3来列出所有可能的句子,然后使用random_member/2。

它工作了一段时间,直到语法变大并且由于递归而开始出现堆栈错误......

然后我想到了另一种方法:在给定时刻制作一组所有可能的术语,应用 random_member 获取下一个术语,递归调用相同的函数,直到我得到空列表...

但是我怎样才能得到一组不完整谓词的所有可能答案呢?我怎样才能在 set 中获得它?

有关信息,我的 DCG 如下所示:

s --> pronoun(X), verb(X), location.
pronoun(1) --> [i].
pronoun(2) --> [you].
verb(1) --> [am].
verb(2) --> [are].
location --> [here].
location --> [there].

我对解决方案的想法(其中 List 是已经连接的术语的列表):

createRandomSentence(List) :- 
    setof(H, s([List|[H|_]], []), Set),
    random_member(Pick, Set),
    append(List, [Pick], List2)
    <recursive call (haven't figured out this one either yet)>

...

提前致谢! :)

【问题讨论】:

    标签: random prolog dcg


    【解决方案1】:

    对我来说似乎是一项任务。我会用另一种策略来解决这个问题,即在 DCG 中插入 selectors 你想要区分替代品的地方。类似的东西

    s --> pronoun(X), verb(X), location.
    pronoun(1) --> {this}, [i].
    pronoun(2) --> [you].
    verb(1) --> [am].
    verb(2) --> [are].
    location --> {this},[here].
    location --> [there].
    
    % here choice just between 2
    this :- random(0,2,1).
    

    产生

    ?- phrase(s,L).
    L = [i, am, there] ;
    L = [you, are, there].
    
    ?- phrase(s,L).
    L = [you, are, there].
    

    【讨论】:

    • 我不知道这是可能的!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多