Select Git revision
fplm_fct.c 2.63 KiB
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "fplm.h"
/** Read a line from the fplm file and extract the form/pos/lemma/morpho.
* Return -1 if there's no more line to read, else the number of string read**/
int read_line_fplm(FILE* fplm, char* form, char* pos, char* lemma, char* morpho)
{
int fields_nb;
char buffer[10000];
if(fgets(buffer, 10000, fplm)==NULL)
return -1;
fields_nb = sscanf(buffer, "%[^\t]\t%s\t%[^\t]\t%s\n", form, pos, lemma, morpho);
return fields_nb;
}
/** Return the class' position in morpho
* (the class could be the tense, the person, the gender or the number of a word)**/
int extract_class_position(CLASS class)
{
switch(class)
{
case TENSE:
return 0;
case PERSON:
return 1;
case GENDER:
return 2;
case NUMBER:
return 3;
default:
fprintf(stderr,"Error morpho\n");
exit(EXIT_FAILURE);
}
return -1;
}
/** Return the class choosen by the user if their class exists**/
int choose_class(char* class)
{
if(!strcmp(class,"tense"))
return TENSE;
else if(!strcmp(class,"person"))
return PERSON;
else if(!strcmp(class,"gender"))
return GENDER;
else if(!strcmp(class,"number"))
return NUMBER;
else
{
fprintf(stderr,"-c argument must be \"tense\", \"person\", \"gender\" or \"number\"\n");
exit(EXIT_FAILURE);
}
return -1;
}
/** Extract the class we want from morpho and write it in morpho_feature**/
void extract_morpho_feature(CLASS class, char* morpho_feature, char* morpho)
{
int cpt_diese = 0;
int i = 0;
int j;
int position = extract_class_position(class);
if(class==TENSE)
{
while(morpho[i]!='#')
{
morpho_feature[i] = morpho[i];
i++;
}
morpho_feature[i] = '\0';
}
else
{
while(i<(int)strlen(morpho) && cpt_diese!=position)
{
if(morpho[i]=='#')
cpt_diese++;
if(cpt_diese==position)
{
i++;
for(j=0; morpho[i]!='#'; j++, i++)
morpho_feature[j] = morpho[i];
}
i++;
}
morpho_feature[j] = '\0';
}
}
/** Write the code_class file (use in predict to know the real class)
* Return the class' code**/
int associate_number_to_classes(FILE* classes_code, CLASS class, char* morpho_feature, int current_morpho_feature)
{
int code = 0;
char tmp[20];
rewind(classes_code);
fscanf(classes_code,"%s",tmp);
while(fscanf(classes_code,"%d %s\n",&code,tmp) == 2)
{
if(class == TENSE)
{
if(!strcmp(tmp, morpho_feature))
return code;
}
else
{
if(tmp[0] == morpho_feature[current_morpho_feature])
return code;
}
}
if(class == TENSE)
fprintf(classes_code, "%d %s\n", code+1, morpho_feature);
else
fprintf(classes_code, "%d %c\n", code+1, morpho_feature[current_morpho_feature]);
return code+1;
}