proba_mot_classe.c 24.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
/*
    --------------------------------------------------------
    LIA_PHON : Un systeme complet de phonetisation de textes
    --------------------------------------------------------

    Copyright (C) 2001 FREDERIC BECHET

    ..................................................................

    This file is part of LIA_PHON

    LIA_PHON is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    ..................................................................

    For any publication related to scientific work using LIA_PHON,
    the following reference paper must be mentioned in the bibliography: 
        
    Bechet F., 2001, "LIA_PHON - Un systeme complet de phonetisation
    de textes", revue Traitement Automatique des Langues (T.A.L.)
    volume 42, numero 1/2001, edition Hermes
    ..................................................................
                              
    Contact :
              FREDERIC BECHET - LIA - UNIVERSITE D'AVIGNON
              AGROPARC BP1228 84911  AVIGNON  CEDEX 09  FRANCE
              frederic.bechet@lia.univ-avignon.fr
    ..................................................................
*/
/*  Gestion des probas des mots sachant les classes :
      * Pour la compilation :
       - en entree : 2 fichiers lexiques (format sirocco)
			- un pour les mots
			- un pour les categories
                     1 fichier texte au format suivant :
			mot\tcate\tnb\n
       - en sortie : 3 fichiers .pmc_des .pmc_tab .pmc_zon
	 avec : .pmc_des=descripteur de taille du modele
		.pmc_tab=table des indices des mots du dico trie par alpha
		.pmc_zon=stockage des probas des mots sachant la classe
      * Pour l'utilisation :
       - en entree : 3 fichiers .pmc_des .pmc_tab .pmc_zon
       - une fonction p_mot_classe donnant la proba du mot sachant la classe  */
/*  FRED 0399  */  /*  Modif ajout des lemmes - FRED 0699  */

#include <stdio.h>
#include <stdlib.h>
#include <libgram.h>

/*................................................................*/

#define TailleLigne	400

#define SiMajuscule(n)	((((n)>='A')&&((n)<='Z'))?1:0)

/*................................................................*/

/* Structure de codage du modele */

/* Arbre des categories avec leur compte :
   * en general pour avoir le nombre total
   * en particulier pour chaque mot */

typedef signed long	compte_t;

typedef struct type_tree_cate_pmc
	{
	wrd_index_t c_cate,c_lemm;
	compte_t nb;
	struct type_tree_cate_pmc *fg,*fd;
	} *ty_tree_cate_pmc;

/* Arbre des mots avec le nb de categorie et l'arbre
   des categories attachees au mot */

typedef struct type_tree_mot_pmc
	{
	signed char dq;
	wrd_index_t c_mot;
	compte_t nb_cate;
	ty_tree_cate_pmc t_cate;
	struct type_tree_mot_pmc *fg,*fd;
	} *ty_tree_mot_pmc;

/* Zone de stockage des categorie avec leur proba en log */

/********** Definie dans le .h

typedef struct type_zon_pmc
	{
	wrd_index_t c_cate;
	float pmc;
	} type_zon_pmc;

typedef struct type_zon_lemm_pmc
	{
	wrd_index_t c_cate,c_lemm;
	float pmc;
	} type_zon_lemm_pmc;

 Tableau des mots avec pointeur sur leur liste de categorie+proba 

typedef struct type_tab_pmc
	{
	wrd_index_t c_mot;
	unsigned int nb_cate;
	long i_cate;
	} type_tab_pmc;

 Type general pmc permettant de stocker plusieurs modeles 

typedef struct type_pmc
	{
	wrd_index_t nb_tab,nb_zon;
	int si_log_e,si_lemme,max_nb_cate;
	type_zon_lemm_pmc *p_zon_lemm;
	type_zon_pmc *p_zon;
	type_tab_pmc *p_tab;
	} *ty_pmc;

**********/

/*................................................................*/

/*  Constructeur, Destructeur, et autres babioles des types ty_tree....  */

/* Tout d'abord quelques rotations .... */

void tree_mot_echange_info(ty_tree_mot_pmc n1,ty_tree_mot_pmc n2)
{
char dq;
wrd_index_t c_mot;
compte_t nb_cate;
ty_tree_cate_pmc t_cate;

dq=n1->dq;
c_mot=n1->c_mot;
nb_cate=n1->nb_cate;
t_cate=n1->t_cate;

n1->dq=n2->dq;
n1->c_mot=n2->c_mot;
n1->nb_cate=n2->nb_cate;
n1->t_cate=n2->t_cate;

n2->dq=dq;
n2->c_mot=c_mot;
n2->nb_cate=nb_cate;
n2->t_cate=t_cate;
}

