File: dict2state.c

package info (click to toggle)
cttex 1.23
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,748 kB
  • ctags: 133
  • sloc: ansic: 2,299; makefile: 53; perl: 21
file content (301 lines) | stat: -rw-r--r-- 7,290 bytes parent folder | download | duplicates (3)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*---------------------------------------------------------------------
 * All portions of code are copyright by their respective author/s.
 * Copyright (C) 1996-2001 Vuthichai Ampornaramveth <vuthi@venus.dti.ne.jp>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *---------------------------------------------------------------------*/
/* Thai word-separator by dictionary */
/* By Vuthichai A.                   */

#define                 DICTFILE               "tdict.txt"
#define                 MAXWORD                 50000
#define                 MAXWORDLENGTH           30
#define                 MAXLINELENGTH           400
#define                 MAXSTATE                100000

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int levtable[]={
                0,2,0,0,2,2,2,2,1,1,1,0,0,0,0,0,
                0,0,0,0,0,0,0,2,3,3,3,3,3,2,3,0,
                0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0 };

int map[MAXSTATE][96];
int state[MAXSTATE];
int maxstate, rnumword;
int mincol, maxcol;

void readfile(unsigned char *);
void fixline(unsigned char *);
void dooneline(unsigned char *,unsigned char *);
int findword(unsigned char *);
void add2map(unsigned char *,int);
void initmap();
void prmap();

unsigned char *wordptr[MAXWORD];
int numword;                    /* Number of words in memory */

main()
{
  FILE *fp, *fopen();
  unsigned char inputfile[80];
  unsigned char str[MAXLINELENGTH], out[MAXLINELENGTH];
  int i, count, nn;
  
  numword = nn = 0;

  /* Sort while reading */
  readfile(DICTFILE);

  initmap();
  for(i=0;i<numword;i++) {
    /* Remove duplicate words */
    if(i<numword-1) {
      if(strcmp(wordptr[i]+1, wordptr[i+1]+1)!=0) {
	add2map(wordptr[i]+1,wordptr[i][0]);
	nn++;
      }
    }
    else {
      add2map(wordptr[i]+1,wordptr[i][0]);
      nn++;
    }
  }

  count=0;
  for(i=0;i<maxstate;i++)
    count+=state[i];
  printf("Word Count: %d %d\n", nn, count);
  printf("Min Max Code: %d %d\n", mincol, maxcol);
  rnumword=nn;
  prmap();
  return 0;
}

void readfile(unsigned char *fname)
{
  FILE *fp, *fopen();
  unsigned char str[MAXWORDLENGTH];
  unsigned char *ostr;
  int l, i;
  
  fp = fopen(fname,"r");

  while(!feof(fp)) {
    fgets(str,MAXWORDLENGTH-1,fp);
    if(!feof(fp)) {
      fixline(str);
      wordptr[numword] = (unsigned char *)malloc((l=strlen(str))+2);
      if(wordptr[numword]==NULL)
        printf("Memory Error\n");
      strcpy(wordptr[numword]+1, str);
      wordptr[numword][l]=0;            /* Remove new line */
      wordptr[numword][0] = l-1;

      if(numword > 0)
      if(strcmp(wordptr[numword]+1,wordptr[numword-1]+1)<0) 
      {
        ostr = wordptr[numword];
        i = numword;
        while(i && (strcmp(ostr+1,wordptr[i-1]+1)<0)) {
          wordptr[i] = wordptr[i-1];
          i--;
        }
        wordptr[i]=ostr;
      }
      numword++;
      if(numword % 2000 ==0)
        printf("%d\n",numword);
    }
  }
  fclose(fp);
  printf("Reading dictionary done.\n");
}

	
void fixline(line)
unsigned char *line;
{
  unsigned char top,up,middle,low;
  unsigned char out[MAXLINELENGTH];
  int i,j,c;

  i=j=0;
  strcpy(out,line);
  top=up=middle=low=0;
  while(c=out[i++]) {
    switch((c>0xD0)?levtable[c-0xD0]:0) {
    case 0 : 
      if(middle) {
	line[j++]=middle;
	if(low) line[j++]=low;
	if(up)  line[j++]=up;
	if(top) line[j++]=top;
      }
      top=up=middle=low=0;
      middle=c; break;
    case 1 : 
      low=c; break;
    case 2 : 
      up=c; break;
    case 3 : 
      top=c; break;
    }
  }
  if(middle) {
    line[j++]=middle;
    if(low) line[j++]=low;
    if(up)  line[j++]=up;
    if(top) line[j++]=top;
  }
  line[j]=0;
}

