简体   繁体   English

Prolog-DCG-随机句子

[英]Prolog - DCG - random sentence

I'm new to Prolog, and I'm trying to write a small program that will give a random sentence from a DCG. 我是Prolog的新手,我正在尝试编写一个小程序,该程序将提供DCG中的随机语句。

My previous way of thinking was to use findall/3 to make a list of all possible sentences, and then using random_member/2. 我以前的思维方式是使用findall / 3列出所有可能的句子,然后使用random_member / 2。

It worked for a little while, until the grammar got bigger and I began to get stack errors because of recursion... 它工作了一段时间,直到语法变大,由于递归,我开始出现堆栈错误...

I then thought of another way : make a set of all possible terms at a given moment, applying random_member to get the next term, recursively call this same function, until I get the empty list... 然后我想到了另一种方法:在给定的时刻制作一组所有可能的术语,应用random_member获得下一个术语,递归调用此相同的函数,直到得到空列表...

But how can I get a set of all possible answers to an incomplete predicate ? 但是,如何获得一个不完整谓词的所有可能答案呢? And how can I get it in a set ? 我怎么能把它放在一套呢?

For information, my DCG looks like this: 有关信息,我的DCG如下所示:

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

My idea of the solution (where List is the list of the already concatenated terms ) : 我对解决方案的想法(其中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)>

... ...

Thanks in advance ! 提前致谢 ! :) :)

Seems a though task to me. 对我来说似乎是一项艰巨的任务。 I would tackle it with another strategy, namely inserting selectors in DCG where you want discriminate alternatives. 我会用另一种策略解决它,也就是将选择在DCG你想要区分的替代品。 Something like 就像是

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).

which yields 产生

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM