How to split a sentence in swi-prolog

I am trying to use SWI-Prolog in win xp. I'm trying to figure out how to split a sentence in Prolog into separate atoms.

Ex: Let's say I have a suggestion like this:

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

like:

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

Can someone explain how this works?

Thanks.

+4
source share
3 answers

I would use atomic_list_concat / 3. Cm

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

It is usually intended to insert a delimiter, but because of the bi-directional union of the Prolog unification, it can also be used to split a delimited string:

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

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

+10
source

I like the answer "pat fats", but you need 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, should be invoked as follows:

 ?- 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 this works: DCG is well explained by @larsman, in this other answer. I quote him

-> which actually adds two hidden arguments to it. The first one is a list to be analyzed according to the grammar rule; the second - "what remains" after parsing. c (F, X, []) calls c in the list X to get the result of F, expecting [] to remain on the left, i.e. the parser must consume the entire list of X.

Here I have 2 arguments, the first is a separator, the second is a list under construction. The built-in line // 1 comes from the SWI-Prolog library (http / dcg_basics). This is a very convenient building block that literally matches anything on return. Here he "eats" each char before the delimiter or end of line. By doing this, we can recursively ...

+4
source

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

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

-5
source

All Articles