ty_tree_mot_pmc tree_mot_rotation_droite(ty_tree_mot_pmc pt)
{
ty_tree_mot_pmc tmpfgfd,tmpfd;

if (pt->fg==NULL)
 {
 fprintf(stderr,"ERROR : le fg ne peut etre NULL !!!!\n");
 exit(0);
 }

/*  On echange *this et fg  */
tree_mot_echange_info(pt,pt->fg);

tmpfgfd=pt->fg->fd;
tmpfd=pt->fd;

pt->fd=pt->fg;
pt->fg=pt->fg->fg;
pt->fd->fg=tmpfgfd;
pt->fd->fd=tmpfd;

return pt;
}

ty_tree_mot_pmc tree_mot_rotation_gauche(ty_tree_mot_pmc pt)
{
ty_tree_mot_pmc tmpfdfg,tmpfg;

if (pt->fd==NULL)
 {
 fprintf(stderr,"ERROR : le fd ne peut etre NULL !!!!\n");
 exit(0);
 }

/*  On echange *this et fd  */
tree_mot_echange_info(pt,pt->fd);

tmpfdfg=pt->fd->fg;
tmpfg=pt->fg;

pt->fg=pt->fd;
pt->fd=pt->fd->fd;
pt->fg->fd=tmpfdfg;
pt->fg->fg=tmpfg;

return pt;
}

ty_tree_mot_pmc tree_mot_rotation_gauche_droite(ty_tree_mot_pmc pt)
{
pt->fg=tree_mot_rotation_gauche(pt->fg);
pt=tree_mot_rotation_droite(pt);
return pt;
}

ty_tree_mot_pmc tree_mot_rotation_droite_gauche(ty_tree_mot_pmc pt)
{
pt->fd=tree_mot_rotation_droite(pt->fd);
pt=tree_mot_rotation_gauche(pt);
return pt;
}

/* et maintenant les allocations memoires */

ty_tree_cate_pmc new_tree_cate_pmc(wrd_index_t c_cate,wrd_index_t c_lemm,compte_t nb)
{
ty_tree_cate_pmc pt;
pt=(ty_tree_cate_pmc)malloc(sizeof(struct type_tree_cate_pmc));
pt->c_cate=c_cate;
pt->c_lemm=c_lemm;
pt->nb=nb;
pt->fg=pt->fd=NULL;
return pt;
}

ty_tree_cate_pmc ajoute_tree_cate_pmc(wrd_index_t c_cate,wrd_index_t c_lemm,
	compte_t nb,ty_tree_cate_pmc racine,int *si_nouveau)
{
if (racine==NULL) { *si_nouveau=1; return new_tree_cate_pmc(c_cate,c_lemm,nb); }
if (racine->c_cate==c_cate) { *si_nouveau=0; racine->nb+=nb; }
else
 if (racine->c_cate>c_cate) racine->fg=ajoute_tree_cate_pmc(c_cate,c_lemm,nb,racine->fg,si_nouveau);
 else                       racine->fd=ajoute_tree_cate_pmc(c_cate,c_lemm,nb,racine->fd,si_nouveau);
return racine;
}

void delete_tree_cate_pmc(ty_tree_cate_pmc racine)
{
if (racine)
 {
 delete_tree_cate_pmc(racine->fg);
 delete_tree_cate_pmc(racine->fd);
 free(racine);
 }
}

int recherche_tree_cate_pmc(wrd_index_t c_cate,compte_t *nb,ty_tree_cate_pmc racine)
{
if (racine==NULL) return 0;
if (racine->c_cate==c_cate) { *nb=racine->nb; return 1; }
if (racine->c_cate>c_cate) return recherche_tree_cate_pmc(c_cate,nb,racine->fg);
else                       return recherche_tree_cate_pmc(c_cate,nb,racine->fd);
}

ty_tree_mot_pmc new_tree_mot_pmc(wrd_index_t c_mot,wrd_index_t c_cate,wrd_index_t c_lemm,compte_t nb)
{
int si_nouveau;
ty_tree_mot_pmc pt;
pt=(ty_tree_mot_pmc)malloc(sizeof(struct type_tree_mot_pmc));
pt->dq=0;
pt->c_mot=c_mot;
pt->nb_cate=1;
pt->t_cate=ajoute_tree_cate_pmc(c_cate,c_lemm,nb,NULL,&si_nouveau);
pt->fg=pt->fd=NULL;
return pt;
}

