void databaseAdd(WORD *sentence, int n_words)
{
int i, j;
char seeker[50];
int n_record;
FILE *dat;
/* Storing into a simple list */
/* Format for entries: n_words word type ... */
dat = fopen(DATABASE, "a");
if(!dat)
{
printf("databaseAdd: Error opening %s", DATABASE);
return;
}
goto skipCode;
for(;;) /* Exit on record "E" found */
{
fscanf(dat, "%s", seeker); /* Get record */
if(!strcmp(seeker, "E"))
{
break;
}
sscanf(seeker, "%d", &n_record);
for(j = 0; j < n_record * 2; j++)
{
fscanf(dat, "%s", seeker); /* Step through records */
}
}
fseek(dat, -1, 1); /* Move backwards one character */
skipCode: /* Yes, it's unclean--who cares? */
fprintf(dat, "%d", n_words);
for(i = 0; i < n_words; i++)
{
fprintf(dat, " %s %d", sentence[i].word, sentence[i].code);
}
fprintf(dat, "\n");
fclose(dat);
}
int databaseRetrieve(WORD *question, int n_words, int max_sentences)
{
int n_sentences = 0, n_record, n_match, i, j;
char scanner[50];
char tmp[20][50];
FILE *dat;
/* Retrieve database matches up to max_sentences */
dat = fopen(DATABASE, "r");
if(!dat)
{
printf("Ack!!! I don't know anything.\n");
printf("Alas, I am useless without my database.\n");
printf("Oh where, oh where, has my data file gone?\n");
printf("Oh where, oh where can it be?\n");
printf("I suppose it's not here or I'd have found it.\n");
printf("Create it--then call me again.\n");
printf("Yes, the programmer got bored... ;)\n");
return -1;
}
for(; n_sentences <= max_sentences;) /* Reads through file */
{
n_match = 0;
fscanf(dat, "%s", scanner);
if(!strcmp(scanner, "E"))
{
break;
}
sscanf(scanner, "%d", &n_record);
for(i = 0; i < n_record; i++)
{
fscanf(dat, "%s %s", tmp[i], scanner);
}
for(i = 0; i < n_words; i++)
{
for(j = 0; j < n_record; j++)
{
if(!strcmp(tmp[j], question[i].word))
{
n_match++;
}
}
}
if((float) n_match > (float) n_words * 0.5)
{
n_sentences++;
for(i = 0; i < n_record; i++)
{
printf("%s ", tmp[i]);
}
printf("\n");
}
}
fclose(dat);
return 0;
}
|