DecoupeMots.c
10.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
/*
# --------------------------------------------------------
# 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
# ..................................................................
*/
/* Decoupe un corpus en accord avec un dico compile
par CompileLexiTree et une table de separateur hard-codee
Entree : stdin pour le texte et 1er parametre pour le dico
Sortie : stdout */
/* FRED 0199 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
/* Definition des separateurs de mots */
char TablSeparateur[]=
{
' ','-','_','\'','"','*','%','+','/', /* separateur intra/inter mots */
'.',';',',','?','!',':','<','>','«','»', /* separateur inter mots */
'\'','`','"', /* separateur inter mots (DUC2006) */
'(',')','[',']','{','}', /* separateur phrase */
'\n','\0'} ; /* fin de phrase */
char TablSeparateurLazy[]=
{
' ','\'','"', /* separateur intra/inter mots */
'.',';',',','?','!',':','«','»', /* separateur inter mots */
'(',')','[',']','{','}', /* separateur phrase */
'\n','\0'} ; /* fin de phrase */
int SiSeparateur(char c)
{
static int n;
if (c=='\0') return 1;
for(n=0;TablSeparateur[n];n++) if (c==TablSeparateur[n]) return 1;
return 0;
}
int SiSeparateurLazy(char c)
{
static int n;
if (c=='\0') return 1;
for(n=0;TablSeparateurLazy[n];n++) if (c==TablSeparateurLazy[n]) return 1;
return 0;
}
/*................................................................*/
/* src/format/GestionTablMots.c */
/* Permet de charger un fichier lexique compile avec 'CompileLexiTree'
puis d'utiliser la fonction 'Present' afin d'obtenir la plus
longue chaine compatible avec le lexique */
/* FRED 0199 */
#define debug_GestionTablMots 0
typedef struct
{
char c,mot;
unsigned int fg,fd;
} type_stoktree;
type_stoktree *StockTree;
int NbNode;
#define SiIgnoreBlanc 1
/* Chargement de l'arbre-tableau */
void ChargeLexiqueCompile(ch)
char *ch;
{
FILE *file;
if (!(file=fopen(ch,"rb")))
{ fprintf(stderr,"Can't open : %s\n",ch); exit(0); }
fseek(file,0,SEEK_END);
NbNode=(int)(ftell(file)/sizeof(type_stoktree));
fseek(file,0,SEEK_SET);
StockTree=(type_stoktree *)malloc(sizeof(type_stoktree)*(NbNode));
if (fread(StockTree,sizeof(type_stoktree),NbNode+1,file)!=NbNode)
{
fprintf(stderr,"Erreur lors de la lecture du fichier : %s\n",ch);
exit(0);
}
fclose(file);
}
/* Decoupage */
int EgalChar(c_test,c_arbre)
char c_test,c_arbre;
{
if (SiIgnoreBlanc)
if (((c_test==' ')||(c_test=='\n'))&&(c_arbre=='_')) return 1; /* MODIF FRED 11/2003 */
/*if ((c_test==' ')&&((c_arbre=='_')*//*||(c_arbre=='-'))) return 1;*/
return 0;
}
/* 'Present' permet d'obtenir la plus longue chaine compatible avec
le lexique. Les parametres sont les suivants :
- ch : chaine a analyser en entree
- addr : addresse dans l'arbre, a initialiser a 1
- lastword : renvoi l'adresse du dernier caractere de la chaine 'ch'
qui fait partie d'un mot du lexique
- indice : indice du caractere en cours, a initialiser a 0
- refe : tableau de caractere qui va contenir le mot reference
le plus long, dans le lexique, a partir de la chaine 'ch'
- lastindice: indice de la fin du mot le plus long dans refe
Si l'ensemble de la chaine 'ch' forme un mot du lexique, alors 'Present'
retourne la valeur 1, et 0 sinon */
int Present(char *ch, unsigned int addr, char **lastword, int indice, char *refe, int *lastindice, int if_lazy)
{
static int y;
if (addr==0) return 0;
if ((*ch==StockTree[addr].c)||(EgalChar(*ch,StockTree[addr].c)))
{
refe[indice]=StockTree[addr].c;
if (debug_GestionTablMots)
{
for(y=0;y<=indice;y++) printf("%c",refe[y]);
printf("\navec StockTree[addr].mot=%d ch[1]=[%c]\n",StockTree[addr].mot,ch[1]);
}
if (if_lazy)
{
if ((StockTree[addr].mot)&&((ch[1]=='\0')||(SiSeparateurLazy(ch[1]))||(SiSeparateurLazy(ch[0])))) { *lastword=ch; *lastindice=indice+1; }
}
else
{
if ((StockTree[addr].mot)&&((ch[1]=='\0')||(SiSeparateur(ch[1]))||(SiSeparateur(ch[0])))) { *lastword=ch; *lastindice=indice+1; }
}
if ((ch[1]=='\0')&&(StockTree[addr].mot)) return 1;
return Present(ch+1,StockTree[addr].fg,lastword,indice+1,refe,lastindice,if_lazy);
}
return Present(ch,StockTree[addr].fd,lastword,indice,refe,lastindice,if_lazy);
}
/*................................................................*/
#define TailleLigne 8000
void ProcessLine(char *ch, int if_lazy)
{
char refe[TailleLigne],*lastword;
int lastindice,siblanc=0;
while ((*ch)&&(*ch!='\n'))
{
if (*ch!=' ')
{
lastword=NULL;
Present(ch,1,&lastword,0,refe,&lastindice,if_lazy);
if (lastword)
{
refe[lastindice]='\0';
printf("%s ",refe);
ch=lastword+1;
siblanc=1;
}
else
{
/* Politique : on coupe les mots d'un qu'on trouve un separateur
sauf si le separateur est inclu a un mot du dico */
siblanc=0;
if (if_lazy) while (!SiSeparateurLazy(*ch)) printf("%c",*ch++);
else while (!SiSeparateur(*ch)) printf("%c",*ch++);
if (*ch!=' ') printf(" %c ",*ch++);
}
}
else
{
while (*ch==' ') ch++;
if (siblanc==0) printf(" ");
}
}
if (*ch=='\n') printf("\n");
}
#define True 1
#define False 0
#define SIZE_BUFFER 800000
#define SIZE_LINE 80
#define BLANK(a) (((a)==' ')||((a)=='\n')||((a)=='\t'))
#define CAPITAL(a) (((a)>='A')&&((a)<='Z'))
#define NUMBER(a) (((a)>='0')&&((a)<='9'))
#define MAX_SIZE_TAG 80
int format_ben(char *ch,char **lastword, char *refe, int *lastindice)
{
int i;
/* SGML tags <?*> */
if ((ch[0]=='<')&&(ch[1]!='>'))
{
refe[0]=ch[0];
for(i=1;(ch[i])&&(i<MAX_SIZE_TAG)&&(ch[i]!='>')&&(ch[i]!='<');i++) refe[i]=ch[i];
if (ch[i]=='>')
{
refe[i]=ch[i];
refe[i+1]='\0';
*lastindice=i+1;
*lastword=(ch+i);
return True;
}
else return False;
}
/* forms starting with '$' */
if ((ch[0]!='$')||(!(CAPITAL(ch[1])))) return False;
refe[0]=ch[0];
refe[1]=ch[1];
for(i=2;(!BLANK(ch[i]))&&((CAPITAL(ch[i]))||(NUMBER(ch[i]))||(ch[i]=='_'));i++)
refe[i]=ch[i];
if (BLANK(ch[i]))
{
*lastindice=i;
*lastword=(ch+i-1);
return True;
}
else return False;
}
void ProcessBuffer(FILE *file, int if_ben, int keep_fmt, int if_lazy)
{
static char buffer[SIZE_BUFFER+1]; /* BUG FIXED: +1 !!!! FRED 0105 */
int nbread,fini,i;
char refe[TailleLigne],*lastword,*ch;
int lastindice,siblanc=0,t;
nbread=fread(buffer,sizeof(char),SIZE_BUFFER,file);
for(t=0,ch=buffer;ch!=buffer+nbread;)
{
if (!(BLANK(*ch)))
{
lastword=NULL;
if ((if_ben==False)||(format_ben(ch,&lastword,refe,&lastindice)==False))
Present(ch,1,&lastword,0,refe,&lastindice,if_lazy);
if (lastword)
{
refe[lastindice]='\0';
printf("%s ",refe);
t+=strlen(refe);
ch=lastword+1;
siblanc=1;
}
else
{
/* Politique : on coupe les mots d'un qu'on trouve un separateur
sauf si le separateur est inclu a un mot du dico */
siblanc=0;
if (if_lazy) while (!SiSeparateurLazy(*ch)) { printf("%c",*ch++); t++; }
else while (!SiSeparateur(*ch)) { printf("%c",*ch++); t++; }
if ((*ch!=' ')&&(*ch!='\n')) { printf(" %c ",*ch++); t+=3; }
else
if ((!keep_fmt)&&(*ch=='\n')) *ch=' ';
}
}
else
{
while (BLANK(*ch)) { if (keep_fmt) printf("%c",*ch); ch++; }
if (!keep_fmt)
{
if (t>SIZE_LINE) { t=0; printf("\n"); } else { printf(" "); t++; }
}
/* on est sur un <espace> on "vide" et re-rempli le buffer */
if ((nbread==SIZE_BUFFER)&&((int)(ch-buffer)>(SIZE_BUFFER/2)))
{
/*printf("\nVIDE: nbread=%d processed=%d to be done = %d",
nbread,(int)(ch-buffer),SIZE_BUFFER-(int)(ch-buffer));*/
for(i=0;i<SIZE_BUFFER-(int)(ch-buffer);i++) buffer[i]=ch[i];
nbread=fread(buffer+i,sizeof(char),(int)(ch-buffer),stdin);
ch=buffer;
/*printf(" || nbread=%d new nbread=%d\n",nbread,nbread+i);*/
nbread+=i;
buffer[nbread]='\0';
}
}
}
}
/* Prog Principal */
int main(argc,argv)
int argc;
char **argv;
{
char ch[TailleLigne];
int nb,if_ben,keep_fmt,if_lazy;
if (argc<2)
{
fprintf(stderr,"Syntaxe : %s [-h] [-ben] [-keep_fmt] [-lazy] <lexi compile>\n",argv[0]);
exit(0);
}
if (!strcmp(argv[1],"-h"))
{
fprintf(stderr,"Syntaxe : %s [-h] <lexi compile>\n\
\t Decoupe un corpus en accord avec un dico compile\n\
\t par CompileLexiTree et une table de separateur hard-codee\n\
\t dans le tableau 'TablSeparateur'. La politique de segmentation\n\
\t est la suivante : on concatene les mots suivant la plus grande\n\
\t expression composee trouvee dans le dico. Lorsqu'un\n\
\t mot contient un caractere separateur, s'il fait partie du\n\
\t dico, on le laisse tel-quel, sinon on l'eclate autour\n\
\t du caractere separateur.\n\
\t Modif Novembre 2004: l'option '-ben' permet de ne pas tokeniser\n\
\t les mots qui commencent par un '$' suivi d'une lettre majuscule\n\
\t suivi d'une sequence quelconque de lettre majuscule, de '_' ou de\n\
\t chiffres ainsi que toute les balises '<?*>'\n\
\t Modif Novembre 2006: l'option '-lazy' permet de ne considerer comme separateurs\n\
\t que les ponctuations: '.' ':' ',' ';' '!' '?' et c'est tout.\n\
\t Entree : stdin pour le texte et 1er parametre pour le dico\n\
\t Sortie : stdout\n",argv[0]);
exit(0);
}
/*fprintf(stderr,"Chargement de l'arbre dans le tableau -> ");*/
if_ben=keep_fmt=if_lazy=False;
for(nb=1;nb<argc;nb++)
if (!strcmp(argv[nb],"-ben")) if_ben=True; else
if (!strcmp(argv[nb],"-keep_fmt")) keep_fmt=True; else
if (!strcmp(argv[nb],"-lazy")) if_lazy=True;
else ChargeLexiqueCompile(argv[nb]);
/*fprintf(stderr,"Termine\n");*/
/*fprintf(stderr,"On a lu : %d noeuds\n",NbNode);*/
ProcessBuffer(stdin,if_ben,keep_fmt,if_lazy);
/* while(fgets(ch,TailleLigne,stdin)) ProcessLine(ch,if_lazy); */
return 0;
}