next up previous contents
Next: Exemple d'analyse Up: Grammaire catégorielle (version de Previous: Algorithme d'analyse

Programme

/*****************************************************************************/
/*                                                                           */
/* Christophe DELORD                                                         */
/*                                                                           */
/* Stage 1997/98 : ENSEEIHT - Traitement du langage naturel                  */
/*                                                                           */
/*****************************************************************************/

/*****************************************************************************/
/*                                                                           */
/* Analyse de phrases : utilisation de la grammaire catégorielle             */
/*                                                                           */
/*****************************************************************************/

/*****************************************************************************/
/*                                                                           */
/* Définition des catégories                                                 */
/*                                                                           */
/*****************************************************************************/

:-op(400,yfx,'/').
:-op(400,yfx,'\').

categorie(phrase,s).
categorie(verbe_intransitif,iv).
categorie(nom_commun,cn).
categorie(article,(s/iv)/cn).
categorie(nom_propre,(s/iv)).
categorie(pronom_personnel,(s/iv)).
categorie(verbe_transitif,iv/(s/iv)).
categorie(adjectif,cn/cn).
categorie(adjectif,cn\cn).
categorie(verbe_d_etat,iv/(cn/cn)).
categorie(verbe_d_etat,iv/(cn\cn)).
categorie(pronom_relatif_qui,(((s/iv)\(s/iv))/iv)).
categorie(de_complement_de_nom,((cn\cn)/(s/iv))).
categorie(de_complement_de_nom,((cn\cn)/cn)).
categorie(adverbe,(X/X)):-accepte_adverbe(X).
categorie(adverbe,(X\X)):-accepte_adverbe(X).
categorie(preposition_circonstancielle,(s/s)/(s/iv)).
categorie(preposition_circonstancielle,(s\s)/(s/iv)).
categorie(conjonction_de_coordination,((X/X)\X)).

accepte_adverbe(s).
accepte_adverbe(iv).
accepte_adverbe((iv/(s/iv))).
accepte_adverbe((X/X)).
accepte_adverbe((X\X)).

/*****************************************************************************/
/*                                                                           */
/*  Associations mots/catégories                                             */
/*                                                                           */
/*****************************************************************************/

mot(le,article).
mot(chat,nom_commun).
mot(dort,verbe_intransitif).
mot(souris,nom_commun).
mot(regarde,verbe_transitif).
mot(mange,verbe_transitif).
mot(la,article).
mot(une,article).
mot(qui,pronom_relatif_qui).
mot(du,article).
mot(fromage,nom_commun).
mot(christophe,nom_propre).
mot(vite,adverbe).
mot(tres,adverbe).
mot(blanche,adjectif).
mot(grise,adjectif).
mot(gris,adjectif).
mot(il,pronom_personnel).
mot(court,verbe_intransitif).
mot(dans,preposition_circonstancielle).
mot(jardin,nom_commun).
mot(petit,adjectif).
mot(petite,adjectif).
mot(et,conjonction_de_coordination).
mot(parle,verbe_intransitif).
mot(bien,adverbe).
mot(quant,preposition_circonstancielle).
mot(soleil,nom_commun).
mot(brille,verbe_intransitif).
mot(fait,verbe_transitif).
mot(fait,verbe_d_etat).
mot(est,verbe_d_etat).
mot(beau,adjectif).
mot(mon,article).
mot(oncle,nom_commun).
mot(de,de_complement_de_nom).
mot(ma,article).
mot(tante,nom_commun).

/*****************************************************************************/
/*                                                                           */
/*  Affichage d'une liste de catégories                                      */
/*                                                                           */
/*****************************************************************************/

afficher_liste_categories([]).
afficher_liste_categories([C|LC]):-
    ( C='.' -> writef(". ") ; writef("(%t) ",[C]) ),
    afficher_liste_categories(LC).

/*****************************************************************************/
/*                                                                           */
/*  Construction de la liste de catégories                                   */
/*                                                                           */
/*****************************************************************************/

liste_categories([],[]).
liste_categories([M|PH],[CM|CPH]):-
    mot(M,T),
    categorie(T,CM),
    liste_categories(PH,CPH).

/*****************************************************************************/
/*                                                                           */
/*  Mémorisation des étapes de calcul                                        */
/*                                                                           */
/*****************************************************************************/

% liste_calculs([...]). % couples : PHRASE:CATEG
:-dynamic liste_calculs/1.

raz_calculs:-retractall(liste_calculs(_)),asserta(liste_calculs([])).

memoriser(PH,LC):-                              % 1° passage :
    retract(liste_calculs(OLD)),
    asserta(liste_calculs([PH:LC|OLD])).        % on mémorise l'étape

memoriser(_,_):-                                % 2° passage : (après un échec)
    retract(liste_calculs([_|OLD])),
    asserta(liste_calculs(OLD)),                % on restaure la liste
    fail.                                       % et on ne recommence pas

afficher_calculs:-
    liste_calculs(L),
    afficher_calculs(L).
afficher_calculs([]).
afficher_calculs([PH:LC|SUITE]):-
    afficher_calculs(SUITE),
    writef("%t : ",[PH]),
    afficher_liste_categories(LC),
    nl.

/*****************************************************************************/
/*                                                                           */
/*  Grammaire catégorielle (version de base)                                 */
/*                                                                           */
/*****************************************************************************/

/*****************************************************************************/
/*                                                                           */
/* Réduction d'une liste de catégories : (x/y).y=x et y.(x\y)=x              */
/*                                                                           */
/*****************************************************************************/

reduire([A,B|LM],[(X/Y),Y|LC],NPH,NLC):-
    memoriser([A,'.',B],[(X/Y),'.',Y]),
    reduire([[A,B]|LM],[X|LC],NPH,NLC).

reduire([A,B|LM],[Y,(X\Y)|LC],NPH,NLC):-
    memoriser([A,'.',B],[Y,'.',(X\Y)]),
    reduire([[A,B]|LM],[X|LC],NPH,NLC).

reduire([A|LM],[X|LC],NPH,NLC):-
    reduire(LM,LC,LM1,LC1),
    LC\=LC1,                % sinon, ça boucle sur des morceaux irréductibles
    reduire([A|LM1],[X|LC1],NPH,NLC).

reduire(X,C,X,C).

/*****************************************************************************/
/*                                                                           */
/*  Analyse d'une phrase                                                     */
/*                                                                           */
/*****************************************************************************/

analyser(PH):-
    raz_calculs,
    liste_categories(PH,L),
    reduire(PH,L,NPH,[s]),
    afficher_calculs,
    writef("%t : (s)\n\n",[NPH]).

test:-
    analyser([la,souris,mange,tres,vite,du,fromage]).



Christophe Delord
1998-09-02