/* PARSE : analyseur dcg  */

/* ---------------------  */

 

:- style_check(-singleton).

 

/* loading getsentence and pretty printer */

 

:- [fgetsen, prpr].

 

/* call : process (enter stop. to stop */

 

     go :-

                 repeat,

                 getsentence(Sentence,user),

                 (Sentence \= [stop] -> true ; abort),

                 parseit(Sentence,_,Parse).

   

/* parsing */

 

 parseit(Sentence,_,Parse):-

                 parse(Sentence,_,Parse),

                 nl,

                 prpr(Parse,0),

                 get_single_char(ZZZ),

                 nl,

                 nl,

                 nl,

                 fail.

 

/* la grammaire */

 

  parse(P0,[],[s,Np,Vp]) :-

    sentence(P0,[],[s,Np,Vp]) .

  

  sentence(P0,P2,[s,Np,Vp]) :-

     nounphrase(P0,P1,Np,Number,Semsubj) ,

     verbphrase(P1,P2,Vp, Number,Semverb),

     sfok(Semverb,Semsubj).

    

  nounphrase(P0,P2,[snp,Det,N],Number,Sem) :-

     determiner(P0,P1,Det,Number) ,

     noun(P1,P2,N,Number,Sem),

     zerook(Det,N,Number,Sem).

 

  nounphrase(P0,P3,[prepnp,Det,N,Prepphrase],Number,Sem) :-

     determiner(P0,P1,Det,Number) ,

     noun(P1,P2,N,Number,Sem),

     prepphrase(P2,P3,Prepphrase,Prep).

    

  nounphrase(P0,P3,[sentnp,Det,Sentn,S],Number,Sem) :-

     determiner(P0,P1,Det,Number) ,

     sentnoun(P1,P2,Sentn,Number,Sem) ,

     sentence(P2,P3,S).

    

  nounphrase(P0,P3,[relnp,Det,N,Relcl],Number,Sem) :-

     determiner(P0,P1,Det,Number),

     noun(P1,P2,N,Number,Sem) ,

     relclause(P2,P3,Relcl).

      

  prepphrase(P0, P2, [prepphrase, [prep(Prep)], Np],Prep):-

     prep(P0,P1,[prep(Prep)],Semlist),

     nounphrase(P1,P2,Np,Number,Sem).

 

  relclause(P0,P3,[relcl,Det,N,Vtr]) :-

     determiner(P0,P1,Det,Number) ,

     noun(P1,P2,N,Number,Sem) ,

     verbtr(P2,P3,Vtr,Number,Semsubj,Semobj),

     sfok(Semsubj,Sem).      

    

 

  verbphrase(P0,P1,[vpi,Vintr],Number,Semsubj) :-

  verbintr(P0,P1,Vintr,Number,Semsubj).

 

  verbphrase(P0,P2,[vpt,Vtr,Np],Number,Semsubj) :-

    verbtr(P0,P1,Vtr,Number,Semsubj,Semobj) ,

    nounphrase(P1,P2,Np,_,Sem),

    sfok(Semobj,Sem).

 

 verbphrase(P0,P2,[vpi,Vintr,Prepphrase],Number,Semsubj) :-

  verbintr(P0,P1,Vintr,Number,Semsubj),

  prepphrase(P1,P2,Prepphrase,Prep).

 

  verbphrase(P0,P3,[vpt,Vtr,Np,Prepphrase],Number,Semsubj) :-

    verbtr(P0,P1,Vtr,Number,Semsubj,Semobj) ,

    nounphrase(P1,P2,Np,_,Sem),

    sfok(Semobj,Sem),

    prepphrase(P2,P3,Prepphrase,Prep) .

 

 verbphrase(P0,P2,[vpprep,Vprep,Prepphrase],Number,Semsubj):-

   vprep(P0,P1,Vprep,Number,Semsubj,Semobj,Prep),

   prepphrase(P1,P2,Prepphrase,Prep).

 

  verbphrase(P0,P3,[vpc,Vcomp,Np1,Np2],Number,Semsubj) :-

    vcomp(P0,P1,Vcomp,Number,Semsubj,Semobj),

    nounphrase(P1,P2,Np1,_,Sem),

    nounphrase(P2,P3,Np2,_,Sem),

    sfok(Semobj,Sem).

 

