Blame view

tools/lia_ltbox/lia_tagg_orig/src/tagg/LitTriMorpho.c 10.6 KB
e6be5137b   Jean-François Rey   reinitialized pro...
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
  /*
  #    --------------------------------------------------------
  #    LIA_TAGG: a statistical POS tagger + syntactic bracketer
  #    --------------------------------------------------------
  #
  #    Copyright (C) 2001 FREDERIC BECHET
  #
  #    ..................................................................
  #
  #    This file is part of LIA_TAGG
  #
  #    LIA_TAGG 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
  #    ..................................................................
  #
  #    Contact :
  #              FREDERIC BECHET - LIA - UNIVERSITE D'AVIGNON
  #              AGROPARC BP1228 84911  AVIGNON  CEDEX 09  FRANCE
  #              frederic.bechet@lia.univ-avignon.fr
  #    ..................................................................
  */
  /*  .................................................  */
  /*  Lits le fichier tri lettre d'une serie de classes  */
  /*  .................................................  */
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <GereTreeMorpho.h>
  #include <LitTriMorpho.h>
  
  #define Lambda1	0.7
  #define Lambda2	0.2
  #define Lambda3	0.1
  
  int NbClass;
  
  
  /*  Allocation memoire  */
  
  char *mystrdup(ch)
   char *ch;
  {
  char *resu;
  resu=(char *)malloc(sizeof(char)*(strlen(ch)+1));
  strcpy(resu,ch);
  return resu;
  }
  
  ty_class NewTabClass(chfile,nnbb)
   char *chfile;
   int *nnbb;
  {
  FILE *file;
  int nb,n,taille;
  char ch[200],tri[3];
  ty_class resu;
  
  if (!(file=fopen(chfile,"rt")))
   { printf("Impossible d'ouvrir : %s
  ",chfile); exit(0); }
  
  fgets(ch,200,file);
  sscanf(ch,"NombreClasse: %d",&nb);
  
  resu=(ty_class)my_malloc(sizeof(struct type_class)*(nb+1));
  resu[0].tree=NULL;
  resu[0].label=(char *)mystrdup("tout");
  resu[0].taille=resu[0].nbtri=resu[0].nbmots=0;
  for (nb=1;fgets(ch,200,file);nb++)
   {
   if (strncmp(ch,"Classe",6))
    { printf("Mauvais format de fichier ....
  "); exit(0); }
  
   for(n=8;ch[n]!=' ';n++);
   ch[n]='\0';
   resu[nb].label=(char *)mystrdup(ch+8);
   resu[nb].code=nb-1;
   sscanf(ch+n+9,"%d %d %d",&resu[nb].taille,&resu[nb].nbtri,&resu[nb].nbmots);
   resu[nb].tree=NULL;
   for (n=0;n<resu[nb].taille;n++)
    {
    fgets(ch,200,file);
    sscanf(ch,"%c%c%c %d",tri,tri+1,tri+2,&taille);
    resu[nb].tree=AjouteTrilettre(resu[nb].tree,tri,taille);
    resu[0].tree=AjouteTrilettre(resu[0].tree,tri,taille);
    }
   resu[nb].score=1.;
  
   resu[0].taille+=resu[nb].taille;
   resu[0].nbtri+=resu[nb].nbtri;
   resu[0].nbmots+=resu[nb].nbmots;
   }
  NbClass=(*nnbb)=nb-1;
  fclose(file);
  return resu;
  }
  
  void LibereClass(class)
   ty_class class;
  {
  int n;
  
  for(n=0;n<=NbClass;n++)
   {
   free(class[n].label);
   LibereArbre(class[n].tree);
   }
  free(class);
  }
  
  void AfficheClass(class,file)
   ty_class class;
   FILE *file;
  {
  int n;
  
  fprintf(file,"NombreClasse: %d
  ",NbClass);
  for(n=0;n<=NbClass;n++)
   {
   fprintf(file,"Classe: %s Taille: %d %d
  ",
  	class[n].label,class[n].taille,class[n].nbtri);
   AfficheArbre(class[n].tree,file);
   }
  }
  
  /*  Determination du score lie a chaque classe  */
  
  void AffecteScoreNaif(class,ch)
   ty_class class;
   char *ch;
  {
  int n,nb,le,sc_tri;
  
  le=strlen(ch);
  for (nb=1;nb<=NbClass;nb++)
   {
   class[nb].score=1.0;
   if (le>MAGIC_N) n=le-MAGIC_N; else n=0;
   for (;n<le-2;n++)
    {
    sc_tri=(int)ScoreTri(ch+n,class[nb].tree);
    class[nb].score+=(double)((double)sc_tri/(double)class[nb].nbmots);
    }
   class[nb].score/=(float)(le-2);
  
   }
  }
  
  void AffecteScore(class,ch,comm)
   ty_class class;
   char *ch;
   int comm;
  {
  int n,nb,le;
  double tmp,somm;
  
  le=strlen(ch);
  if (le>MAGIC_N) n=le-MAGIC_N; else n=0;
  
  for (nb=1;nb<=NbClass;nb++) class[nb].score=1.;
  
  for (;n<le-2;n++)
   if ((int)ScoreTri(ch+n,class[0].tree))
    {
    if (comm)
     printf("%c%c%c  =  %d
  ",ch[n],ch[n+1],ch[n+2],(int)ScoreTri(ch+n,class[0].tree));
  
  
    for (somm=0.,nb=1;nb<=NbClass;nb++)
     somm+=ScoreTri(ch+n,class[nb].tree);
  
    for (nb=1;nb<=NbClass;nb++)
     {
  
     tmp=ScoreTri(ch+n,class[nb].tree)/somm;
     class[nb].score*=tmp;
  
     if (comm)
      printf("  %s=%d -> %lf total : %lf  somm : %lf
  ",class[nb].label,
  	(int)ScoreTri(ch+n,class[nb].tree),tmp,class[nb].score,somm);
     }
    } 
  }
   
  void AffecteScoreTrue(class,classbi,ch,comm)
   ty_class class,classbi;
   char *ch;
   int comm;
  {
  int n,nb,le,nbbi;
  double C1,C2;
  char bich[4];
  
  le=strlen(ch);
  if (le>MAGIC_N) n=le-MAGIC_N; else n=0;
  
  for (nb=1;nb<=NbClass;nb++)
   {
   class[nb].score=(double)((double)class[nb].nbmots/(double)class[0].nbmots);
   if (comm)
    printf("Classe : %s = %d  Total = %d  Score=%lf
  ",
  	class[nb].label,class[nb].nbmots,class[0].nbmots,class[nb].score);
   }
  
  for (;n<le-2;n++)
   {
   bich[0]=ch[n+1];bich[1]=ch[n+2];bich[2]='@';bich[3]='\0';
   if (comm)
    printf("%c%c%c  (%s) =  %d
  ",ch[n],ch[n+1],ch[n+2],bich,(int)ScoreTri(ch+n,class[0].tree));
   for (nb=1;nb<=NbClass;nb++)
    {
    C1=ScoreTri(ch+n,class[nb].tree);
    /*
    for(nbbi=1;(nbbi<=NbClass)&&(strcmp(class[nb].label,classbi[nbbi].label));nbbi++);
    if (nbbi>NbClass) { printf("Probleme : %s
  ",class[nb].label); exit(0); }
    */
  
    nbbi=nb;
    C2=ScoreTri(bich,classbi[nbbi].tree);
    if (((int)C1==0)&&(C1==C2)) C1=0.0000001;
  
    class[nb].score*=(double)(C1/C2);
    if (comm)
     printf("%s  Ctri=%d  Cbi=%d  Prob=%.4lf  Score=%.4lf
  ",class[nb].label,
  	(int)C1,(int)C2,C1/C2,class[nb].score);
    } 
   }
  }
   
  void AffecteScoreTrueII(class,classbi,classun,ch,comm)
   ty_class class,classbi,classun;
   char *ch;
   int comm;
  {
  int n,nb,le;
  double C1,C2,P1,P2,P3;
  char bich1[4],bich2[4],unch1[4],unch2[4];
  
  le=strlen(ch);
  if (le>MAGIC_N) n=le-MAGIC_N; else n=0;
  
  for (nb=1;nb<=NbClass;nb++)
   {
   class[nb].score=(double)((double)class[nb].nbmots/(double)class[0].nbmots);
   if (comm)
    printf("Classe : %s = %d  Total = %d  Score=%lf
  ",
  	class[nb].label,class[nb].nbmots,class[0].nbmots,class[nb].score);
   }
  
  for (;n<le-2;n++)
   {
   bich1[0]=ch[n+1];bich1[1]=ch[n+2];bich1[2]='@';bich1[3]='\0';
   bich2[0]=ch[n];bich2[1]=ch[n+1];bich2[2]='@';bich2[3]='\0';
   unch1[0]=ch[n+1];unch1[1]=unch1[2]='@';unch1[3]='\0';
   unch2[0]=ch[n];unch2[1]=unch2[2]='@';unch2[3]='\0';
  
   if (comm)
    printf("%c%c%c : c2c3=%s c1c2=%s c2=%s c1=%s
  ",ch[n],ch[n+1],ch[n+2],bich1,bich2,unch1,unch2);
  
   for (nb=1;nb<=NbClass;nb++)
    {
    if (comm) printf ("%s ",class[nb].label);
  
    C1=ScoreTri(ch+n,class[nb].tree);
    C2=ScoreTri(bich1,classbi[nb].tree);
  
    if (comm) printf("C1=ScoreTri(%c%c%c,trimodel)=%lf
  ",ch[n],ch[n+1],ch[n+2],C1);
    if (comm) printf("C2=ScoreTri(%s,bimodel)=%lf
  ",bich1,C2); 
  
    if (C2==0) P1=0; else P1=(double)(Lambda1*C1)/C2;
  
    /*if (comm) printf("P(%c|%c%c)=%d/%d=%lf  ",ch[n],ch[n+1],ch[n+2],(int)C1,(int)C2,P1);*/
    if (comm) printf("P(%c|%c%c)=%lf ",ch[n],ch[n+1],ch[n+2],P1);
  
    C1=ScoreTri(bich2,classbi[nb].tree);
    C2=ScoreTri(unch1,classun[nb].tree);
    if (C2==0) P2=0; else P2=(double)(Lambda2*C1)/C2;
  
    /*if (comm) printf("P(%c|%c)=%d/%d=%lf  ",ch[n],ch[n+1],(int)C1,(int)C2,P2);*/
    if (comm) printf("P(%c|%c)=%lf ",ch[n],ch[n+1],P2);
  
    C1=ScoreTri(unch2,classun[nb].tree);
    C2=(double)classun[nb].nbtri;
    if (C2==0) P3=0; else P3=(double)(Lambda3*C1)/C2;
  
    /*if (comm) printf("P(%c)=%d/%d=%lf ",ch[n],(int)C1,(int)C2,P3);*/
    if (comm) printf("P(%c)=%lf  ",ch[n],P3);
  
    class[nb].score*=(double)(P1+P2+P3);
  
    if (comm) printf("P=%lf S=%lf
  ",(double)P1+P2+P3,class[nb].score);
    } 
   }
  }
   
  void AffecteScoreII(class,ch,comm)
   ty_class class;
   char *ch;
   int comm;
  {
  int n,nb,le;
  double tmp;
  
  le=strlen(ch);
  if (le>MAGIC_N) n=le-MAGIC_N; else n=0;
  for (nb=1;nb<=NbClass;nb++) class[nb].score=1.;
  for (;n<le-2;n++)
   if ((int)ScoreTri(ch+n,class[0].tree))
    {
    if (comm)
     printf("%c%c%c  =  %d
  ",ch[n],ch[n+1],ch[n+2],(int)ScoreTri(ch+n,class[0].tree));
    for (nb=1;nb<=NbClass;nb++)
     {
     tmp=(double)(ScoreTri(ch+n,class[nb].tree)/**1000.*/)/(double)class[nb].nbtri;
     class[nb].score*=tmp;
     if (comm)
      printf("  %s=%d -> %lf total : %lf
  ",class[nb].label,
  	(int)ScoreTri(ch+n,class[nb].tree),tmp,class[nb].score);
     }
    }
  }
  
  void SortScore(class,classbi,classun)
   ty_class class,classbi,classun;
  {
  int fini=0,n;
  struct type_class cl;
  
  while(!fini)
   for(n=1,fini=1;n<=NbClass-1;n++)
    if (class[n].score<class[n+1].score)
     {
     fini=0;
     cl=class[n];
     class[n]=class[n+1];
     class[n+1]=cl;
  
     cl=classbi[n];
     classbi[n]=classbi[n+1];
     classbi[n+1]=cl;
  
     cl=classun[n];
     classun[n]=classun[n+1];
     classun[n+1]=cl;
     }
  }
  
  void AfficheScore(class)
   ty_class class;
  {
  int nb;
  
  for (nb=1;nb<=NbClass;nb++)
   printf("%2d] %8s  (%E)
  ",nb,class[nb].label,class[nb].score);
  }
  
  void RangeScore(class,tablch,tablscore,tailletabl)
   ty_class class;
   char **tablch;
   double *tablscore;
   int tailletabl;
  {
  int nb;
  /* on ne copie que jusqu'a 'tailletabl' qui est le nb max de categorie
     qu'a recu un mot dans le lexique utilise par le tagger !! */
  for (nb=1;(nb<=tailletabl)&&(nb<=NbClass);nb++)
   if (class[nb].score>0.0)
    {
    tablch[nb-1]=class[nb].label;
    tablscore[nb-1]=class[nb].score;
    }
  }
  
  /*  Outils  */
  
  void TrouvePlace(ch,mot,cate)
   char *ch,**mot,**cate;
  {
  int n;
  
  *mot=ch;
  for(n=0;(ch[n])&&(ch[n]!=' ');n++);
  ch[n]='\0';
  for(++n;(ch[n])&&(ch[n]==' ');n++);
  *cate=ch+n;
  /*
  for(;(ch[n])&&(ch[n]!=' ');n++);
  ch[n]='\0';
  */
  }
  
  /*  Retablissement des accents  */
  
  void accent_mot(ch)
   char *ch;
  {
  char temp[200];
  int i,n;
  
  for(n=0,i=0;ch[n];n++)
   {
   switch (ch[n+1])
    {
    case '1' :
  	switch (ch[n])
  	 {
  	 case 'e' : temp[i++]='é'; n++; break;
  	 default  : temp[i++]=ch[n];
  	 }
  	break;
    case '2' :
  	switch (ch[n])
  	 {
  	 case 'a' : temp[i++]='à'; n++; break;
  	 case 'e' : temp[i++]='è'; n++; break;
  	 case 'u' : temp[i++]='ù'; n++; break;
  	 default  : temp[i++]=ch[n];
  	 }
  	break;
    case '3' :
  	switch (ch[n])
  	 {
  	 case 'a' : temp[i++]='â'; n++; break;
  	 case 'e' : temp[i++]='ê'; n++; break;
  	 case 'i' : temp[i++]='î'; n++; break;
  	 case 'o' : temp[i++]='ô'; n++; break;
  	 case 'u' : temp[i++]='û'; n++; break;
  	 default  : temp[i++]=ch[n];
  	 }
  	break;
    case '4' :
  	switch (ch[n])
  	 {
  	 case 'a' : temp[i++]='Ì'; n++; break;
  	 case 'e' : temp[i++]='ë'; n++; break;
  	 case 'i' : temp[i++]='ï'; n++; break;
  	 case 'o' : temp[i++]='Î'; n++; break;
  	 case 'u' : temp[i++]='ü'; n++; break;
  	 default  : temp[i++]=ch[n];
  	 }
  	break;
    case '5' :
  	switch (ch[n])
  	 {
  	 case 'c' : temp[i++]='ç'; n++; break;
  	 default  : temp[i++]=ch[n];
  	 }
  	break;
    default  : temp[i++]=ch[n];
    }
   }
  
  temp[i]='\0';
  strcpy(ch,temp);
  }