2
my_list([this,is,a,dog,.,are,tigers,wild,animals,?,the,boy,eats,mango,.]).

suppose this is a list in prolog which i want to divide in three parts that is up to three full stops and store them in variables.

how can i do that...

counthowmany(_, [], 0) :- !.
counthowmany(X, [X|Q], N) :- !, counthowmany(X, Q, N1), N is N1+1.
counthowmany(X, [_|Q], N) :- counthowmany(X, Q, N).
number_of_sentence(N) :- my_list(L),counthowmany(.,L,N).

i already counted the number of full stops in the list(my_list) now i want to divide the list up to first full stop and store it in a variable and then divide up to second full stop and store in a variable and so on.........

repeat
  • 18,496
  • 4
  • 54
  • 166
Ishrak
  • 71
  • 6
  • 18

3 Answers3

4

UPDATE: the code slightly simplified after @CapelliC comment.

One of the many ways to do it (another, better way - is to use DCG - definite clause grammar):

You don't really need counthowmany.

split([], []).
split(List, [Part | OtherParts]) :-
    append(Part, ['.' | Rest], List),
    split(Rest, OtherParts).

Let's try it:

?- my_list(List), split(List, Parts).
List = [this, is, a, dog, '.', tigers, are, wild, animals|...],
Parts = [[this, is, a, dog], [tigers, are, wild, animals], [the, boy, eats, mango]] 
Sergii Dymchenko
  • 6,890
  • 1
  • 21
  • 46
  • 1
    Very nice. Just split/3 seems a bit superflous – CapelliC Feb 26 '14 at 06:58
  • @SergeyDymchenko your solution worked but i made some change in the list(my_list) in the question above and added an interrogative sentences with a "?" sign at the end. i think by just checking with full stop will not do the trick for separating the sentences now, so can you help me please....... – Ishrak Feb 26 '14 at 15:18
  • 1
    I get two answers for `split([a,'.',b,'.'],Parts).` and the second answer is incorrect. – false Feb 26 '14 at 15:23
  • how can i do it if one of the full stop '.' is a question '?' sign – Ishrak Feb 26 '14 at 15:29
  • @Ishrak this method will not work if you have more than one sentence separator. – Sergii Dymchenko Feb 26 '14 at 18:42
3

Your problem statement did not specify what a sequence without a dot should correspond to. I assume that this would be an invalid sentence - thus failure.

:- use_module(library(lambda)).

list_splitted(Xs, Xss) :-
   phrase(sentences(Xss), Xs).

sentences([]) --> [].
sentences([Xs|Xss]) -->
   sentence(Xs),
   sentences(Xss).

sentence(Xs) -->
   % {Xs = [_|_]},  % add this, should empty sentences not be allowed
   allseq(dif('.'),Xs),
   ['.'].

% sentence(Xs) -->
%    allseq(\X^maplist(dif(X),['.',?]), Xs),
%    (['.']|[?]).

allseq(_P_1, []) --> [].
allseq( P_1, [C|Cs]) -->
   [C],
   {call(P_1,C)},
   allseq(P_1, Cs).
repeat
  • 18,496
  • 4
  • 54
  • 166
false
  • 10,264
  • 13
  • 101
  • 209
  • "source_sink 'library(lambda)' does not exist" and "Goal (directive) failed: user:use_module(library(lambda))" i get this two warnings and i am just a beginner in prolog i don't understand your code – Ishrak Feb 26 '14 at 16:51
  • 1
    @Ishrak: What system do you have? YAP has it. You can - always add [this implementation](http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/ISO-Hiord#implementation). But you need it only for the case where you have dot and question mark. – false Feb 26 '14 at 16:55
  • i have this "" SWI-Prolog version 6.4.1 by Jan Wielemaker (jan@swi-prolog.org)"" on windows and in your code some lines are commented out with "%" sign at the beginning. do i need them or not and if you please send me an explanation of your code at my e-mail (ishrak55@live.com) it will be very helpful for me.... – Ishrak Feb 26 '14 at 17:20
1

In this answer we define split_/2 based on splitlistIf/3 and list_memberd_t/3:

split_(Xs, Yss) :-
   splitlistIf(list_memberd_t(['?','.','!']), Xs, Yss).

Sample queries:

?- _Xs = [this,is,a,dog,'.', are,tigers,wild,animals,?, the,boy,eats,mango,'.'], 
   split_(_Xs, Yss).
Yss = [  [this,is,a,dog]   ,[are,tigers,wild,animals] ,[the,boy,eats,mango]   ].

?- split_([a,'.',b,'.'], Yss).
Yss = [[a],[b]].            % succeeds deterministically
Community
  • 1
  • 1
repeat
  • 18,496
  • 4
  • 54
  • 166