~ubuntu-branches/ubuntu/precise/hime/precise

« back to all changes in this revision

Viewing changes to src/hime-tsin2gtab-phrase.c

  • Committer: Package Import Robot
  • Author(s): Yao Wei (魏銘廷)
  • Date: 2012-01-14 00:24:08 UTC
  • Revision ID: package-import@ubuntu.com-20120114002408-e79gagbeg1rt8npv
Tags: upstream-0.9.9
Import upstream version 0.9.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2011 Edward Der-Hua Liu, Hsin-Chu, Taiwan
 
2
 *
 
3
 * This library is free software; you can redistribute it and/or
 
4
 * modify it under the terms of the GNU Lesser General Public
 
5
 * License as published by the Free Software Foundation; either
 
6
 * version 2.1 of the License, or (at your option) any later version.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
11
 * Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public
 
14
 * License along with this library; if not, write to the Free Software
 
15
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
16
 */
 
17
 
 
18
#include "hime.h"
 
19
#include "pho.h"
 
20
#include "gtab.h"
 
21
// #include "gtab-phrase-db.h"
 
22
#include "tsin.h"
 
23
#include "lang.h"
 
24
 
 
25
#define MAX_K (500000)
 
26
 
 
27
ITEM it[MAX_K];
 
28
ITEM64 it64[MAX_K];
 
29
gboolean key64;
 
30
int itN;
 
31
 
 
32
int qcmp_ch(const void *aa, const void *bb)
 
33
{
 
34
  return memcmp(((ITEM *)aa)->ch, ((ITEM *)bb)->ch, CH_SZ);
 
35
}
 
36
 
 
37
int qcmp_ch64(const void *aa, const void *bb)
 
38
{
 
39
  return memcmp(((ITEM64 *)aa)->ch, ((ITEM64 *)bb)->ch, CH_SZ);
 
40
}
 
41
 
 
42
ITEM *find_ch(char *s, int *N)
 
43
{
 
44
  ITEM t;
 
45
 
 
46
  bzero(t.ch, CH_SZ);
 
47
  u8cpy((char *)t.ch, s);
 
48
 
 
49
  ITEM *p = (ITEM *)bsearch(&t, it, itN, sizeof(ITEM), qcmp_ch);
 
50
  if (!p)
 
51
    return NULL;
 
52
 
 
53
  ITEM *q = p+1;
 
54
 
 
55
  while (p > it && !qcmp_ch(p-1, &t))
 
56
    p--;
 
57
 
 
58
  ITEM *end = it + itN;
 
59
  while (q < end && !qcmp_ch(q, &t))
 
60
    q++;
 
61
 
 
62
  *N = q - p;
 
63
  if (*N > 20)
 
64
    p_err("err");
 
65
 
 
66
  return p;
 
67
}
 
68
 
 
69
ITEM64 *find_ch64(char *s, int *N)
 
70
{
 
71
  ITEM64 t;
 
72
 
 
73
  bzero(t.ch, CH_SZ);
 
74
  u8cpy((char *)t.ch, s);
 
75
 
 
76
  ITEM64 *p = (ITEM64 *)bsearch(&t, it64, itN, sizeof(ITEM64), qcmp_ch64);
 
77
  if (!p)
 
78
    return NULL;
 
79
 
 
80
  ITEM64 *q = p+1;
 
81
 
 
82
  while (p > it64 && !qcmp_ch64(p-1, &t))
 
83
    p--;
 
84
 
 
85
  ITEM64 *end = it64 + itN;
 
86
  while (q < end && !qcmp_ch64(q, &t))
 
87
    q++;
 
88
 
 
89
  *N = q - p;
 
90
  if (*N > 20)
 
91
    p_err("err");
 
92
 
 
93
  return p;
 
94
}
 
95
 
 
96
typedef struct {
 
97
  ITEM *arr;
 
98
  int N;
 
99
} KKARR;
 
100
 
 
101
typedef struct {
 
102
  ITEM64 *arr;
 
103
  int N;
 
104
} KKARR64;
 
105
 
 
106
 
 
107
void get_keymap_str(u_int64_t k, char *keymap, int keybits, char tkey[]);
 
108
 
 
109
int main(int argc, char **argv)
 
