如何在 swi-prolog 中分割句子

发布于 2024-09-27 22:55:13 字数 223 浏览 9 评论 0原文

我正在尝试在 win xp 中使用 SWI-Prolog。我试图了解如何将 Prolog 中的句子分成单独的原子。

例如:假设我有一个这样的句子:

“这是一个字符串”
有什么方法可以将单个单词存储在变量中吗?

就像:

X = 这个
Y = 是
....
等等。

谁能解释一下这是如何工作的吗?

谢谢。

I am trying my hands on SWI-Prolog in win xp. I am trying to understand how to split a sentence in Prolog into separate atoms.

Ex : Say I have a sentence like this :

"this is a string"
Is there any way to get individual words to get stored in a variable?

like :

X = this
Y = is
....
and so forth.

Can anyone please explain how this works?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最丧也最甜 2024-10-04 22:55:13

我会使用atomic_list_concat/3。请参阅

http://www.swi-prolog.org/pldoc/man? predicate=atomic_list_concat%2F3

通常它是为了插入一个分隔符,但由于 Prolog 统一的双向性,它也可以用来分割一个字符串给定分隔符:

atomic_list_concat(L,' ', 'This is a string').
L = ['This',is,a,string]

当然一旦分割完成后你可以使用列表 L 的元素。

I would use atomic_list_concat/3. See

http://www.swi-prolog.org/pldoc/man?predicate=atomic_list_concat%2F3

Normally it is meant to insert a separator but because of Prolog's bidirectionality of unification, it can also be used to split a string given the separator:

atomic_list_concat(L,' ', 'This is a string').
L = ['This',is,a,string]

Of course once the split is done you can play with the elements of the list L.

知你几分 2024-10-04 22:55:13

我喜欢“pat fats”的答案,但你必须先将字符串转换为原子:

...,atom_codes(Atom, String),atomic_list_concat(L, ' ', Atom), ...

如果您需要直接使用字符串,我的“武器库”中有此代码:

%%  split input on Sep
%
%   minimal implementation
%
splitter(Sep, [Chunk|R]) -->
    string(Chunk),
    (   Sep -> !, splitter(Sep, R)
    ;   [], {R = []}
    ).

作为 DCG,必须以这种方式调用:

?- phrase(splitter(" ", L), "this is a string"), maplist(atom_codes, As, L).
L = [[116, 104, 105, 115], [105, 115], [97], [115, 116, 114, 105, 110|...]],
As = [this, is, a, string] .

编辑: 更多说明

我忘了解释它是如何工作的:@larsman 在这个其他答案中很好地解释了 DCG。我引用他的话

-->,实际上向其中添加了两个隐藏参数。第一个是要由语法规则解析的列表;第二个是解析后的“剩下的”。 c(F,X,[]) 在列表 X 上调用 c 以获得结果 F,期望留下 [],即解析器应该消耗整个列表 X。

这里我有 2 个参数,第一个是分隔符,第二个是正在构建的列表。内置字符串//1来自SWI-Prolog库(http/dcg_basics)。这是一个非常方便的构建块,几乎可以匹配回溯中的任何内容。这里它“吃掉”分隔符字符串结尾之前的每个字符。完成后,我们可以递归......

I like the answer of 'pat fats', but you have to convert your string to atom before:

..., atom_codes(Atom, String), atomic_list_concat(L, ' ', Atom), ...

If you need to work directly with strings, I have this code in my 'arsenal':

%%  split input on Sep
%
%   minimal implementation
%
splitter(Sep, [Chunk|R]) -->
    string(Chunk),
    (   Sep -> !, splitter(Sep, R)
    ;   [], {R = []}
    ).

being a DCG, must be called in this way:

?- phrase(splitter(" ", L), "this is a string"), maplist(atom_codes, As, L).
L = [[116, 104, 105, 115], [105, 115], [97], [115, 116, 114, 105, 110|...]],
As = [this, is, a, string] .

edit: more explanation

I forgot to explain how that works: DCG are well explained by @larsman, in this other answer. I cite him

-->, which actually adds two hidden arguments to it. The first of these is a list to be parsed by the grammar rule; the second is "what's left" after the parse. c(F,X,[]) calls c on the list X to obtain a result F, expecting [] to be left, i.e. the parser should consume the entire list X.

Here I have 2 arguments, the first it's the separator, the second the list being built. The builtin string//1 come from SWI-Prolog library(http/dcg_basics). It's a very handy building block, that match literally anything on backtracking. Here it's 'eating' each char before the separator or the end-of-string. Having done that, we can recurse...

那支青花 2024-10-04 22:55:13

?-split("这是一个字符串"," ", Out)。

Out=["这个","是","a","字符串"]

?-split("this is a string"," ", Out).

Out=["this","is","a"," string"]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文