/* MWU'S : an ndfa representation */

% ----------------------------------

 

:- style_check(-singleton).

 

% GO step

 

go :- nl,

     write('A NON-DETERMINISTIC FINITE AUTOMATON FOR MWU RECOGNITION'),nl,

     write('--------------------------------------------------------'),

     nl,nl,

     write('A.Michiels, University of Liège'),nl,nl,

     write('Input file?  [stdin. or file_name.] --> '),

     read(Input),

     dealwith(Input,HandleIn),

     write('Output file? [file_name.] --> '),

     read(Output),

     concat(Output,'.lst',Outlist),

     !,

     start(Outlist,HandleIn,Input).

 

 

% DEALING WITH INPUT

% Input is from stdin(user's terminal) or from file

 

% standard input

dealwith(Input,_):-

                   Input=stdin,!.

 

 

% input from a file

dealwith(FileIn,HandleIn):-

                   open(FileIn,read,HandleIn).

 

 

% standard input

start(Outlist,_,stdin) :-

             open(Outlist,write,Lists),

                 recorda(out,Lists,_),

             repeat,

                 nl,

                 write('Key in your sentence or stop. to quit'),

             nl, 

             getsentence(Sentence,user),

                    % getting the sentence to be parsed from the user's terminal

               process(Mwuname,Sentence,Lists),

                 fail.

 

 

% input from a file

start(Outlist,HandleIn,Input) :-

             Input \= stdin,

             open(Outlist,write,Lists),

                 recorda(out,Lists,_),

             nl,

             repeat,

                 nl, 

             write('>>'),

                 getsentence(Sentence,HandleIn),

                 write(Sentence),nl,

             process(Mwuname,Sentence,Lists),

                 fail.

 

 

% PROCESSING

 

% stopping...

process(Mwuname,Sentence,Lists) :-

               Sentence = [stop],

               close(Lists),

               abort.

 

% normal processing

process(Mwuname,Word_List,Output_Stream) :-

                    ndfa(Mwu_name,Word_List,Results),

                    writelist(Results,Output_Stream).

 

% NDFA

 

/* stepping down a word list in search of a multi-word unit */

 

ndfa(Mwu_name,Word_List,[Mwu_name,initial(Initial_State)|Mwu]) :-

      initial(Mwu_name,Initial_State),

        traverse(Mwu_name,Initial_State,Word_List,Mwu).

 

% standard move

traverse(Mwu_name,

          State,

          [Word|Word_List],

          [transition(from(State),

                      to(Next_State),

                      reading(Word),

                      of_category(Lemma))|Lemma_List]) :-

         transition(Mwu_name,State,Next_State,Lemma),

         link(Word,Lemma),

         traverse(Mwu_name,Next_State,Word_List,Lemma_List).

 

% silent move

traverse(Mwu_name,

          State,

          Word_List,

          [transition(from(State),

                      to(Next_State),

                      silent_move)|Lemma_List]) :-

         transition(Mwu_name,State,Next_State,silent_move),

         traverse(Mwu_name,Next_State,Word_List,Lemma_List).

 

% end of recursion - final state

traverse(Mwu_name,Final_State,[],[final(Final_State)]) :-

         final(Mwu_name,Final_State).

 

 

/* bear_the_brunt : the transitions */

 

 transition(bear_the_brunt,q0,q0,any_Word).     

 transition(bear_the_brunt,q0,q1,bear).

 transition(bear_the_brunt,q0,q1,take).

 transition(bear_the_brunt,q0,q1,carry).

 transition(bear_the_brunt,q0,q1,catch).

 transition(bear_the_brunt,q0,q1,feel).

 transition(bear_the_brunt,q0,q1,face).

 transition(bear_the_brunt,q1,q2,the).

 transition(bear_the_brunt,q1, q2, a).

 transition(bear_the_brunt,q2,q2,bear_adj).

 transition(bear_the_brunt,q2,q4,brunt).

 transition(bear_the_brunt,q1,q4, brunt).

 transition(bear_the_brunt,q4,q5,of).

 transition(bear_the_brunt,q5,q6,any_Word).

 transition(bear_the_brunt,q6,q6,any_Word).

 

/* play_havoc_with : the transitions */

 

 transition(play_havoc_with,q0,q0,any_Word).

     

 transition(play_havoc_with,q0,q1,play).

 transition(play_havoc_with,q0,q1a,make).

 

 transition(play_havoc_with,q0,q1b,havoc_verb).

 

 transition(play_havoc_with,q1b,q1c,havoc_pre_adj).

 transition(play_havoc_with,q1b,q1c,silent_move).

 transition(play_havoc_with,q1c,q1c,havoc_adj).

 

 transition(play_havoc_with,q1,q2,havoc).

 transition(play_havoc_with,q1a,q2a,havoc).

 transition(play_havoc_with,q1c,q2b,havoc).

 

 transition(play_havoc_with,q2,q3,with).

 transition(play_havoc_with,q2a,q3,of).

 transition(play_havoc_with,q2b,q3,havoc_prep).

 

 transition(play_havoc_with,q3,q4,any_Word).

 transition(play_havoc_with,q4,q4,any_Word).

 

 

/* linking words and grammatical categories */

 

 link(Word,Lemma) :- morph(Word,Lemma).

  

 link(Word,Category) :- listof(Category,Category_List),

                        inlist(Lemma,Category_List),

                        morph(Word,Lemma).

 

 link(_,any_Word). % anything matches any_Word

 

 

/* morphological variants */

 

 morph(causing,cause).

 morph(causes, cause).

 morph(caused, cause).

 

 morph(creating,create).

 morph(creates, create).

 morph(created, create).

 

 morph(doing,do).

 morph(does,do).

 morph(did,do).

 morph(done,do).

 

 morph(making,make).

 morph(makes, make).

 morph(made, make).

 

 morph(wreaking,wreak).

 morph(wreaks, wreak).

 morph(wreaked, wreak).

 morph(wrought,wreak).

 

 morph(playing,play).

 morph(plays, play).

 morph(played, play).

 

 morph(bearing,bear).

 morph(bears, bear).

 morph(bore, bear).

 morph(borne,bear).

 

 morph(taking,take).

 morph(takes, take).

 morph(took, take).

 morph(taken,take).

 

 morph(carrying,carry).

 morph(carries, carry).

 morph(carried, carry).

 

 morph(catching,catch).

 morph(catches, catch).

 morph(caught,catch).

 

 morph(feeling,feel).

 morph(feels, feel).

 morph(felt,feel).

 

 morph(facing,face).

 morph(faces, face).

 morph(faced,face).

 

 

/* no morphological variation */

 

 morph(Lemma,Lemma).

 

 

 /* initial and final states */

 

 initial(bear_the_brunt,q0).

 

 initial(play_havoc_with,q0).

 

 final(bear_the_brunt,q4).

 final(bear_the_brunt,q6).

 

 final(play_havoc_with,q2).

 final(play_havoc_with,q2a).

 final(play_havoc_with,q2b).

 final(play_havoc_with,q4).

 

/* the category lists */

 

listof(havoc_verb,[cause,create,do,wreak]).

listof(havoc_pre_adj,[more,much,some,such]).

listof(havoc_adj,[horrible,financial,seasonal,untold]).

listof(bear_adj,[main,full,heaviest,real,considerable,financial]).

listof(havoc_prep,[against,among,around,at,for,in,on,to,with]).

 

/* membership of a list - identical to pre-defined member predicate */

 

inlist(First,[First|_]).

inlist(Element,[_|Tail_List]):- inlist(Element,Tail_List).

 

/* printing a list */

 

writelist([H|T],Handle) :- write(Handle,H),write(H),nl,nl(Handle),

                           writelist(T,Handle).

writelist([],Handle).

 

 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

%% GETSENTENCE

 

%% reads in a S from the user's terminal or a file

%% standard in the literature

 

getsentence(Wordlist,Handle) :- get0(Handle,Char),

                                     getrest(Handle,Char,Wordlist).

 

getrest(Handle,46,[]):-!.        %% period

getrest(Handle,63,[]):-!.       %% ?

 

getrest(Handle,32,Wordlist) :- !,getsentence(Wordlist,Handle).

    

getrest(Handle,45,Wordlist) :- !,getsentence(Wordlist,Handle).

 

getrest(Handle,Letter,[Word|Wordlist]):-

             getletters(Handle,Letter,Letters,Nextchar),

             name(Word,Letters),

             getrest(Handle,Nextchar,Wordlist).

 

getletters(H,46,[],46):-!.   %% period

 

getletters(H,32,[],32):-!.   %% space

 

getletters(H,45,[],45):-!.   %% hyphen used as word delimiter

 

getletters(H,63,[],63):-!.  %% ?

 

getletters(Handle,Let,[Let1|Letters],Nextchar):-

                 transform(Let,Let1),!,

                 get0(Handle,Char),

                 getletters(Handle,Char,Letters,Nextchar).

 

getletters(Handle,Let,Letters,Nextchar):-

                 get0(Handle,Char),

                 getletters(Handle,Char,Letters,Nextchar).

 

 

%%  transform(C,C1):-C>64,C<91,!,C1 is C+32.  no capital letters

 

transform(C,C) :- C>31. %% chucks non-printing chars out

 

% ----------------------------------------------------------------