如何在 swi-prolog 中分割句子
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用atomic_list_concat/3。请参阅
http://www.swi-prolog.org/pldoc/man? predicate=atomic_list_concat%2F3
通常它是为了插入一个分隔符,但由于 Prolog 统一的双向性,它也可以用来分割一个字符串给定分隔符:
当然一旦分割完成后你可以使用列表 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:
Of course once the split is done you can play with the elements of the list L.
我喜欢“pat fats”的答案,但你必须先将字符串转换为原子:
...,atom_codes(Atom, String),atomic_list_concat(L, ' ', Atom), ...
如果您需要直接使用字符串,我的“武器库”中有此代码:
作为 DCG,必须以这种方式调用:
编辑: 更多说明
我忘了解释它是如何工作的:@larsman 在这个其他答案中很好地解释了 DCG。我引用他的话
这里我有 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':
being a DCG, must be called in this way:
edit: more explanation
I forgot to explain how that works: DCG are well explained by @larsman, in this other answer. I cite him
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...
?-split("这是一个字符串"," ", Out)。
Out=["这个","是","a","字符串"]
?-split("this is a string"," ", Out).
Out=["this","is","a"," string"]