%% Morphologie-Analyse-Programm für deutsche Adjektive
%% (c) Martin Volk

%% für Listenprädikate (wie z.B. append/3 und member/2)
:- use_module(library(lists)).


%% eine Adjektiv-Wortform analysieren
analyse_adj(Wordform, Stem, Ending, Feat, Komp) :-
  %% Aufteilung in Stamm und Endung
  append(Stem, Ending, Wordform),
  %% Endung prüfen
  member(Ending, ["", "en", "e", "er", "es", "em", "n", "r", "s", "m"]), 
  %% Stamm im Lexikon nachschlagen
  lex_adj(Stem, _, _, _, Stem_e, Komp),
  %% Endung im Lexikon nachschlagen
  check_suffix(Ending, Stem_e, Feat).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Stammlexikon
%%%% Format: Lemma, Komparationsklasse, Umlautung, Ellision, Stammende
lex_adj("gut",       irreg,  no_uml,  no_ell,  no_stem_e,  pos).
lex_adj("besser",    irreg,  no_uml,  no_ell,  no_stem_e,  komp).
lex_adj("best",      irreg,  no_uml,  no_ell,  no_stem_e,  sup).

lex_adj("schön",     st,     no_uml,  no_ell,  no_stem_e,  pos).
lex_adj("wichtig",   st,     no_uml,  no_ell,  no_stem_e,  pos).
lex_adj("böse",      st,     no_uml,  no_ell,  stem_e,     pos).
lex_adj("erfolglos", est,    no_uml,  no_ell,  no_stem_e,  pos).
lex_adj("ledig",     no,     no_uml,  no_ell,  no_stem_e,  pos).

%% Regel für Komparativ-Formen
%%%% Stamm nicht auf -e
lex_adj(Form, st, Uml, Ell, no_stem_e, komp) :-
  append(Stem, "er", Form),
  lex_adj(Stem, Komp, Uml, Ell, no_stem_e, pos),
  member(Komp, [st, est]).

%%%% Stamm auf -e (mit Komparativ-Endung -r Wechsel zu no_stem_e)
lex_adj(Form, st, Uml, Ell, no_stem_e, komp) :-
  append(Stem, "r", Form),
  lex_adj(Stem, st, Uml, Ell, stem_e, pos).

%% Regel für Superlativ-Formen
lex_adj(Form, st, Uml, Ell, no_stem_e, sup) :-
  append(Stem, "st", Form),
  lex_adj(Stem, st, Uml, Ell, _, pos).

lex_adj(Form, st, Uml, Ell, no_stem_e, sup) :-
  append(Stem, "est", Form),
  lex_adj(Stem, est, Uml, Ell, _, pos).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Endungsmuster

%% Leere Endung
check_suffix([], _, 'UNDEKL').

%% Endung -en
check_suffix("en", no_stem_e, ('Gen', 'Sg', _, 'Schw')).
check_suffix("en", no_stem_e, ('Gen', 'Sg', _, 'Gem')).
check_suffix("en", no_stem_e, ('Dat', 'Sg', _, 'Schw')).
check_suffix("en", no_stem_e, ('Dat', 'Sg', _, 'Gem')).
check_suffix("en", no_stem_e, (_    , 'Pl', _, 'Schw')).
check_suffix("en", no_stem_e, (_    , 'Pl', _, 'Gem')).
check_suffix("en", no_stem_e, ('Akk', 'Sg', 'Mask', _)).
check_suffix("en", no_stem_e, ('Gen', 'Sg', 'Mask', 'Stark')).
check_suffix("en", no_stem_e, ('Gen', 'Sg', 'Ntr', 'Stark')).
check_suffix("en", no_stem_e, ('Dat', 'Pl', _,     'Stark')).

%% Endung -e
check_suffix("e",  no_stem_e, ('Nom', 'Sg', 'Fem', _)).
check_suffix("e",  no_stem_e, ('Akk', 'Sg', 'Fem', _)).
check_suffix("e",  no_stem_e, ('Nom', 'Sg', 'Ntr', 'Schw')).
check_suffix("e",  no_stem_e, ('Akk', 'Sg', 'Ntr', 'Schw')).
check_suffix("e",  no_stem_e, ('Nom', 'Sg', 'Mask', 'Schw')).
check_suffix("e",  no_stem_e, ('Nom', 'Pl', _, 'Stark')).
check_suffix("e",  no_stem_e, ('Akk', 'Pl', _, 'Stark')).