void initmap() 
{
  int i,j;

  for(i=0;i<MAXSTATE;i++) {
    state[i]=0;
    for(j=0;j<96;j++)
      map[i][j]=0;
  }
  maxstate = 1;
  mincol=255;
  maxcol=0;
}

void add2map(unsigned char *str,int len)
{
  int curstate,i,c,cc;

  curstate=i=0;
  while(c=str[i]) {
    cc=c;
    if(cc>maxcol) maxcol=cc;
    if(cc<mincol) mincol=cc;
    c-=160;
    if(c<0) {
      fprintf(stderr,"Fatal Error\n");
      exit(1);
    }
    if(map[curstate][c]>0) {
      curstate = map[curstate][c];
      /* printf("%c to %d\n", cc, curstate); */
    }
    else {
      map[curstate][c] = maxstate;
      curstate=maxstate;
      if(i==len-1) {
	state[maxstate] = 1;
	/* printf("%s at state %d\n", str, maxstate); */
      }
      maxstate++;
      if(maxstate >= MAXSTATE) {
	fprintf(stderr,"Not Enough No. of States\n");
	exit(1);
      }
    }
    i++;
  }

}

void prmap()
{
  FILE *FP;
  int i,j,c;
  int state_min[MAXSTATE];
  int state_max[MAXSTATE];
  int state_offset[MAXSTATE];
  int offset,min,max;

  c=maxcol-mincol+1;
  offset=0;

  printf("Writing Map File...\n");

  FP=fopen("map.c","w");
  fprintf(FP,"unsigned short map[] = {\n");
  for(i=0;i<maxstate;i++) {
    min=max=0;
    for(j=mincol;j<=maxcol;j++) {
      if(map[i][j-160]) {
	if(!min)
	  min=j;
	max=j;
      }
    }
    state_min[i]=min;
    state_max[i]=max;
    state_offset[i]=offset;
    for(j=min;j<=max;j++) {
      fprintf(FP,"%d",map[i][j-160]);
      offset++;
      if(i<maxstate-1 || j<max) fprintf(FP,",");
      if(offset%16==15) fprintf(FP,"\n");
    }
  }
  fprintf(FP,"};\n");

  fprintf(FP,"unsigned char state[%d] = {\n", maxstate);
  for(i=0;i<maxstate;i++) {
    fprintf(FP,"%d",state[i]);
    if(i<maxstate-1) fprintf(FP,",");
    if(i%16==15) fprintf(FP,"\n");
  }
  fprintf(FP,"};\n");

  fprintf(FP,"unsigned char state_min[%d] = {\n", maxstate);
  for(i=0;i<maxstate;i++) {
    fprintf(FP,"%d",state_min[i]);
    if(i<maxstate-1) fprintf(FP,",");
    if(i%16==15) fprintf(FP,"\n");
  }
  fprintf(FP,"};\n");

  fprintf(FP,"unsigned char state_max[%d] = {\n", maxstate);
  for(i=0;i<maxstate;i++) {
    fprintf(FP,"%d",state_max[i]);
    if(i<maxstate-1) fprintf(FP,",");
    if(i%16==15) fprintf(FP,"\n");
  }
  fprintf(FP,"};\n");

  fprintf(FP,"int state_offset[%d] = {\n", maxstate);
  for(i=0;i<maxstate;i++) {
    fprintf(FP,"%d",state_offset[i]);
    if(i<maxstate-1) fprintf(FP,",");
    if(i%16==15) fprintf(FP,"\n");
  }
  fprintf(FP,"};\n");
  fclose(FP);
    
  FP=fopen("map.h","w");
  fprintf(FP,"int numword = %d;\n", rnumword);
  fprintf(FP,"int mincol = %d;\n", mincol);
  fprintf(FP,"int maxcol = %d;\n", maxcol);
  fprintf(FP,"int maxstate = %d;\n", maxstate);
  fprintf(FP,"extern unsigned short map[%d];\n", offset);
  fprintf(FP,"extern unsigned char state[%d];\n", maxstate);
  fprintf(FP,"extern unsigned char state_min[%d];\n", maxstate);
  fprintf(FP,"extern unsigned char state_max[%d];\n", maxstate);
  fprintf(FP,"extern int state_offset[%d];\n", maxstate);
  fclose(FP);

}