#!/usr/local/bin/swipl -q -g start -s % formula2counts - auxiliary corpus script % Copyright (C) 2004-2007 Richard Moot (Richard.Moot@labri.fr) % Copyright (C) 2004-2007 CNRS (http://www.cnrs.fr) % Copyright (C) 2004-2007 INRIA (http://www.inria.fr) % This library is free software; you can redistribute it and/or % modify it under the terms of the GNU Lesser General Public % License as published by the Free Software Foundation; either % version 2.1 of the License, or (at your option) any later version. % This library is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU % Lesser General Public License for more details. % You should have received a copy of the GNU Lesser General Public % License along with this library; if not, write to the Free Software % Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA % This script take a Prolog file containing a formula array and % outputs the count check of each of the items - this means the % differences between the positive and negative occurrences of % np, s, n and pp formulas. If other atomic formulas are found, % they will be reported at the end of the run. % this version of formula2counts simply continues if it encounters an % formula constructor or atomic formula it doesn't recognize. % formula2pol.pl.20050119 would fail in these situations. % missing items are kept in an ordered set and printed after the file % `counts' is saved. start :- consult(formula), get_directory(Dir), working_directory(Old, Dir), tell('counts'), findall(N-F, formula(N, F), List), sort(List, Set), process(Set), working_directory(_, Old), halt. get_directory(Dir) :- prolog_flag(argv, List), last(List, Final), ( Final = -- -> working_directory(Dir, Dir) ; Final = swipl -> working_directory(Dir, Dir) ; Dir = Final ). process([]). process([X|Xs]) :- process1(Xs, X, [], M), portray_list(M). process1([], N-F, M0, M) :- count(F, A, B, C, D, E, M0, M), format('~w ~w ~w ~w ~w~n', [A, B, C, D, E, F, N]), told. process1([X|Xs], N-F, M0, M) :- count(F, A, B, C, D, E, M0, M1), format('~w ~w ~w ~w ~w~n', [A, B, C, D, E, F, N]), process1(Xs, X, M1, M). count(dl(_,F,F), 0, 0, 0, 0, 0, M, M) :- !. count(dr(_,F,F), 0, 0, 0, 0, 0, M, M) :- !. count(F, A, B, C, D, E, M0, M) :- count0(F, 0, A, 0, B, 0, C, 0, D, 0, E, M0, M). count0(s, A0, A, B, B, C, C, D, D, E, E, M, M) :- !, A is A0+1. count0(np, A, A, B0, B, C, C, D, D, E, E, M, M) :- !, B is B0+1. count0(n, A, A, B, B, C0, C, D, D, E, E, M, M) :- !, C is C0+1. count0(pp, A, A, B, B, C, C, D0, D, E, E, M, M) :- !, D is D0+1. count0(let, A, A, B, B, C, C, D, D, E0, E, M, M) :- !, E is E0+1. count0(p(_,P,Q), A0, A, B0, B, C0, C, D0, D, E0, E, M0, M) :- !, count0(P, A0, A1, B0, B1, C0, C1, D0, D1, E0, E1, M0, M1), count0(Q, A1, A, B1, B, C1, C, D1, D, E1, E, M1, M). count0(dl(_,P,Q), A0, A, B0, B, C0, C, D0, D, E0, E, M0, M) :- !, count1(P, A0, A1, B0, B1, C0, C1, D0, D1, E0, E1, M0, M1), count0(Q, A1, A, B1, B, C1, C, D1, D, E1, E, M1, M). count0(dr(_,P,Q), A0, A, B0, B, C0, C, D0, D, E0, E, M0, M) :- !, count0(P, A0, A1, B0, B1, C0, C1, D0, D1, E0, E1, M0, M1), count1(Q, A1, A, B1, B, C1, C, D1, D, E1, E, M1, M). count0(X, A, A, B, B, C, C, D, D, E, E, M0, M) :- ord_insert(M0, X, M). count1(s, A0, A, B, B, C, C, D, D, E, E, M, M) :- !, A is A0-1. count1(np, A, A, B0, B, C, C, D, D, E, E, M, M) :- !, B is B0-1. count1(n, A, A, B, B, C0, C, D, D, E, E, M, M) :- !, C is C0-1. count1(pp, A, A, B, B, C, C, D0, D, E, E, M, M) :- !, D is D0-1. count1(let, A, A, B, B, C, C, D, D, E0, E, M, M) :- !, E is E0-1. count1(p(_,P,Q), A0, A, B0, B, C0, C, D0, D, E0, E, M0, M) :- !, count1(P, A0, A1, B0, B1, C0, C1, D0, D1, E0, E1, M0, M1), count1(Q, A1, A, B1, B, C1, C, D1, D, E1, E, M1, M). count1(dl(_,P,Q), A0, A, B0, B, C0, C, D0, D, E0, E, M0, M) :- !, count0(P, A0, A1, B0, B1, C0, C1, D0, D1, E0, E1, M0, M1), count1(Q, A1, A, B1, B, C1, C, D1, D, E1, E, M1, M). count1(dr(_,P,Q), A0, A, B0, B, C0, C, D0, D, E0, E, M0, M) :- !, count1(P, A0, A1, B0, B1, C0, C1, D0, D1, E0, E1, M0, M1), count0(Q, A1, A, B1, B, C1, C, D1, D, E1, E, M1, M). count1(X, A, A, B, B, C, C, D, D, E, E, M0, M) :- ord_insert(M0, X, M). % = portray_list(+List) % % prints the elements of List one item per line portray_list([]). portray_list([X|Xs]) :- format('Missing items:~n'), portray_list(Xs, X). portray_list([], X) :- format(' ~p~n',[X]). portray_list([X|Xs], Y) :- format(' ~p,~n', [Y]), portray_list(Xs, X). % From the DEC-10 public domain Prolog library, written by % Richard A. O'Keefe. % ord_insert(+Set1, +Element, ?Set2) % is the equivalent of add_element for ordered sets. It should give % exactly the same result as merge(Set1, [Element], Set2), but a bit % faster, and certainly more clearly. ord_insert([], Element, [Element]). ord_insert([Head|Tail], Element, Set) :- compare(Order, Head, Element), ord_insert(Order, Head, Tail, Element, Set). ord_insert(<, Head, Tail, Element, [Head|Set]) :- ord_insert(Tail, Element, Set). ord_insert(=, Head, Tail, _, [Head|Tail]). ord_insert(>, Head, Tail, Element, [Element,Head|Tail]).