ty_tree_mot_pmc ajoute_tree_mot_pmc(wrd_index_t c_mot,wrd_index_t c_cate,wrd_index_t c_lemm,
	compte_t nb, ty_tree_mot_pmc racine, char *si_augm)
{
int si_nouveau;
static char r;
/*
if (c_mot==25) printf("TEST: c_mot=%d racine==%s \n",c_mot,racine==NULL?"NULL":"PASNULL");
*/
if (racine==NULL) { *si_augm=1; return new_tree_mot_pmc(c_mot,c_cate,c_lemm,nb); }

if (racine->c_mot==c_mot)
 {
 /*if (c_mot==25) printf("TEST: c_mot=%d racine->c_mot=%d : nb_cate=%d \n",c_mot,racine->c_mot,racine->nb_cate);*/

 *si_augm=0;
 racine->t_cate=ajoute_tree_cate_pmc(c_cate,c_lemm,nb,racine->t_cate,&si_nouveau);
 if (si_nouveau) racine->nb_cate++;

 /*if (c_mot==25) printf("TEST:                apres ajout: nb_cate=%d \n",racine->nb_cate);*/
 }
else
 if (racine->c_mot>c_mot)
  { /* sur le fils gauche */
  racine->fg=ajoute_tree_mot_pmc(c_mot,c_cate,c_lemm,nb,racine->fg,si_augm);
  if ((*si_augm==0)||(racine->dq<=-1)) r=0; else r=1;
  racine->dq+=(*si_augm);
  *si_augm=r;

  /* eventuelle rotation */
  if (racine->dq==2)
   {
   if (racine->fg->dq==1)
    {
    racine=tree_mot_rotation_droite(racine);
    racine->dq=racine->fd->dq=0;
    }
   else
    {
    racine=tree_mot_rotation_gauche_droite(racine);
    switch (racine->dq)
     {
     case  1 : racine->fg->dq=0; racine->fd->dq=-1; break;
     case -1 : racine->fg->dq=1; racine->fd->dq= 0; break;
     case  0 : racine->fg->dq=racine->fd->dq=0; break;
     }
    racine->dq=0;
    }
   *si_augm=0;
   }
  }
 else
  { /* sur le fils droit */
  racine->fd=ajoute_tree_mot_pmc(c_mot,c_cate,c_lemm,nb,racine->fd,si_augm);
  if ((*si_augm==0)||(racine->dq>=1)) r=0; else r=1;
  racine->dq-=(*si_augm);
  *si_augm=r;

  /* eventuelle rotation */
  if (racine->dq==-2)
   {
   if (racine->fd->dq==-1)
    {
    racine=tree_mot_rotation_gauche(racine);
    racine->dq=racine->fg->dq=0;
    }
   else
    {
    racine=tree_mot_rotation_droite_gauche(racine);
    switch (racine->dq)
     {
     case  1 : racine->fd->dq=-1; racine->fg->dq= 0; break;
     case -1 : racine->fd->dq= 0; racine->fg->dq= 1; break;
     case  0 : racine->fg->dq=racine->fd->dq=0; break;
     }
    racine->dq=0;
    }
   *si_augm=0;
   }
  }
return racine;
}

void delete_tree_mot_pmc(ty_tree_mot_pmc racine)
{
if (racine)
 {
 delete_tree_mot_pmc(racine->fg);
 delete_tree_mot_pmc(racine->fd);
 delete_tree_cate_pmc(racine->t_cate);
 free(racine);
 }
}

int recherche_tree_mot_pmc(wrd_index_t c_mot,compte_t *nb_cate,ty_tree_cate_pmc *t_cate,ty_tree_mot_pmc racine)
{
if (racine==NULL) return 0;
if (racine->c_mot==c_mot) { *nb_cate=racine->nb_cate; *t_cate=racine->t_cate; return 1; }
if (racine->c_mot>c_mot) return recherche_tree_mot_pmc(c_mot,nb_cate,t_cate,racine->fg);
else                     return recherche_tree_mot_pmc(c_mot,nb_cate,t_cate,racine->fd);
}

/*................................................................*/

/* Constructeur d'un modele pmc */