%% Endung -er
check_suffix("er",  no_stem_e, ('Nom', 'Sg', 'Mask', 'Gem')).
check_suffix("er",  no_stem_e, ('Nom', 'Sg', 'Mask', 'Stark')).
check_suffix("er",  no_stem_e, ('Gen', 'Sg', 'Fem', 'Stark')).
check_suffix("er",  no_stem_e, ('Dat', 'Sg', 'Fem', 'Stark')).
check_suffix("er",  no_stem_e, ('Gen', 'Pl', _,     'Stark')).

%% Endung -es
check_suffix("es",  no_stem_e, ('Nom', 'Sg', 'Ntr', 'Gem')).
check_suffix("es",  no_stem_e, ('Akk', 'Sg', 'Ntr', 'Gem')).
check_suffix("es",  no_stem_e, ('Nom', 'Sg', 'Ntr', 'Stark')).
check_suffix("es",  no_stem_e, ('Akk', 'Sg', 'Ntr', 'Stark')).

%% Endung -em
check_suffix("em",  no_stem_e, ('Dat', 'Sg', 'Mask', 'Stark')).
check_suffix("em",  no_stem_e, ('Dat', 'Sg', 'Ntr' , 'Stark')).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Spezialregeln für Verben, deren Stamm auf -e endet (wie z.B. 'böse')
%% Endung -n entspricht der 'normalen' Endung -en
check_suffix("n", stem_e, Feat) :-
  check_suffix("en", no_stem_e, Feat).

%% Leere Endung entspricht der 'normalen' Endung -e
check_suffix("", stem_e, Feat) :-
  check_suffix("e", no_stem_e, Feat).

%% Endung -r entspricht der 'normalen' Endung -er
check_suffix("r", stem_e, Feat) :-
  check_suffix("er", no_stem_e, Feat).

%% Endung -s entspricht der 'normalen' Endung -es
check_suffix("s", stem_e, Feat) :-
  check_suffix("es", no_stem_e, Feat).

%% Endung -m entspricht der 'normalen' Endung -em
check_suffix("m", stem_e, Feat) :-
  check_suffix("em", no_stem_e, Feat).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Testprädikate

%%%% Wordform als String eingeben!!
%%%% Analyse-Richtung
a_test(Wordform) :-
  analyse_adj(Wordform, Stem, Ending, Feat, Komp),
  format("~s :  ~s ~s : ~w  ~a ~n", [Wordform, Stem, Ending, Feat, Komp]),
  fail.

a_test(_).

%%%% Generierungs-Richtung ==> geht nur für einfache Fälle
g_test(Stem, Feat, Komp) :-
  analyse_adj(Wordform, Stem, Ending, Feat, Komp),
  format("~s ~s : ~w  ~a ==> ~s ~n", [Stem, Ending, Feat, Komp, Wordform]),
  fail.

g_test(_, _, _).


/* *************** Beispiele **********************
   
| ?- a_test("schöne").
schöne :  schön e : Nom,Sg,Fem,_307  pos 
schöne :  schön e : Akk,Sg,Fem,_307  pos 
schöne :  schön e : Nom,Sg,Ntr,Schw  pos 
schöne :  schön e : Akk,Sg,Ntr,Schw  pos 
schöne :  schön e : Nom,Sg,Mask,Schw  pos 
schöne :  schön e : Nom,Pl,_306,Stark  pos 
schöne :  schön e : Akk,Pl,_306,Stark  pos 


| ?- a_test("wichtigerem").
wichtigerem :  wichtiger em : Dat,Sg,Mask,Stark  komp 
wichtigerem :  wichtiger em : Dat,Sg,Ntr,Stark  komp 


| ?- a_test("böser").
böser :  böse r : Nom,Sg,Mask,Gem  pos 
böser :  böse r : Nom,Sg,Mask,Stark  pos 
böser :  böse r : Gen,Sg,Fem,Stark  pos 
böser :  böse r : Dat,Sg,Fem,Stark  pos 
böser :  böse r : Gen,Pl,_316,Stark  pos 
böser :  böser  : UNDEKL  komp 


| ?- g_test("schön", ('Akk', _, _, _), pos).
schön en : Akk,Pl,_96,Schw  pos ==> schönen 
schön en : Akk,Pl,_96,Gem  pos ==> schönen 
schön en : Akk,Sg,Mask,_106  pos ==> schönen 
schön e : Akk,Sg,Fem,_106  pos ==> schöne 
schön e : Akk,Sg,Ntr,Schw  pos ==> schöne 
schön e : Akk,Pl,_96,Stark  pos ==> schöne 
schön es : Akk,Sg,Ntr,Gem  pos ==> schönes 
schön es : Akk,Sg,Ntr,Stark  pos ==> schönes 

************************************************** */