Blame view

tools/sctk-2.4.10/src/sclite/alex.c 6.06 KB
8dcb6dfcb   Yannick Estève   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
  #include "sctk.h"
  
  static int index_search(AUTO_LEX *alex, TEXT *goal, int *success);
  
  void AUTO_LEX_init(AUTO_LEX *alex, int size){
      alex->max = size;
      alex->num = 0;
      alloc_singZ(alex->str,alex->max,TEXT *,(TEXT *)0);
      alloc_singZ(alex->field_a,alex->max,int,0);
      alloc_singZ(alex->field_b,alex->max,int,0);
      alloc_singZ(alex->field_c,alex->max,double,0);
      alloc_singZ(alex->sort,alex->max,int,0);
  }
  
  void AUTO_LEX_free(AUTO_LEX *alex){
      free_2dimarr(alex->str,alex->num,TEXT);
      free_singarr(alex->field_a,int);
      free_singarr(alex->field_b,int);
      free_singarr(alex->field_c,double);
      free_singarr(alex->sort,int);
      alex->max = 0;
      alex->num = 0;
  }
  
  static int index_search(AUTO_LEX *alex, TEXT *goal, int *success)
  {
      int low, high, mid, eval;
   
      low = 0, high = alex->num-1;
   
      if (low > high){
  	*success = 0;
  	return(low);
      }
  
      *success = 1;
      do { 
          mid = (low + high)/2;
          eval = TEXT_strcmp(goal,alex->str[alex->sort[mid]]);
  /*        printf("%s:  %s (%d) [mid %s (%d)] %s (%d) = res %d
  ",
                     goal,list[low],low,list[mid],mid,
                     list[high],high,eval);*/
          if (eval == 0)
              return(mid);
          if (eval < 0)
              high = mid-1;
          else
              low = mid+1;
      } while (low <= high);
   
      *success = 0;
      return(low);
  }
  
  int AUTO_LEX_find(AUTO_LEX *alex, TEXT *str){
    int success = 0;
    int ind = index_search(alex,str,&success);
    if (success)
      return(ind);
    return(-1);
  }
  
  int AUTO_LEX_insert(AUTO_LEX *alex, TEXT *new){
      char *proc = "AUTO_LEX_insert";
      TEXT **newstr;
      int *newsort, ind, success, *new_field_a, *new_field_b;
      double *new_field_c;
  
      if (db >= 1) printf("Entering %s: new='%s'
  ",proc, new);
  
      if (alex->num + 1 >= alex->max) {
  	if (db >= 5)
  	    printf("%s: Expanding Data Array
  ",proc);
  	alloc_singarr(newstr,alex->max * 2,TEXT *);
  	memcpy(newstr,alex->str,sizeof(TEXT *) * alex->max);
  
  	alloc_singarr(newsort,alex->max * 2,int);	
  	memcpy(newsort,alex->sort,sizeof(int) * alex->max);
  
  	alloc_singZ(new_field_a,alex->max * 2,int,0);	
  	memcpy(new_field_a,alex->field_a,sizeof(int) * alex->max);
  
  	alloc_singZ(new_field_b,alex->max * 2,int,0);	
  	memcpy(new_field_b,alex->field_b,sizeof(int) * alex->max);
  
  	alloc_singZ(new_field_c,alex->max * 2,double,0);	
  	memcpy(new_field_c,alex->field_c,sizeof(double) * alex->max);
   
  	alex->max *= 2;
  
  	free_singarr(alex->str,TEXT *);
  	free_singarr(alex->sort,int);
  	free_singarr(alex->field_a,int);
  	free_singarr(alex->field_b,int);
  	free_singarr(alex->field_c,double);
  
  	alex->str = newstr;
  	alex->sort = newsort;
  	alex->field_a = new_field_a;
  	alex->field_b = new_field_b;	
  	alex->field_c = new_field_c;	
      }
  
      ind = index_search(alex,new,&success);
      if (success)
  	return(alex->sort[ind]);
  
      /* insert ind into the list */
      if (db >= 5)
  	printf("Adding to Lexicon, ind = %d
  ",ind);
      
      /* shift data from ind down to num */
      if (ind < alex->num){
  	register int x, *ptr;
  	if (db >= 5)
  	    printf("Shifting down data array
  ");
  	for (x=alex->num, ptr=alex->sort + x; x>ind; x--){
  	    *ptr = *(ptr-1);
  	    ptr--;
  	}
      } 
      alex->sort[ind] = alex->num;
      alex->str[alex->num] = TEXT_strdup(new);
  
      alex->num ++;
  
      return (alex->num-1);
  }
  
  double AUTO_LEX_get_c(AUTO_LEX *alex, int ind){
      char *proc = "AUTO_LEX_get_c";
  
      if (db >= 1) printf("Entering %s:
  ",proc);
      if (ind >= 0 && ind < alex->num)
  	return(alex->field_c[ind]);
      else
  	return(0.0);
  }
  
  TEXT *AUTO_LEX_get(AUTO_LEX *alex, int ind){
      char *proc = "AUTO_LEX_get";
  
      if (db >= 1) printf("Entering %s:
  ",proc);
      if (ind >= 0 && ind < alex->num)
  	return(alex->str[ind]);
      else
  	return(NULL_TEXT);
  }
         
  void AUTO_LEX_dump(AUTO_LEX *alex, FILE *fp){
      char *proc = "AUTO_LEX_dump";
      int ind;
  
      if (db >= 1) printf("Entering %s:
  ",proc);
  
      fprintf(fp,"Dump of AUTO LEX:  %d items
  ",alex->num);
      for (ind = 0; ind < alex->num; ind ++){
  	fprintf(fp,"ind %4d:  Str: %30s  lsort: %3d  field_a: %3d  field_b: %3d  field_c: %f
  ",ind,
  		alex->str[ind],
  		alex->sort[ind],
  		alex->field_a[ind],
  		alex->field_b[ind],
  		alex->field_c[ind]);		
      }
      fprintf(fp,"
  ");
  }
  
  void AUTO_LEX_printout(AUTO_LEX *alex, FILE *fp, char *title, int threshhold){
      char *proc = "AUTO_LEX_printout";
      int ind, ord, sum = 0, sumu = 0;
  
      if (db >= 1) printf("Entering %s:
  ",proc);
  
      /* sort field_a of alex, using field_b as the pointers */
      sort_int_arr(alex->field_a, alex->num, alex->field_b, DECREASING);
  
      /* do a secondary sort so that the words with equal occurances are sorted */
      {   int beg_pos, pos=0, w, ss;
  	while (pos < alex->num){
  	    for (beg_pos = pos; pos < alex->num && 
  		                alex->field_a[alex->field_b[beg_pos]] ==
  		                    alex->field_a[alex->field_b[pos]]; pos++)
  		;
  	    /* printf("  SEt found from %d to %d
  ",beg_pos, pos); */
  	    for (ss=beg_pos; ss<pos; ss++){
  		for (w=0; w<alex->num && alex->sort[w] != alex->field_b[ss]; w++)
  		    ;
  		if (w == alex->num) {
  		    fprintf(scfp,"Error: internal error in %s, Call NIST
  ",proc);
  		    exit(1);
  		}
  		alex->field_b[ss] = w;
  	    }	    
  	    qsort((void *)(alex->field_b + beg_pos), pos - beg_pos,
  		   sizeof(int), qsort_int_compare);
  	    /* dereference the field_b pointers back through alex->sort[] */
  	    for (ss=beg_pos; ss<pos; ss++)
  		alex->field_b[ss] = alex->sort[alex->field_b[ss]];
  	    beg_pos = pos;
  	}
      }
      /* end of the sorting */
  
      for (ind = 0, ord = 0; ind < alex->num; ind ++)
  	if (alex->field_a[alex->sort[ind]] >= threshhold)
  	    sumu++;
      
      fprintf(fp,"%-30s   Total                 (%d)
  ",title,alex->num);
      fprintf(fp,"%-30s   With >=%3d occurances (%d)
  ","",threshhold,sumu);
      fprintf(fp,"
  ");
      for (ind = 0, ord = 0; ind < alex->num; ind ++){
  	if (alex->field_a[alex->sort[alex->field_b[ind]]] >= threshhold){
  	    fprintf(fp,"%4d: %4d  ->  %s
  ",++ord,alex->field_a[alex->field_b[ind]],
  		    alex->str[alex->field_b[ind]]);
  	    sum += alex->field_a[alex->field_b[ind]];
  	}
      }
      fprintf(fp,"     -------
  ");
      fprintf(fp,"     %5d
  ",sum);
      fprintf(fp,"
  ");
  }