ty_pmc cons_pmc(wrd_index_t nb_tab,wrd_index_t nb_zon,int si_log_e,int si_lemme,int max_nb_cate)
{
ty_pmc pt;

pt=(ty_pmc)malloc(sizeof(struct type_pmc));
if (pt==NULL) return NULL;

pt->nb_tab=nb_tab;
pt->nb_zon=nb_zon;
pt->max_nb_cate=max_nb_cate;
pt->si_log_e=si_log_e;
pt->si_lemme=si_lemme;

pt->p_tab=(type_tab_pmc *)malloc(nb_tab*sizeof(type_tab_pmc));
if (si_lemme)
 {
 pt->p_zon_lemm=(type_zon_lemm_pmc *)malloc(nb_zon*sizeof(type_zon_lemm_pmc));
 pt->p_zon=NULL;
 }
else
 {
 pt->p_zon=(type_zon_pmc *)malloc(nb_zon*sizeof(type_zon_pmc));
 pt->p_zon_lemm=NULL;
 }
if ((pt->p_tab==NULL)||((si_lemme)&&(pt->p_zon_lemm==NULL))||((!si_lemme)&&(pt->p_zon==NULL)))
 return NULL;

return pt;
}

void delete_pmc(pt)
 ty_pmc pt;
{
if (pt)
 {
 if (pt->p_tab) free(pt->p_tab);
 if (pt->p_zon) free(pt->p_zon);
 if (pt->p_zon_lemm) free(pt->p_zon_lemm);
 free(pt);
 }
}

/*................................................................*/

/* Acces dichotomique sur le tableau p_tab */

int acces_dicho(wrd_index_t code ,wrd_index_t d ,wrd_index_t f ,wrd_index_t *resu , type_tab_pmc *p_tab)
{
static wrd_index_t m;
while(d<=f)
 {
 m=(d+f)/2;
 if (code<p_tab[m].c_mot) f=m-1;
 else
  if (code>p_tab[m].c_mot) d=m+1;
  else { *resu=m; return 1; }
 }
return 0;
}

void range_zone_pmc(wrd_index_t c_mot,ty_tree_cate_pmc t_cate,ty_tree_cate_pmc p_cate,
	type_zon_pmc *p_zon,type_zon_lemm_pmc *p_zon_lemm,
	long *indice_zone,compte_t nb_cate,int si_log_e,int si_lemme)
{
static compte_t nb_total;

/*if (c_mot==25) printf("TEST: c_mot=%d nb_cate=%d t_cate==%s\n",c_mot,nb_cate,t_cate==NULL?"NULL":"PASNULL");*/

if (t_cate)
 {
 if (nb_cate<=0) /* XXXXXX : <= ou < ?????? */
  {
  fprintf(stderr,"STRANGE : pourquoi l'arbre t_cate a plus de noeuds que nb_cate ???? c_mot=%d nb_cate=%d\n",
	c_mot,nb_cate);
  exit(0);
  }
 if (t_cate->fg) range_zone_pmc(c_mot,t_cate->fg,p_cate,p_zon,p_zon_lemm,indice_zone,--nb_cate,si_log_e,si_lemme);
 if (t_cate->fd) range_zone_pmc(c_mot,t_cate->fd,p_cate,p_zon,p_zon_lemm,indice_zone,--nb_cate,si_log_e,si_lemme);

 if (si_lemme)
  {
  p_zon_lemm[*indice_zone].c_cate=t_cate->c_cate;
  p_zon_lemm[*indice_zone].c_lemm=t_cate->c_lemm;
  }
 else
  p_zon[*indice_zone].c_cate=t_cate->c_cate;

 if (!recherche_tree_cate_pmc(t_cate->c_cate,&nb_total,p_cate))
  {
  fprintf(stderr,"STRANGE : pourquoi la categorie %ld est-elle inconue de l'arbre ????\n",t_cate->c_cate);
  exit(0);
  }
/* 
 if (c_mot==25)
  {
  printf("\nTEST : c_mot=%ld - cate=%ld - lemme=%ld - nb=%ld - nbtotal=%ld  **  indice_zone=%ld\n",
	c_mot,t_cate->c_cate,t_cate->c_lemm,t_cate->nb,nb_total,*indice_zone);
  }
*/ 
 if (si_log_e)
  if (si_lemme)
   p_zon_lemm[*indice_zone].pmc=(float)log((double)t_cate->nb/(double)nb_total);
  else
   p_zon[*indice_zone].pmc=(float)log((double)t_cate->nb/(double)nb_total);
 else
  if (si_lemme)
   p_zon_lemm[*indice_zone].pmc=(float)log10((double)t_cate->nb/(double)nb_total);
  else
   p_zon[*indice_zone].pmc=(float)log10((double)t_cate->nb/(double)nb_total);
 /*
 if (c_mot==262721) printf("TEST\tproba=%f\n",p_zon_lemm[*indice_zone].pmc);
 */
 (*indice_zone)++;
 }
}