110
{
 
111
  gtk_init(&argc, &argv);
 
112
 
 
113
#if 1
 
114
  if (argc != 3)
 
115
    p_err("%s a_file.gtab outfile", argv[0]);
 
116
#endif
 
117
#if 1
 
118
  char *infile = argv[1];
 
119
  char *outfile = argv[2];
 
120
#else
 
121
  char *infile = "data/ar30.gtab";
 
122
  char *outfile = "l";
 
123
#endif
 
124
 
 
125
  FILE *fr;
 
126
  if ((fr=fopen(infile, "rb"))==NULL)
 
127
      p_err("cannot err open %s", infile);
 
128
 
 
129
  FILE *fp_out;
 
130
  if ((fp_out=fopen(outfile,"w"))==NULL) {
 
131
    printf("Cannot open %s", outfile);
 
132
    exit(-1);
 
133
  }
 
134
 
 
135
  struct TableHead th;
 
136
  fread(&th,1, sizeof(th), fr);
 
137
#if NEED_SWAP
 
138
  swap_byte_4(&th.version);
 
139
  swap_byte_4(&th.flag);
 
140
  swap_byte_4(&th.space_style);
 
141
  swap_byte_4(&th.KeyS);
 
142
  swap_byte_4(&th.MaxPress);
 
143
  swap_byte_4(&th.M_DUP_SEL);
 
144
  swap_byte_4(&th.DefC);
 
145
  for(i=0; i <= KeyNum; i++)
 
146
    swap_byte_4(&idx1[i]);
 
147
#endif
 
148
  int KeyNum = th.KeyS;
 
149
  dbg("keys %d\n",KeyNum);
 
150
 
 
151
  if (!th.keybits)
 
152
    th.keybits = 6;
 
153
  dbg("keybits:%d  maxPress:%d\n", th.keybits, th.MaxPress);
 
154
 
 
155
//  int max_keyN;
 
156
  if (th.MaxPress*th.keybits > 32) {
 
157
//    max_keyN = 64 / th.keybits;
 
158
    key64 = TRUE;
 
159
    dbg("it's a 64-bit .gtab\n");
 
160
  } else {
 
161
//    max_keyN = 32 / th.keybits;
 
162
    key64 = FALSE;
 
163
  }
 
164
 
 
165
  dbg("key64:%d\n", key64);
 
166
 
 
167
  char kname[128][CH_SZ];
 
168
  char keymap[128];
 
169
  gtab_idx1_t idx1[256];
 
170
//  static char kno[128];
 
171
 
 
172
  itN = th.DefC;
 
173
 
 
174
  bzero(keymap, sizeof(keymap));
 
175
  fread(keymap, 1, th.KeyS, fr);
 
176
  fread(kname, CH_SZ, th.KeyS, fr);
 
177
  fread(idx1, sizeof(gtab_idx1_t), KeyNum+1, fr);
 
178
 
 
179
#if 0
 
180
  int i;
 
181
  for(i=0; i < th.KeyS; i++) {
 
182
    kno[keymap[i]] = i;
 
183
  }
 
184
#endif
 
185
 
 
186
  fprintf(fp_out,TSIN_GTAB_KEY" %d %d %s\n", th.keybits, th.MaxPress, keymap+1);
 
187
 
 
188
  if (key64) {
 
189
    fread(it64, sizeof(ITEM64), th.DefC, fr);
 
190
    qsort(it64, th.DefC, sizeof(ITEM64), qcmp_ch64);
 
191
  }
 
192
  else {
 
193
    fread(it, sizeof(ITEM), th.DefC, fr);
 
194
    qsort(it, th.DefC, sizeof(ITEM), qcmp_ch);
 
195
  }
 
196
 
 
197
  itN = th.DefC;
 
198
 
 
199
//  dbg("itN:%d\n", itN);
 
200
#if 0
 
201
  for(i=0; i < itN; i++) {
 
202
    printf("\n%d ", i);
 
203
    utf8_putchar(it64[i].ch);
 
204
  }
 
205
#endif
 
206
 
 
207
  fclose(fr);
 
208
 
 
209
  char fname[128];
 
210
  get_hime_user_fname(tsin32_f, fname);
 
211
 
 
212
  FILE *fp;
 
213
  if ((fp=fopen(fname,"rb"))==NULL) {
 
214
    printf("Cannot open %s", fname);
 
215
    exit(-1);
 
216
  }
 
217
 
 
218
 
 
219
  while (!feof(fp)) {
 
220
    int i;
 
221
    phokey_t phbuf[MAX_PHRASE_LEN];
 
222
    u_char clen;
 
223
    usecount_t usecount;
 
224
 
 
225
    fread(&clen,1,1,fp);
 
226
    fread(&usecount, sizeof(usecount_t), 1,fp);
 
227
    fread(phbuf,sizeof(phokey_t), clen, fp);
 
228
 
 
229
    char str[MAX_PHRASE_LEN * CH_SZ + 1];
 
230
    int strN = 0;
 
231
    KKARR kk[MAX_PHRASE_LEN];
 
232
    KKARR64 kk64[MAX_PHRASE_LEN];
 
233
    gboolean has_err = FALSE;
 
234
 
 
235
    if (key64)
 
236
      bzero(kk64, sizeof(kk64));
 
237
    else
 
238
      bzero(kk, sizeof(kk));
 
239
 
 
240
//    dbg("clen %d\n", clen);
 
241
    for(i=0;i<clen;i++) {
 
242
      char ch[CH_SZ];
 
243
 
 
244
      int n = fread(ch, 1, 1, fp);
 
245
      if (n<=0)
 
246
        goto stop;
 
247
 
 
248
      int len=utf8_sz(ch);
 
249
 
 
250
      fread(&ch[1], 1, len-1, fp);
 
251
//      utf8_putchar(ch);
 
252
 
 
253
      if (key64) {
 
254
        if (!(kk64[i].arr = find_ch64(ch, &kk64[i].N)))
 
255
          has_err = TRUE;
 
256
      } else {
 
257
        if (!(kk[i].arr = find_ch(ch, &kk[i].N)))
 
258
          has_err = TRUE;
 
259
      }
 
260
 
 
261
      memcpy(str+strN, ch, len);
 
262
      strN+=len;
 
263
    }
 
264
 
 
265
    if (has_err) {
 
266
//      dbg("has_error\n");
 
267
      continue;
 
268
    }
 
269
#if 0
 
270
    for(i=0; i < clen; i++)
 
271
      printf("%d ", kk64[i].N);
 
272
    printf("\n");
 
273
#endif
 
274
    str[strN]=0;
 
275
 
 
276
    int permN;
 
277
    if (key64) {
 
278
      permN=kk64[0].N;
 
279
      for(i=1;i<clen;i++)
 
280
        permN *= kk64[i].N;
 
281
    }
 
282
    else {
 
283
      permN=kk[0].N;
 
284
      for(i=1;i<clen;i++)
 
285
        permN *= kk[i].N;
 
286
    }
 
287
 
 
288
    int z;
 
289
    for(z=0; z < permN; z++) {
 
290
      char vz[MAX_PHRASE_LEN];
 
291
 
 
292
      int tz = z;
 
293
 
 
294
      if (key64) {
 
295
        for(i=0; i < clen; i++) {
 
296
          vz[i] = tz % kk64[i].N;
 
297
          tz /= kk64[i].N;
 
298
        }
 
299
      } else {
 
300
        for(i=0; i < clen; i++) {
 
301
          vz[i] = tz % kk[i].N;
 
302
          tz /= kk[i].N;
 
303
        }
 
304
      }
 
305
 
 
306
      char kstr[512];
 
307
      kstr[0]=0;
 
308
 
 
309
      for(i=0;i<clen;i++) {
 
310
         char tkey[16];
 
311
         u_int64_t k=0;
 
312
 
 
313
         if (key64) {
 
314
           memcpy(&k, kk64[i].arr[vz[i]].key, 8);
 
315
         } else {
 
316
           u_int t;
 
317
           memcpy(&t, kk[i].arr[vz[i]].key, 4);
 
318
           k = t;
 
319
         }
 
320
 
 
321
         get_keymap_str(k, keymap, th.keybits, tkey);
 
322
 
 
323
         strcat(kstr, tkey);
 
324
         strcat(kstr, " ");
 
325
      }
 
326
 
 
327
      fprintf(fp_out,"%s %s%d\n", str, kstr, usecount);
 
328
    }
 
329
  }
 
330
stop:
 
331
 
 
332
  fclose(fp);
 
333
  fclose(fp_out);
 
334
  return 0;
 
335
}