Blame view

tools/lia_ltbox/lia_phon/src/format/DecoupePhrases.c 9.34 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
  /*
      --------------------------------------------------------
      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
      ..................................................................
  */
  /*  Decoupe un texte tokenise par DecoupeMots en phrases
      en accord avec un certain nombre d'heuristiques
      Les separateurs de phrases sont :
      <s>  : debut de phrase
      </s> : fin de phrase
      Entree : stdin
      Sortie : stdout  */
  /*  FRED 0199  */
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <strings.h>
  #include <GestionTablMots.h>
  
  #define TailleLigne	8000
  
  /* Gestion de la liste de mots necessitant de ne pas considere le '.' comme separateur */
  
  char CharSeparateur;
  
  char TablSeparateur[]=
  	{
  	'.', '!', '?',		/* separateur 'classique' de fin de phrase */
  	'\0'
  	};
  
  int SiSeparateur(c)
   char c;
  {
  static int n;
  for(n=0;TablSeparateur[n];n++) if (c==TablSeparateur[n]) return 1;
  return 0;
  }
  
  int MotSeparateur(ch)
   char *ch;
  {
  if ((ch[1]=='\0')&&(SiSeparateur(ch[0]))) return 1;
  if (!strcmp(ch,"...")) return 1;
  return 0;
  }
  
  #define SiMajuscule(a)	((((a)>='A')&&((a)<='Z'))?1:0)
  #define SiChiffre(a)	((((a)>='0')&&((a)<='9'))?1:0)
  #define SiUnite(a)	((((a)=='%')||((a)=='$')||((a)=='F')||((a)==',')||((a)=='.'))?1:0)
  #define SiDebutBloc(a)	((((a)=='[')||((a)=='(')||((a)=='"'))?1:0)
  #define SiFinBloc(a)	((((a)==']')||((a)==')')||((a)=='"'))?1:0)
  
  int DebutParagraphe(c)
   char c;
  {
  if ((c=='-')||(c=='*')||(c=='(')||(c=='[')) return 1;
  return 0;
  }
  
  /*  Definition de la fenetre d'examen  */
  
  /* On regarde 5 mots avant et 5 mots apres */
  #define TailleFenetre	11
  #define SpotFenetre	5
  
  #define TailleMot	200
  
  char *Fenetre[TailleFenetre];
  char Phrase[TailleFenetre][TailleMot];
  
  /*  Heuristiques sur la fin de phrase  */
  
  int CaraDebutPossible(ch)
   char *ch;
  {
  /* un cara est un debut possible s'il commence :
      * par une majuscule
      * par un chiffre
      * par un caractere debut_paragraphe */
  if (SiMajuscule(*ch)) return 1;
  if (SiChiffre(*ch)) return 1;
  if (DebutParagraphe(*ch)) return 1;
  return 0;
  }
  
  int JustDigit(ch)
   char *ch;
  {
  int n;
  for(n=0;ch[n];n++) if (!SiChiffre(ch[n])) return 0;
  return 1;
  }
  
  int MotDebutPossible(ch)
   char *ch;
  {
  /* un mot est un debut possible s'il est compose :
      * une majuscule suivie de minuscule
      * un nombre
      * par un caractere debut_paragraphe */
  int n;
  
  if (SiMajuscule(*ch))
   {
   for(n=1;(ch[n])&&(!(SiMajuscule(ch[n])));n++);
   if (ch[n]=='\0') return 1;
   }
  if (SiChiffre(*ch))
   {
   for(n=1;(ch[n])&&(SiChiffre(ch[n])||(SiUnite(ch[n])));n++);
   if (ch[n]=='\0') return 1;
   }
  if (DebutParagraphe(*ch)) return 1;
  return 0;
  }
  
  int TraiteFinPhrase()
  {
  /* si on a [. 
   
   ou plus] alors  fin_phrase */
  if ((Fenetre[SpotFenetre+1][0]=='
  ') &&
      (Fenetre[SpotFenetre+1][1]>=2))
  	strcat(Fenetre[SpotFenetre],"
  </s>
  <s>
  ");
  
  /* si on a [. 
   cara_debu_possible] alors  fin_phrase */
  if ((Fenetre[SpotFenetre+1][0]=='
  ') &&
      (CaraDebutPossible(Fenetre[SpotFenetre+2])))
  	strcat(Fenetre[SpotFenetre],"
  </s>
  <s>
  ");
  
  /* si on a [chif . chif quantificateur alors pas de fin de phrase */
  if ((JustDigit(Fenetre[SpotFenetre-1]))&&(JustDigit(Fenetre[SpotFenetre+1])) &&
      (JustPresent(Fenetre[SpotFenetre+2])))
  	return 0;
  
  /* si on a [. mot_debut_possible] alors  fin_phrase */
  if (MotDebutPossible(Fenetre[SpotFenetre+1]))
  	strcat(Fenetre[SpotFenetre],"
  </s>
  <s>
  ");
  
  /* si on a [. fin_bloc 
   cara_debu_possible] alors  fin_phrase */
  if ((SiFinBloc(Fenetre[SpotFenetre+1][0])) &&
      (Fenetre[SpotFenetre+2][0]=='
  ') &&
      (CaraDebutPossible(Fenetre[SpotFenetre+3])))
  	strcat(Fenetre[SpotFenetre+1],"
  </s>
  <s>
  ");
  
  /* si on a [. fin_bloc/debut_bloc mot_debut_possible] alors  fin_phrase */
  if (((SiDebutBloc(Fenetre[SpotFenetre+1][0]))||(SiFinBloc(Fenetre[SpotFenetre+1][0]))) &&
      (MotDebutPossible(Fenetre[SpotFenetre+2])))
  	strcat(Fenetre[SpotFenetre+1],"
  </s>
  <s>
  ");
  
  /* si on a [. 
   debut_bloc cara_debu_possible] alors  fin_phrase */
  if ((SiDebutBloc(Fenetre[SpotFenetre+2][0])) &&
      (Fenetre[SpotFenetre+1][0]=='
  ') &&
      (CaraDebutPossible(Fenetre[SpotFenetre+3])))
  	strcat(Fenetre[SpotFenetre],"
  </s>
  <s>
  ");
  
  return 1;
  }
  
  void TraiteLigne(ch)
   char *ch;
  {
  static int i_phrase=TailleFenetre,taille=0,last_saut=-1,just_open=1;
  int i,n,l;
  char *pt;
  
   for(pt=strtok(ch," \t");pt;pt=strtok(NULL," \t"))
    {
    i_phrase=i_phrase%TailleFenetre;
    for(i=0;i<TailleFenetre-1;i++) Fenetre[i]=Fenetre[i+1];
    Fenetre[i]=Phrase[i_phrase];
  
    /* on recopie pt dans Phrase[i_phrase] en enlevant 
   sauf s'il est seul 
       car c'est un indice pour le decoupage en ligne et on comptabilise le nb de 
   consecutifs */
    for(n=0;(pt[n])&&(pt[n]!='
  ');n++) Phrase[i_phrase][n]=pt[n];
    if (pt[n]=='
  ')
     if (n>0) { Phrase[i_phrase][n]='\0'; last_saut=-1; }
     else
      {
      if (last_saut==-1)
       { Phrase[i_phrase][0]='
  '; Phrase[i_phrase][1]='\1'; Phrase[i_phrase][2]='\0'; last_saut=i_phrase; /*fprintf(stderr,"premier saut
  ");*/ }
      else /* on incremente le compteur */
       { Phrase[last_saut][1]++; Phrase[i_phrase][0]='\0'; /*fprintf(stderr,"\t saut num : %d
  ",Phrase[last_saut][1]);*/ }
      }
    else { Phrase[i_phrase][n]='\0'; last_saut=-1; }
    i_phrase++;
  
    if (MotSeparateur(Fenetre[SpotFenetre])) TraiteFinPhrase();
    if ((Fenetre[SpotFenetre][0])&&(Fenetre[SpotFenetre][0]!='
  '))
     {
     printf("%s",Fenetre[SpotFenetre]);
     l=strlen(Fenetre[SpotFenetre]);
     /* si les 4 derniers caracteres affiche sont : <s>
   alors, just_open=1, pour eviter les lignes vides */
     if (!strncmp(&(Fenetre[SpotFenetre][l-4]),"<s>
  ",4)) just_open=1; else just_open=0;
     if (Fenetre[SpotFenetre][l-1]=='
  ')
      taille=0;
     else
      {
      taille+=l;
      if (taille>54) { printf("
  "); taille=0; }
      else printf("%c",CharSeparateur);
      }
     }
    else
     if (Fenetre[SpotFenetre][0]=='
  ')
      {
      /*fprintf(stderr,"saut de lignes consecutifs = %d
  ",Fenetre[SpotFenetre][1]);*/
      if ((just_open==0) && (Fenetre[SpotFenetre][1]>=3)) /* on insere une fin de phrase si on ne vient pas d'ouvrir une ligne */
       printf("
  </s>
  <s>
  ");
      }
    }
  }
  
  int main(argc,argv)
   int argc;
   char **argv;
  {
  char ch[TailleLigne];
  int i;
  
  CharSeparateur=' '; /* par defaut, phrase formatees en paragraphe
  		       ATTENTION : si on utilise lia_nett_capital
  		       il FAUT que les phrases soient codees en paragraphe
  		       et non pas 1 mot par ligne !!!! */
  
  if (argc>1)
   {
   if (!strcmp(argv[1],"-h"))
    {
    fprintf(stderr,"Syntaxe : %s [-h] [-l] <list mot virgule> < stdin > stdout
  \
    \t Decoupe un texte en phrases tokenise (par exemple par la
  \
    \t commande 'lia_token') en accord avec un certain nombre
  \
    \t d'heuristiques codes dans la fonction 'TraiteFinPhrase'.
  \
    \t Les separateurs de phrases sont : '<s>' = debut de phrase
  \
    \t et '</s>' = fin de phrase.
  \
    \t Le fichier <list mots virgule compil> contient un lexique
  \
    \t compile avec la commande 'lia_compile_lexitree'. Ce lexique contient
  \
    \t tous les mots 'M' tel que, une expression de la forme :
  \
    \t '<nombre1> . <nombre2> M' ne sera pas decoupee en phrase 
  \
    \t L'option -l permet d'obtenir un mot par ligne, sinon les phrases
  \
    \t sont formatees en paragraphes - ATTENTION : lia_nett_capital n'accepte
  \
    \t que les phrases structurees en paragraphe, donc SANS l'option -l
  \
    \t Merci de votre comprehension !!!!
  ",argv[0]);
    exit(0);
    }
   if ((argc>2)&&(!strcmp(argv[2],"-l"))) CharSeparateur='
  ';
   }
  else
   {
   fprintf(stderr,"Syntaxe : %s [-h] [-l] <list mot virgule> < stdin > stdout
  ",argv[0]);
   exit(0);
   }
  
  ChargeLexiqueCompile(argv[1]);
  
  /* Initialisation de la phrase */
  for(i=0;i<TailleFenetre;i++)
   {
   strcpy(Phrase[i],"");
   Fenetre[i]=Phrase[i];
   }
  
  /* premier tag de debut de phrase */
  printf("<s>
  ");
  
  while(fgets(ch,TailleLigne,stdin)) TraiteLigne(ch);
  
  /* On vide la phrase */
  strcpy(ch,"
  ");
  for(i=0;i<=SpotFenetre;i++) TraiteLigne(ch);
  
  /* dernier tag de fin de phrase */
  printf("</s>
  ");
  
  exit(0); 
  }