void range_tableau_pmc(ty_tree_mot_pmc p_mot,ty_tree_cate_pmc p_cate,
	ty_pmc pt_pmc,long *indice_zone,int *max_nb_cate)
{
static wrd_index_t indice;
if (p_mot)
 {
 if (p_mot->nb_cate>(*max_nb_cate)) *max_nb_cate=(int)p_mot->nb_cate;

 range_tableau_pmc(p_mot->fg,p_cate,pt_pmc,indice_zone,max_nb_cate);
 range_tableau_pmc(p_mot->fd,p_cate,pt_pmc,indice_zone,max_nb_cate);

 /* maintenant on travaille la */
 /* acces au code mot par dichotomie sur le tableau p_tab pour trouver sa place */
 if (!acces_dicho(p_mot->c_mot,0,pt_pmc->nb_tab,&indice,pt_pmc->p_tab))
  {
  fprintf(stderr,"ERREUR : et pourquoi ce code est inconnu du tabelau p_tab ???? code=%ld\n",p_mot->c_mot);
  exit(0);
  }
 pt_pmc->p_tab[indice].nb_cate=(unsigned int)p_mot->nb_cate;
 pt_pmc->p_tab[indice].i_cate=*indice_zone;

 /*if (p_mot->c_mot==25) printf("TEST : juste avant 'range_zone_pmc', nb cate=%d\n",p_mot->nb_cate);*/
 range_zone_pmc(p_mot->c_mot,p_mot->t_cate,p_cate,pt_pmc->p_zon,pt_pmc->p_zon_lemm,indice_zone,p_mot->nb_cate,
	pt_pmc->si_log_e,pt_pmc->si_lemme);
 if (*indice_zone>pt_pmc->nb_zon)
  {
  fprintf(stderr,"ERROR : indice_zone=%ld depasse la taille de nb_zon=%ld !!!!\n",*indice_zone,pt_pmc->nb_zon);
  exit(0);
  }
 }
}

/*................................................................*/

/* Construit l'arbre du modele a partir du fichier texte et compile
   le modele pmc en le stockant dans les tableaux */

