Skip to content
Snippets Groups Projects
produce_action_string_fst.c 4.57 KiB
/*  Produce the simplest string producing a given action  */
/*  FRED 0215  */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

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

#define TailleLigne     8000

#define True    1
#define False   0

void ERREUR(char *ch1,char *ch2)
{
fprintf(stderr,"ERREUR : %s %s\n",ch1,ch2);
exit(0);
}

void ERREURd(char *ch1, int i)
{
fprintf(stderr,"ERREUR : %s %d\n",ch1,i);
exit(0);
}

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

/* format:
action(1,1,"start_scene1","uno") 2
action(1,1,"#ENDSEQUENCE(1)","") 3
action(1,1,"#ENDSECTION(1)","") 4
action(2,1,"open_scene2","dos") 5
action(2,1,"open_2A","open_system") 6
action(2,1,"#ENDSEQUENCE(1)","") 7
action(2,2,"start_system_voice","tell_me") 8
action(2,2,"open_2B","open_technical_characteristics") 9
action(2,2,"open_2B1","read") 10
action(2,2,"open_2B2","next") 11
action(2,2,"open_2B3","yes") 12
action(2,2,"open_2B4","read") 13
action(2,2,"open_2B5","download") 14
action(2,2,"open_2C","open_the_terms_and_conditions_of_use_of_body_x_epsilon_system_three_point_zero") 15
action(2,2,"open_2C1","accept_terms_and_conditions_of_use") 16
action(2,2,"open_2C2","next") 17
action(2,2,"open_2D","install_the_new_version_of_me") 18
action(2,2,"#end","give_me_my_data") 19
action(2,2,"#ENDSEQUENCE(2)","") 20
action(2,2,"#ENDSECTION(2)","") 21
*/

#define MAX_ACTION	2000

typedef struct
	{
	char *ch;
	int nbsec,code;
	} type_action;

type_action T_action[MAX_ACTION];

void load_action(char *chfile)
{
FILE *file;
char ch[TailleLigne],*chcode;
int code,i,nb,nbsec;
if (!(file=fopen(chfile,"rt"))) ERREUR("can't open:",chfile);
for(nb=0;fgets(ch,TailleLigne,file);) if (strstr(ch,"action("))
 {
 if (nb==MAX_ACTION) ERREUR("cste MAX_ACTION too small","");
 chcode=strtok(ch," \n"); if (chcode) chcode=strtok(NULL," \n"); if (!chcode) ERREUR("bad format:",ch);
 if (sscanf(ch,"action(%d,",&nbsec)!=1) ERREUR("bad format:",ch);
 if (sscanf(chcode,"%d",&code)!=1) ERREUR("bad format:",chcode);
 T_action[nb].ch=strdup(ch);
 T_action[nb].nbsec=nbsec;
 T_action[nb].code=code;
 nb++;
 }
if (nb==MAX_ACTION) ERREUR("cste MAX_ACTION too small","");
T_action[nb].ch=NULL;
fclose(file);
}

void print_fst_section(FILE *file, int nbsec, int nbac)
{
int i;
for(i=0;T_action[i].ch;i++) if ((T_action[i].nbsec==nbsec)&&(i!=nbac))
 fprintf(file,"0\t0\t%d\n",T_action[i].code);
fprintf(file,"0\t1\t%d\n",T_action[nbac].code);
fprintf(file,"1\n");
}

void process_section(int nbsec, char* fst, char* dico)
{
int i,j,nb;
static char ch[TailleLigne],*t_field[10];
FILE *file;
for(i=0;T_action[i].ch;i++) if (T_action[i].nbsec==nbsec)
 {
 sprintf(ch,"temp.tmp");
 if (!(file=fopen(ch,"wt"))) ERREUR("can't write in:",ch);
 print_fst_section(file,nbsec,i);
 fclose(file);
 sprintf(ch,"fstcompile --acceptor temp.tmp | fstarcsort > temp.fst");
 system(ch);
 sprintf(ch,"fstarcsort %s | fstcompose - temp.fst | fstshortestpath | fsttopsort | fstproject | fstrmepsilon | fstprint --isymbols=%s > temp.fst.txt",fst,dico);
 system(ch);
 if (!(file=fopen("temp.fst.txt","rt"))) ERREUR("can't read:","temp.fst.txt");
 printf("%s\t",T_action[i].ch);
 for(j=0;fgets(ch,TailleLigne,file);)
  {
  for(nb=0;nb<10;nb++) t_field[nb]=NULL;
  for(nb=1,t_field[0]=strtok(ch," \t\n\r");t_field[nb-1];nb++) t_field[nb]=strtok(NULL," \t\n\r");
  if (t_field[2]) { if (j>0) printf(" "); printf("%s",t_field[2]); j++; }
  }
 if (j==0) printf("WARNING");
 printf("\n");
 fclose(file);
 }
}

int main(int argc, char **argv)
{
int nb, section = -1;
char ch[TailleLigne],*chaction,*fst,*dico;

chaction=fst=dico=NULL;
if (argc>1)
 for(nb=1;nb<argc;nb++)
  if (!strcmp(argv[nb],"-action"))
   {
   if (nb+1==argc) ERREUR("must have a value after argument;",argv[nb]);
   chaction=argv[++nb];
   }
  else
  if (!strcmp(argv[nb],"-fst"))
   {
   if (nb+1==argc) ERREUR("must have a value after argument;",argv[nb]);
   fst=argv[++nb];
   }
  else
  if (!strcmp(argv[nb],"-dico"))
   {
   if (nb+1==argc) ERREUR("must have a value after argument;",argv[nb]);
   dico=argv[++nb];
   }
  else
  if (!strcmp(argv[nb],"-section"))
   {
   if (nb+1==argc) ERREUR("must have a value after argument;",argv[nb]);
   section=atoi(argv[++nb]);
   }
  else
  if (!strcmp(argv[nb],"-h"))
   {
   fprintf(stderr,"Syntax: %s [options] \n-action <caction>\n-fst <fst>\n-dico <dico>\n-section <section>\n",argv[0]);
   exit(0);
   }
  else ERREUR("unknown option:",argv[nb]);
if (!chaction || !fst || !dico || section==-1) ERREUR("bad syntax, check '-h'","");
load_action(chaction);
//for(nb=0;nb<10;nb++) process_section(nb);
process_section(section, fst, dico);
exit(0);
}