Blame view

tools/lia_ltbox/lia_phon/src/format/CompileLexiTree.c 6.57 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
  /*
      --------------------------------------------------------
      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
      ..................................................................
  */
  /*  Codage du lexique_union sous forme d'arbre en 
      partie commune pour le tokenizer  */
  /*  FRED 0199  */
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <strings.h>
  
  #define TailleLigne	8000
  
  /* Structure de codage de l'arbre */
  
  typedef struct type_tree
  	{
  	char c,mot;
  	unsigned int addr;
  	struct type_tree *fg,*fd;
  	} *ty_tree;
  
  typedef struct
  	{
  	char c,mot;
  	unsigned int fg,fd;
  	} type_stoktree;
  
  ty_tree Arbre;
  
  type_stoktree *StockTree;
  
  int NbNode;
  
  /* Gestion des noeuds */
  
  ty_tree NewNode(c,mot)
   char c,mot;
  {
  ty_tree pt;
  pt=(ty_tree)malloc(sizeof(struct type_tree));
  pt->c=c;
  pt->mot=mot;
  pt->fg=pt->fd=NULL;
  NbNode++; /* Le noeud 0 est reserve au test de case NULL */
  pt->addr=(unsigned int)NbNode;
  return pt;
  }
  
  /* Recopie de l'arbre dans le tableau */
  
  void Arbre2Tableau(racine)
   ty_tree racine;
  {
  if (racine==NULL) return;
  if (racine->addr>NbNode)
   { fprintf(stderr,"Depassement : racine->addr=%d
  ",racine->addr); exit(0); }
  if ((racine->fg)&&(racine->fg->addr>NbNode))
   { fprintf(stderr,"Depassement : racine->fg->addr=%d
  ",racine->fg->addr); exit(0); }
  if ((racine->fd)&&(racine->fd->addr>NbNode))
   { fprintf(stderr,"Depassement : racine->fd->addr=%d
  ",racine->fd->addr); exit(0); }
  
  StockTree[racine->addr].c=racine->c;
  StockTree[racine->addr].mot=racine->mot;
  if (racine->fg) StockTree[racine->addr].fg=racine->fg->addr;
  else StockTree[racine->addr].fg=0;
  if (racine->fd) StockTree[racine->addr].fd=racine->fd->addr;
  else StockTree[racine->addr].fd=0;
  Arbre2Tableau(racine->fg);
  Arbre2Tableau(racine->fd);
  }
  
  /* Cree l'arbre lexical */
  
  ty_tree Compile(ch,racine,pere)
   char *ch;
   ty_tree racine,pere;
  {
  if (*ch=='\0')
   {
   if (pere==NULL) { fprintf(stderr,"C'est quoi ce Oaie ....
  "); exit(0); }
   pere->mot=1;
   return racine;
   }
  if (racine==NULL) racine=NewNode(*ch,0);
  if (*ch==racine->c)
   {
   racine->fg=Compile(ch+1,racine->fg,racine);
   return racine;
   }
  racine->fd=Compile(ch,racine->fd,racine);
  return racine;
  }
  
  /* Test la presence d'un mot */
  
  int Present(ch,addr)
   char *ch;
   unsigned int addr;
  {
  if (addr==0) return 0;
  if (*ch==StockTree[addr].c)
   {
   if ((ch[1]=='\0')&&(StockTree[addr].mot)) return 1;
   return Present(ch+1,StockTree[addr].fg);
   }
  return Present(ch,StockTree[addr].fd);
  }
  
  /* Test coherence du dico compile */
  
  void TestCoherence(racine,ch,indice)
   ty_tree racine;
   char *ch;
   int indice;
  {
  int n;
  if (racine==NULL) return;
  ch[indice]=racine->c;
  if (racine->mot)
   {
   for(n=0;n<=indice;n++) printf("%c",ch[n]);
   printf("
  ");
   }
  TestCoherence(racine->fg,ch,indice+1);
  TestCoherence(racine->fd,ch,indice);
  }
  
  void ProfMax(racine,prof,profmax)
   ty_tree racine;
   int prof,*profmax;
  {
  if (racine==NULL)
   {
   if (prof>*profmax) *profmax=prof;
   return;
   }
  ProfMax(racine->fg,prof+1,profmax);
  ProfMax(racine->fd,prof+1,profmax);
  }
  
  /*  Prog Principal  */
  
  int main(argc,argv)
   int argc;
   char **argv;
  {
  char ch[TailleLigne],*pt;
  int nb,prof;
  FILE *file;
  
  if (argc<2)
   {
   fprintf(stderr,"Syntaxe : %s [-h] <lexique_union> <lexi compile>
  ",argv[0]);
   exit(0);
   }
  
  if (!strcmp(argv[1],"-h"))
   {
   fprintf(stderr,"Syntaxe : %s [-h] <lexique_union> <lexi compile>
  \
   \t Ce programme permet de compiler une liste de mots sous forme d'arbre afin
  \
   \t d'etre utilise par les programmes 'lia_token' , 'lia_nett_capital'
  \
   \t et 'lia_nomb2alpha'. Le fichier d'entree doit contenir un mot par ligne, la
  \
   \t graphie devant se trouver dans le premier champs (les autres champs, s'ils
  \
   \t existent sont ignores).
  ",argv[0]);
   exit(0);
   }
  
  if (!(file=fopen(argv[1],"r")))
   {
   fprintf(stderr,"Can't open : %s
  ",argv[1]);
   exit(0);
   }
  
  Arbre=NULL; NbNode=0;
  
  fprintf(stderr,"Lecture du lexique et construction de l'arbre
  ");
  
  for(nb=1;fgets(ch,TailleLigne,file);nb++)
   {
   pt=strtok(ch," 
  ");
   Arbre=Compile(pt,Arbre,NULL);
   if (nb%50000==0) fprintf(stderr,"En cours : %d
  ",nb);
   }
  fclose(file);
  
  prof=0;
  ProfMax(Arbre,0,&prof);
  fprintf(stderr,"La profondeur maximum de l'arbre est : %d
  ",prof);
  fprintf(stderr,"Nombre de noeuds necessaires au stockage : %d (taille=%.2fKo)
  ",
  	(NbNode+1),(float)(sizeof(struct type_tree)*(NbNode+1))/(float)1024);
  fprintf(stderr,"Taille de l'arbre stocke dans le tableau : %.2fKo
  ",
  	(float)(sizeof(type_stoktree)*(NbNode+1))/(float)1024); /* Node+1 car on commence a 1 */
  
  fprintf(stderr,"Chargement de l'arbre dans le tableau -> ");
  StockTree=(type_stoktree *)malloc(sizeof(type_stoktree)*(NbNode+1));
  StockTree[0].c=StockTree[0].mot=(char)0;
  StockTree[0].fg=StockTree[0].fd=0;
  Arbre2Tableau(Arbre);
  fprintf(stderr,"Termine
  ");
  
  fprintf(stderr,"Sauvegarde du tableau lexique -> ");
  sprintf(ch,"%s",argv[2]);
  if (!(file=fopen(ch,"wb")))
   {
   fprintf(stderr,"Can't write in %s
  ",ch);
   exit(0);
   }
  if (fwrite(StockTree,sizeof(type_stoktree),NbNode+1,file)!=NbNode+1)
   {
   fprintf(stderr,"Erreur lors de l'ecriture du fichier : %s
  ",ch);
   exit(0);
   }
  
  fclose(file);
  fprintf(stderr,"Termine
  ");
  
  exit(0); 
  }