ty_pmc compile_modele_pmc(char *nomfic,ty_lexique pt_lexi_mot,ty_lexique pt_lexi_cate,int si_log_e,int si_lemme)
{
FILE *file;
ty_tree_cate_pmc pt_tree_cate;
ty_tree_mot_pmc pt_tree_mot;
compte_t nb_couple;
char ch[TailleLigne],*mot,*cate,*ch_compte,*lemm,si_augm;
compte_t compte;
int si_nouveau,in_lexi,max_nb_cate;
long indice_zone;
wrd_index_t c_mot,c_cate,c_lemm,i;
ty_pmc pt_pmc;

if (!(file=fopen(nomfic,"rt")))
 {
 fprintf(stderr,"Can't open : %s\n",nomfic);
 exit(0);
 }

/* Construction de l'arbre */

pt_tree_cate=NULL;
pt_tree_mot=NULL;

fprintf(stderr,"Compilation des arbres classe et mot\n");

for(nb_couple=0;fgets(ch,TailleLigne,file);)
 {
 /* format : mot cate compte */
 mot=(char*)strtok(ch," \t\n");
 cate=(char*)strtok(NULL," \t\n");
 ch_compte=(char*)strtok(NULL," \t\n");
 sscanf(ch_compte,"%ld",&compte);
 if (si_lemme) lemm=(char*)strtok(NULL," \t\n");
 /*
 if (!strcmp(mot,"y"))
  printf("TEST : lecture -> %s %s %s %s\n",mot,cate,lemm,ch_compte);
 */
 if (!(Mot2Code(cate,&c_cate,pt_lexi_cate)))
  {
  fprintf(stderr,"ATTENTION : c'est quoi cette categorie [%s] !!!!\n",cate);
  exit(0);
  }

 /* on ne prends que les mots qui font partie du lexique de mots */
 in_lexi=Mot2Code(mot,&c_mot,pt_lexi_mot);
 if ((!in_lexi)&&(SiMajuscule(mot[0]))&&(!SiMajuscule(mot[1])))
  { /* on laisse une chance aux mots qui sont dans le lexique sans la majuscule de debut */
  mot[0]+=('a'-'A');
  in_lexi=Mot2Code(mot,&c_mot,pt_lexi_mot);
  }
 if (in_lexi)
  {
  if (si_lemme)
   {
   /* on cherche le code du lemme , s'il n'y est pas, on prend le mot lui meme */
   if (!Mot2Code(lemm,&c_lemm,pt_lexi_mot)) c_lemm=c_mot;
   }
  else c_lemm=0;

  nb_couple++;
/*  
  if (c_mot==25)
   {
   printf("TEST : %s=%ld %s=%ld %s=%ld nb=%ld\n",mot,c_mot,cate,c_cate,lemm,c_lemm,compte);
   }
*/  
  pt_tree_mot=ajoute_tree_mot_pmc(c_mot,c_cate,c_lemm,compte,pt_tree_mot,&si_augm);

  if ((nb_couple%50000)==0) fprintf(stderr,"\ten cours : %d\n",nb_couple);
  }
 /* on ajoute ds le compte total de chaque categorie meme les comptes des mots inconnus */
 pt_tree_cate=ajoute_tree_cate_pmc(c_cate,0,compte,pt_tree_cate,&si_nouveau);
 }
fclose(file);

fprintf(stderr,"Termine\n -> on a lu %d couples\n",nb_couple);

fprintf(stderr,"Allocation du modele PMC -> ");

/* Allocation du modele pmc */
pt_pmc=cons_pmc(pt_lexi_mot->GereLexique_NbMots,nb_couple,si_log_e,si_lemme,0);
if (pt_pmc==NULL) { fprintf(stderr,"desole, pas assez de memoire\n"); exit(0); }

fprintf(stderr,"Termine\n");

fprintf(stderr,"Taille necessaire au modele : %.2f Ko\n",
	(float)((sizeof(type_tab_pmc)*pt_lexi_mot->GereLexique_NbMots)+
		(sizeof(type_zon_pmc)*nb_couple))/(float)1024);

/* Initialisation :
	on initialise le tableau pt_pmc->p_tab avec les codes des mots
	ranges dans le tableau TablLexiqueGraf du lexique mot. En effet
	ce dernier est tri dans l'ordre des codes, pour la dichotomie,
	et cela permet d'initialiser toutes les cases du tableau p_tab
	avec 0 dans le champs nb_cate pour permettre de traiter sans
	probleme les mots du lexique qui n'ont pas ete vu dans le fichier
	texte d'apprentissage du modele pmc. Il suffira lors de l'utilisation
	du modele de tester cette variable, et s'il est egal a 0 de renvoyer
	une proba bien basse, mes amis */

fprintf(stderr,"Initialisation du modele -> ");

for(i=0;i<pt_pmc->nb_tab;i++)
 {
 pt_pmc->p_tab[i].c_mot=pt_lexi_mot->TablLexiqueGraf[i].code;
 pt_pmc->p_tab[i].i_cate=0;
 pt_pmc->p_tab[i].nb_cate=0;
 }

fprintf(stderr,"Termine\n");

/* Maintenant on parcours l'arbre des mots pour les ranger dans les 2 tableaux */
indice_zone=0;

fprintf(stderr,"Calcul et stockage des probas dans le modele -> ");

max_nb_cate=0;
range_tableau_pmc(pt_tree_mot,pt_tree_cate,pt_pmc,&indice_zone,&max_nb_cate);
pt_pmc->max_nb_cate=max_nb_cate;

fprintf(stderr,"Termine\n");

fprintf(stderr,"On a range %ld couples mot/cate (%ld dans le fichier)\n",indice_zone,pt_pmc->nb_zon);
/* on reactualise 'pt_pmc->nb_zon' pour enlever les doublons */
pt_pmc->nb_zon=indice_zone;

delete_tree_mot_pmc(pt_tree_mot);
delete_tree_cate_pmc(pt_tree_cate);

return pt_pmc;
}

/*................................................................*/

/* Sauvegarde du modele PMC */

