dico.c 3.31 KB
#include "dico.h"

void make_empty_dico(dico d){
  for(int i=0;i<NBCHAR;i++)
    d[i]=NULL;
}
void delete_dico(dico d){
  for(int i=0;i<NBCHAR;i++)
    delete_tree(d[i]);
}



byte addto_dico(dico d,const string word){
  //-1 if word can't enter, 0if allready in dico, 1 if added
  int i;
  byte isIn, endKind=end_kind(word);
  printf("%x\n",endKind);
  if(!is_word(endKind)){
    printf("%s is incorrect\n",word);
    return -1;
  }
  tree*tmp=&d[hash(word[0])],*temp;
  //1st, let's go through the tree, until the word is not in tree
  for(i=0; word[i]!='\0' && word[i]!='\'' && !is_empty(*tmp) ;i++){
    temp=tmp;
    tmp=&((*tmp)->next[hash(word[i+1])]);
  }
  //if word is not ended at the end of the tree
  if(is_empty(*tmp)){
    isIn=0;
    for(; word[i]!='\0' && word[i]!='\'';i++){
      *tmp=make_node(tolower(word[i]),NOT_AN_END);
      temp=tmp;
      tmp=&((*tmp)->next[hash(word[i])]);
    }
    (*temp)->isEnd = endKind;
    printf("%s added\n",word);
    fflush(stdout);
  }
  //if word is ended
  else{
    isIn=endKind & (*temp)->isEnd;
    (*temp)->isEnd = endKind | is_end(*temp);
    printf("%s allready in\n",word);
    fflush(stdout);
  }
  return isIn;
}

 




void loadfrom_keyboard(dico d){loadfrom_file(d,stdin);} 
void loadfrom_file(dico d,FILE*stream){
  if(stream==NULL){
    printf("sorry, we can't open the file\n");
    return;
  }
  char word[30]={0};
  while(fscanf(stream,"%30s",word)!=EOF){
    addto_dico(d,word);
  }
  printf("load success\n");
}














void printto_terminal(dico d){printto_file(d,stdout);} 
void printto_file(dico d,FILE*stream){
  if(stream==NULL){
    printf("sorry, we can't open the file\n");
    return;
  }
  if(d==NULL){
    printf("sorry, we can't open the dictionary\n");
    return;
  }
  printf("\n\nThis is what the dictionnary contains :\n");
  for(int i=0;i<NBCHAR;i++){
    print(d[i],stream,"");
  }
}

void print(tree t,FILE*stream,string prefix){
  //needs to check stream!=NULL
  if(is_empty(t))
    return;
  
  string word=calloc((strlen(prefix)+2),sizeof(char));
  strcpy(word,prefix);
  strncat(word,&(t->letter),1);
  
  if(is_end(t)){
    string word2=calloc((strlen(prefix)+4),sizeof(char));
    strcpy(word2,word);
    //common_end
    if(is_common_end(t) && is_straight_end(t)){
      fprintf(stream,"%s\n",word2);
    }
    if(is_common_end(t) && ends_with_apostrophe(t)){
      fprintf(stream,"%s's\n",word2);
    }
    //proper_end1
    word2[0]=toupper(word2[0]);
    if(is_proper_end(t) && is_straight_end(t)){
      fprintf(stream,"%s\n",word2);
    }
    if(is_proper_end(t) && ends_with_apostrophe(t)){
      fprintf(stream,"%s's\n",word2);
    }
    //acronyme_end
    strupper(word2);
    if(is_acronyme_end(t) && is_straight_end(t)){
      fprintf(stream,"%s\n",word2);
    }
    if(is_acronyme_end(t) && ends_with_apostrophe(t)){
      fprintf(stream,"%s's\n",word2);
    }
    
    free(word2);
  }
  for(int i=0;i<NBCHAR;i++){
    print(t->next[i],stream,word);
  }
    free(word);
}

void strupper(string str){
  for(int i=0;str[i]!='\0';i++)
    str[i]=toupper(str[i]);
}


bool is_in(dico d,string word){
  byte endKind=end_kind(word);
  if(!is_word(endKind)){
    return false;
  }
  tree tmp=d[hash(word[0])];
  for(int i=1; word[i]!='\0' && word[i]!='\'' && !is_empty(tmp) ;i++){
    tmp=tmp->next[hash(word[i])];
  }
  return !is_empty(tmp) && (endKind & is_end(tmp));
}