Blame view

dico.c 2.54 KB
0bc3d1ad   bjeanlou   dico created
1
2
3
  #include "dico.h"
  
  void make_empty_dico(dico d){
003d3e48   bjeanlou   update6 withHash
4
    for(int i=0;i<NBCHAR;i++)
0bc3d1ad   bjeanlou   dico created
5
6
      d[i]=NULL;
  }
a0fe64d2   bjeanlou   Update dico
7
  void delete_dico(dico d){
cf53766d   bjeanlou   update2 dico
8
    for(int i=0;i<NBCHAR;i++)
0bc3d1ad   bjeanlou   dico created
9
10
11
      delete_tree(d[i]);
  }
  
11c78e6d   bjeanlou   update5 withHash
12
13
  
  
003d3e48   bjeanlou   update6 withHash
14
  bool addto_dico(dico d,const string s){
11c78e6d   bjeanlou   update5 withHash
15
16
17
18
    byte endKind=end_kind(s);
    if(!is_word(endKind)){
      printf("incorrect word");
      return true;
a0fe64d2   bjeanlou   Update dico
19
    }
11c78e6d   bjeanlou   update5 withHash
20
21
22
23
24
25
26
    if(is_empty(d[hash(s[0])])){
       d[hash(s[0])]=make_node(s[0],0);
       addto_tree2(d[hash(s[0])],s+1,endKind);
       return false;
    }
    else {
      return addto_tree(d[hash(s[0])],s+1,endKind);
a0fe64d2   bjeanlou   Update dico
27
    }
a0fe64d2   bjeanlou   Update dico
28
29
  }
  
11c78e6d   bjeanlou   update5 withHash
30
31
32
33
34
  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");
      return;
62535895   bjeanlou   Update1 dico
35
    }
003d3e48   bjeanlou   update6 withHash
36
37
38
39
    char word[30]={0};
    while(fscanf(stream,"%s",word)!=EOF)
      addto_dico(d,word);
    printf("load success");
11c78e6d   bjeanlou   update5 withHash
40
41
  }
  
cf53766d   bjeanlou   update2 dico
42
  void printto_terminal(dico d){printto_file(d,stdout);} 
003d3e48   bjeanlou   update6 withHash
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  void printto_file(dico d,FILE*stream){
    if(stream==NULL){
      printf("sorry, we can't open the file");
      return;
    }
    if(d==NULL){
      printf("sorry, we can't open the dictionary");
      return;
    }
    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;
cf53766d   bjeanlou   update2 dico
61
62
63
64
65
    
    string word=calloc((strlen(prefix)+2),sizeof(char));
    strcpy(word,prefix);
    strncat(word,&(t->letter),1);
    
003d3e48   bjeanlou   update6 withHash
66
    if(is_end(t)){
cf53766d   bjeanlou   update2 dico
67
68
69
70
71
      string word2=calloc((strlen(prefix)+4),sizeof(char));
      strcpy(word2,word);
      //common_end
      if(is_common_end(t)){
        if(is_straight_end(t)){
fd1ee590   bjeanlou   Update7 withHash
72
  	fprintf(stream,"%s\n",word2);
cf53766d   bjeanlou   update2 dico
73
74
        }
        if(ends_with_apostrophe(t)){
fd1ee590   bjeanlou   Update7 withHash
75
  	fprintf(stream,"%s's\n",word2);
cf53766d   bjeanlou   update2 dico
76
        }
003d3e48   bjeanlou   update6 withHash
77
      }
cf53766d   bjeanlou   update2 dico
78
      //proper_end
003d3e48   bjeanlou   update6 withHash
79
      if(is_proper_end(t)){
cf53766d   bjeanlou   update2 dico
80
81
        word2[0]=toupper(word2[0]);
        if(is_straight_end(t)){
fd1ee590   bjeanlou   Update7 withHash
82
  	fprintf(stream,"%s\n",word2);
cf53766d   bjeanlou   update2 dico
83
84
        }
        if(ends_with_apostrophe(t)){
fd1ee590   bjeanlou   Update7 withHash
85
  	fprintf(stream,"%s's\n",word2);
cf53766d   bjeanlou   update2 dico
86
        }
003d3e48   bjeanlou   update6 withHash
87
      }
cf53766d   bjeanlou   update2 dico
88
89
90
91
      //acronyme_end
      if(is_acronyme_end(t)){
        strupper(word2);
        if(is_straight_end(t)){
fd1ee590   bjeanlou   Update7 withHash
92
  	fprintf(stream,"%s\n",word2);
cf53766d   bjeanlou   update2 dico
93
94
        }
        if(ends_with_apostrophe(t)){
fd1ee590   bjeanlou   Update7 withHash
95
  	fprintf(stream,"%s's\n",word2);
cf53766d   bjeanlou   update2 dico
96
        }
003d3e48   bjeanlou   update6 withHash
97
      }
cf53766d   bjeanlou   update2 dico
98
      free(word2);
003d3e48   bjeanlou   update6 withHash
99
100
    }
    for(int i=0;i<NBCHAR;i++){
cf53766d   bjeanlou   update2 dico
101
      print(t->next[i],stream,word);
003d3e48   bjeanlou   update6 withHash
102
    }
cf53766d   bjeanlou   update2 dico
103
104
105
106
107
108
      free(word);
  }
  
  void strupper(string str){
    for(int i=0;str[i]!='\0';i++)
      str[i]=toupper(str[i]);
0bc3d1ad   bjeanlou   dico created
109
  }
fd1ee590   bjeanlou   Update7 withHash
110
111
112
113
114
115
116
117
118
119
120
121
122
  
  
  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));
  }