void sauve_pmc(char *nomfic,ty_pmc pt_pmc)
{
char ch[TailleLigne];
FILE *file;
wrd_index_t nb;

sprintf(ch,"%s.pmc_des",nomfic);
if (!(file=fopen(ch,"wt")))
 { fprintf(stderr,"Can't open : %s\n",ch); exit(0); }
fprintf(file,"%ld %ld %d\n",pt_pmc->nb_tab,pt_pmc->nb_zon,pt_pmc->max_nb_cate);
if (pt_pmc->si_log_e) fprintf(file,"LOG_E\n");
else                  fprintf(file,"LOG_10\n");
if (pt_pmc->si_lemme) fprintf(file,"LEMME\n");
fclose(file);

sprintf(ch,"%s.pmc_tab",nomfic);
if (!(file=fopen(ch,"wb")))
 { fprintf(stderr,"Can't open : %s\n",ch); exit(0); }
if ((nb=fwrite((void*)pt_pmc->p_tab,sizeof(type_tab_pmc),pt_pmc->nb_tab,file))!=pt_pmc->nb_tab)
 { fprintf(stderr,"Erreur d'ecriture : %s (%ld items au lieu de %ld)\n",ch,nb,pt_pmc->nb_tab); exit(0); }
fclose(file);

sprintf(ch,"%s.pmc_zon",nomfic);
if (!(file=fopen(ch,"wb")))
 { fprintf(stderr,"Can't open : %s\n",ch); exit(0); }
if (pt_pmc->p_zon_lemm!=NULL)
 {
 if ((nb=fwrite((void*)pt_pmc->p_zon_lemm,sizeof(type_zon_lemm_pmc),pt_pmc->nb_zon,file))!=pt_pmc->nb_zon)
  { fprintf(stderr,"Erreur d'ecriture : %s (%ld items au lieu de %ld)\n",ch,nb,pt_pmc->nb_zon); exit(0); }
 }
else
 {
 if ((nb=fwrite((void*)pt_pmc->p_zon,sizeof(type_zon_pmc),pt_pmc->nb_zon,file))!=pt_pmc->nb_zon)
  { fprintf(stderr,"Erreur d'ecriture : %s (%ld items au lieu de %ld)\n",ch,nb,pt_pmc->nb_zon); exit(0); }
 }
fclose(file);
}

/*................................................................*/

/* Chargement du modele PMC */

ty_pmc charge_pmc(char *nomfic )
{
FILE *file;
char ch[TailleLigne];
ty_pmc pt_pmc;
wrd_index_t nb_tab,nb_zon,nb;
int si_log_e,si_lemme,max_nb_cate;

sprintf(ch,"%s.pmc_des",nomfic);
if (!(file=fopen(ch,"rt")))
 { fprintf(stderr,"Can't open : %s\n",ch); exit(0); }
fgets(ch,TailleLigne,file);
sscanf(ch,"%ld %ld %d",&nb_tab,&nb_zon,&max_nb_cate);
fgets(ch,TailleLigne,file);
if (!strncmp(ch,"LOG_10",6)) si_log_e=0; else si_log_e=1;
if ((fgets(ch,TailleLigne,file))&&(!strncmp(ch,"LEMME",5))) si_lemme=1; else si_lemme=0;
fclose(file);

pt_pmc=cons_pmc(nb_tab,nb_zon,si_log_e,si_lemme,max_nb_cate);
if (pt_pmc==NULL) { fprintf(stderr,"desole, pas assez de memoire\n"); exit(0); }

sprintf(ch,"%s.pmc_tab",nomfic);
if (!(file=fopen(ch,"rb")))
 { fprintf(stderr,"Can't open : %s\n",ch); exit(0); }
if ((nb=fread((void*)pt_pmc->p_tab,sizeof(type_tab_pmc),pt_pmc->nb_tab,file))!=pt_pmc->nb_tab)
 { fprintf(stderr,"Erreur de lecture : %s (%ld items au lieu de %ld)\n",ch,nb,pt_pmc->nb_tab); exit(0); }
fclose(file);

sprintf(ch,"%s.pmc_zon",nomfic);
if (!(file=fopen(ch,"rb")))
 { fprintf(stderr,"Can't open : %s\n",ch); exit(0); }
if (si_lemme)
 {
 if ((nb=fread((void*)pt_pmc->p_zon_lemm,sizeof(type_zon_lemm_pmc),pt_pmc->nb_zon,file))!=pt_pmc->nb_zon)
  { fprintf(stderr,"Erreur de lecture : %s (%ld items au lieu de %ld)\n",ch,nb,pt_pmc->nb_zon); exit(0); }
 }
else
 {
 if ((nb=fread((void*)pt_pmc->p_zon,sizeof(type_zon_pmc),pt_pmc->nb_zon,file))!=pt_pmc->nb_zon)
  { fprintf(stderr,"Erreur de lecture : %s (%ld items au lieu de %ld)\n",ch,nb,pt_pmc->nb_zon); exit(0); }
 }
fclose(file);

return pt_pmc;
}

/*................................................................*/

/* fonction qui renvoi la proba du mot sachant la classe ainsi que le lemme
   associer au couple mot/cate */