/* le lexique */

 

  verbintr([thinks|X],X,[vintr(thinks)],sing,human).

  verbintr([think|X],X,[vintr(think)],plural,human).

  verbintr([thought|X],X,[vintr(thought)],_,human).

 

  verbtr([buys|X],X,[vtr(buys)],sing,human,thing).

  verbtr([buy|X],X,[vtr(buy)],plural,human,thing).

  verbtr([bought|X],X,[vtr(bought)],_,human,thing).

  verbtr([considers|X],X,[vtr(considers)],sing,human,_).

  verbtr([consider|X],X,[vtr(consider)],plural,human,_).

  verbtr([considered|X],X,[vtr(considered)],_,human,_).

  verbtr([likes|X],X,[vtr(likes)],sing,human,_).

  verbtr([like|X],X,[vtr(like)],plural,human,_).

  verbtr([liked|X],X,[vtr(liked)],_,human,_).

  verbtr([makes|X],X,[vtr(makes)],sing,_,_).

  verbtr([make|X],X,[vtr(make)],plural,_,_).

  verbtr([made|X],X,[vtr(made)],_,_,_).

  verbtr([reads|X],X,[vtr(reads)],sing,human,reading_matter).

  verbtr([read|X],X,[vtr(read)],plural,human,reading_matter).

  verbtr([read|X],X,[vtr(read)],_,human,reading_matter).

  verbtr([sees|X],X,[vtr(sees)],sing,human,concrete).

  verbtr([see|X],X,[vtr(see)],plural,human,concrete).

  verbtr([saw|X],X,[vtr(saw)],_,human,concrete).

  verbtr([sells|X],X,[vtr(sells)],sing,human,thing).

  verbtr([sell|X],X,[vtr(sell)],plural,human,thing).

  verbtr([sold|X],X,[vtr(sold)],_,human,thing).

 

  vcomp([considers|X],X,[vcomp(considers)],sing,human,_).

  vcomp([consider|X],X,[vcomp(consider)],plural,human,_).

  vcomp([considered|X],X,[vcomp(considered)],_,human,_).

 

  vprep([depends|X],X,[vprep(depends)],sing,_,_,on).

  vprep([depend|X],X,[vprep(depend)],plural,_,_,on).

  vprep([depended|X],X,[vprep(depended)],_,_,_,on).

  vprep([believes|X],X,[vprep(believes)],sing,human,_,in). 

  vprep([believed|X],X,[vprep(believed)],_,human,_,in). 

  vprep([believe|X],X,[vprep(believe)],plural,human,_,in). 

 

  determiner([a|X],X,[det(a)], sing).

  determiner([the|X],X,[det(the)],_).

  determiner([an|X],X,[det(an)],sing).

  determiner(X,X,[det(zero)],_).

  determiner([some|X],X,[det(some)],_).

  determiner([any|X],X,[det(any)],_).

 

  noun([book|X],X,[n(book)],sing,[thing,reading_matter]).

  noun([books|X],X,[n(books)],plural,[thing,reading_matter]).

  noun([claim|X],X,[n(claim)],sing,[abstract]).

  noun([claims|X],X,[n(claims)],plural,[abstract]).

  noun([computer|X],X,[n(computer)],sing,[thing,human]).

  noun([computers|X],X,[n(computers)],plural,[thing,human]).

  noun([man|X],X,[n(man)],sing,[human]).

  noun([men|X],X,[n(men)],plural,[human]).

  noun([oversimplification|X],X,[n(oversimplification)],sing,[abstract]). 

  noun([oversimplifications|X],X,[n(oversimplifications)],plural,[abstract]). 

  noun([woman|X],X,[n(woman)],sing,[human]).

  noun([women|X],X,[n(women)],plural,[human]).

               

  sentnoun([claim|X],X,[sentn(claim)],sing,[abstract]). 

  sentnoun([claims|X],X,[sentn(claims)],plural,[abstract]).  

                                  

  prep([on|X],X,[prep(on)],[place, time]).

  prep([in|X],X,[prep(in)],[place, time]).

 

 

/* semantic hierarchy */

 

 subclass(X,Y) :- ako(X,Y).

 subclass(X,Y) :- ako(X,Z) , subclass(Z,Y).

 

 ako(reading_matter,abstract).

 ako(thing,concrete).

 ako(human,concrete).

 

 

/* checking semantic constraints */

 

 sfok(Sem,Sem) :- !.

 sfok(Sem,Semlist) :- inlist(Sem,Semlist),!.

 sfok(Sem,Semlist) :- subclass(Hypo,Sem), inlist(Hypo,Semlist),!.

 

 inlist(Sem,[Sem|_]) :- !.

 inlist(Sem,[_|Remainder]) :- inlist(Sem,Remainder).

 

 

 

/* accounting for zero articles : a first attempt !!! */

 

zerook([det(zero)],_,plural,_) :- !.

 

zerook([det(zero)],_,_,Semlist) :-

  sfok(abstract,Semlist), !.

 

zerook([det(zero)],[n(man)],_,_) :- !.

 

zerook([det(Det)],_,_,_) :- Det \= zero, !.