err_t pmc_proba_mot_classe(logprob_t *proba,wrd_index_t *c_lemm,
	const wrd_index_t mot, const wrd_index_t classe,
	ty_pmc pt_pmc, const err_t trace)
{
static wrd_index_t indice,i;

if (!acces_dicho(mot,0,pt_pmc->nb_tab,&indice,pt_pmc->p_tab))
 {
 if (trace & SIR_GRAM_TRACE)
  fprintf(stderr,"PMC : Je ne connais pas le mot : %ld\n",mot);
 *proba=-99.00;
 return GRAM_CODE_UNKNOWN;
 }

if (pt_pmc->p_tab[indice].nb_cate==0)
 {
 if (trace & SIR_GRAM_TRACE)
  fprintf(stderr,"PMC : Le mot : %ld n'a pas ete vu lors de l'apprentissage\n",mot);
 *proba=-99.00;
 return GRAM_CODE_UNKNOWN;
 }

if (pt_pmc->si_lemme) /* il y a des lemmes */
 {
 for(i=0;(i<pt_pmc->p_tab[indice].nb_cate)&&(pt_pmc->p_zon_lemm[pt_pmc->p_tab[indice].i_cate+i].c_cate!=classe);i++);
 /*
  printf("i=%ld - nb_cate=%d - c_cate=%ld classe=%ld\n",
	i,pt_pmc->p_tab[indice].nb_cate,pt_pmc->p_zon_lemm[pt_pmc->p_tab[indice].i_cate+i].c_cate,classe);
 */
 }
else
 {
 for(i=0;(i<pt_pmc->p_tab[indice].nb_cate)&&(pt_pmc->p_zon[pt_pmc->p_tab[indice].i_cate+i].c_cate!=classe);i++);
 }

if (i==pt_pmc->p_tab[indice].nb_cate)
 {
 if (trace & SIR_GRAM_TRACE)
  fprintf(stderr,"PMC : Le mot : %ld n'a pas ete vu avec la classe %ld lors de l'apprentissage\n",mot,classe);
 *proba=-99.00;
 return GRAM_CODE_UNKNOWN;
 }

if (pt_pmc->si_lemme) /* il y a des lemmes */
 {
 *proba=(logprob_t)(pt_pmc->p_zon_lemm[pt_pmc->p_tab[indice].i_cate+i].pmc);
 *c_lemm=pt_pmc->p_zon_lemm[pt_pmc->p_tab[indice].i_cate+i].c_lemm;
 }
else
 {
 *proba=(logprob_t)(pt_pmc->p_zon[pt_pmc->p_tab[indice].i_cate+i].pmc);
 *c_lemm=0;
 }
if (trace & SIR_GRAM_TRACE)
 fprintf(stderr,"PMC : P(%ld|%ld)=%lf\n",mot,classe,*proba);

return CORRECT;
}

/*................................................................*/

/* fonction qui renvoi la liste des classes possibles pour un mot
   avec les proba (MODIF 1101) */

err_t pmc_liste_mot_classe(wrd_index_t *tabl,logprob_t *tpro,int *nb,
	const wrd_index_t mot, ty_pmc pt_pmc, const err_t trace)
{
static wrd_index_t indice,i;

if (!acces_dicho(mot,0,pt_pmc->nb_tab,&indice,pt_pmc->p_tab))
 {
 if (trace & SIR_GRAM_TRACE)
  fprintf(stderr,"PMC : Je ne connais pas le mot : %ld\n",mot);
 *nb=0;
 return GRAM_CODE_UNKNOWN;
 }

if (pt_pmc->p_tab[indice].nb_cate==0)
 {
 if (trace & SIR_GRAM_TRACE)
  fprintf(stderr,"PMC : Le mot : %ld n'a pas ete vu lors de l'apprentissage\n",mot);
 *nb=0;
 return GRAM_CODE_UNKNOWN;
 }
*nb=0;

if (pt_pmc->si_lemme) /* il y a des lemmes */
 {
 for(i=0;i<pt_pmc->p_tab[indice].nb_cate;i++)
  {
  tabl[*nb]=pt_pmc->p_zon_lemm[pt_pmc->p_tab[indice].i_cate+i].c_cate;
  tpro[*nb]=(logprob_t)pt_pmc->p_zon_lemm[pt_pmc->p_tab[indice].i_cate+i].pmc;
  (*nb)++;
  }
 }
else
 {
 for(i=0;i<pt_pmc->p_tab[indice].nb_cate;i++)
  {
  tabl[*nb]=pt_pmc->p_zon[pt_pmc->p_tab[indice].i_cate+i].c_cate;
  tpro[*nb]=(logprob_t)pt_pmc->p_zon[pt_pmc->p_tab[indice].i_cate+i].pmc;
  (*nb)++;
  }
 }
return CORRECT;
}