File: use_dic.c

package info (click to toggle)
anthy 6300d-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,400 kB
  • ctags: 2,270
  • sloc: ansic: 17,009; sh: 13,554; lisp: 1,039; makefile: 252; ruby: 212; perl: 10
file content (292 lines) | stat: -rw-r--r-- 5,800 bytes parent folder | download
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
/*
 * 㼭򰷤 
 * Copyright (C) 2003 TABATA Yusuke
 */
#include <string.h>
#include <stdlib.h>

#include <dic.h>
#include <xstr.h>
#include <alloc.h>
#include <record.h>
#include <hash_map.h>
#include "dic_main.h"
#include "dic_ent.h"
#include "file_dic.h"

#define MAX_RELATION_COUNT 50

static allocator relation_ator;
extern struct mem_dic *anthy_current_personal_dic_cache;

/** ñ֤δط */
struct relation {
  int from;
  int target;
  int used;
  struct relation *next;
};

static struct {
  struct relation *head;
} relation_list;

int anthy_file_dic_check_word_relation(struct file_dic *fdic,
				       int from, int to)
{
  struct relation *rel;
  int i;
  int res;
  (void)fdic;
  /* Ŀͼ */
  for (rel = relation_list.head; rel; rel = rel->next) {
    if (rel->from == from) {
      rel->used = 1;
      return 1;
    }
  }
  /* ͭ */
  res = anthy_check_word_relation(from, to);
  if (!res) {
    return 0;
  }
  for (i = 0; i < fdic->nr_ucs; i++) {
    if (anthy_dic_ntohl(fdic->ucs[i * 2]) == from) {
      return 1;
    }
  }
  return 0;
}

static int
check_duplicated_relation(int from, int target)
{
  struct relation *rel;
  for (rel = relation_list.head; rel; rel = rel->next) {
    if (rel->from == from && rel->target == target) {
      return 1;
    }
  }
  return 0;
}

/** ñδطϿ */
void
anthy_dic_register_relation(int from, int target)
{
  struct relation *rel;
  if (check_duplicated_relation(from, target)) {
    return ;
  }
  rel = anthy_smalloc(relation_ator);
  rel->from = from;
  rel->target = target;
  rel->used = 0;
  rel->next = relation_list.head;
  relation_list.head = rel;
}

static xstr *
word_id_to_xstr(int id)
{
  /*idγɽϡñ*ʻ*ɤߡ
   **/
  struct dic_ent *de =
    anthy_mem_dic_word_id_to_dic_ent(anthy_current_personal_dic_cache, id);
  struct seq_ent *se;
  xstr *res, *wt;
  if (!de) {
    return 0;
  }
  wt = anthy_cstr_to_xstr(de->wt_name, 0);
  se = de->se;
  /* de->str * de->type * se->str */
  res = anthy_xstr_dup(&de->str);
  res = anthy_xstrappend(res, '*');
  res = anthy_xstrcat(res, wt);
  res = anthy_xstrappend(res, '*');
  res = anthy_xstrcat(res, &se->str);
  anthy_free_xstr(wt);
  return res;
}

static int
find_aster(xstr *xs, int start)
{
  int i;
  for (i = start; i < xs->len; i++) {
    if (xs->str[i] == '*') {
      return i;
    }
  }
  return -1;
}

static int
do_xstr_to_word_id(xstr *word, char *wt_name, xstr *yomi)
{
  struct seq_ent *se;
  int i;
  se = anthy_get_seq_ent_from_xstr(yomi);
  if (!se) {
    return 0;
  }
  for (i = 0; i < se->nr_dic_ents; i++) {
    struct dic_ent *de = se->dic_ents[i];
    if (!strcmp(wt_name, de->wt_name) &&
	!anthy_xstrcmp(word, &de->str)) {
      return de->id;
    }
  }
  return 0;
}

static int
xstr_to_word_id(xstr *xs)
{
  xstr word_xs, wt_xs, yomi_xs;
  char *wt_name;
  int idx;
  int word_id;
  /* ֤ޤñ*ʻ*ɤߡפڤФ */
  /* ñڤФ */
  word_xs.str = xs->str;
  idx = find_aster(xs, 0);
  if (idx == -1) {
    return 0;
  }
  word_xs.len = idx;
  /* ʻ̾ڤФ */
  wt_xs.str = &xs->str[idx + 1];
  idx = find_aster(xs, idx + 1);
  if (idx == -1) {
    return 0;
  }
  wt_xs.len = idx - word_xs.len - 1;
  /* ɤߤڤФ */
  yomi_xs.str = &xs->str[idx + 1];
  yomi_xs.len = xs->len - idx - 1;
  /**/
  wt_name = anthy_xstr_to_cstr(&wt_xs, 0);
  if (wt_name) {
    word_id = do_xstr_to_word_id(&word_xs, wt_name, &yomi_xs);
    free(wt_name);
    return word_id;
  }
  return 0;
}

static void
save_to_record(void)
{
  struct relation *rel;
  /* Υǡä */
  if (anthy_select_section("WORD_RELATION", 1)) {
    return;
  }
  anthy_release_section();
  /**/
  if (anthy_select_section("WORD_RELATION", 1)) {
    return;
  }
  for (rel = relation_list.head; rel; rel = rel->next) {
    /**/
    xstr *from_xs, *target_xs;
    from_xs = word_id_to_xstr(rel->from);
    if (!from_xs) {
      continue;
    }
    target_xs = word_id_to_xstr(rel->target);
    if (!target_xs) {
      anthy_free_xstr(from_xs);
      continue;
    }
    if (!anthy_select_column(from_xs, 1)) {
      int nr = anthy_get_nr_values();
      anthy_set_nth_xstr(nr, target_xs);
    }
    anthy_free_xstr(from_xs);
    anthy_free_xstr(target_xs);
  }
}

/** Ԥ줿Τǡ֤ */
void
anthy_dic_commit_relation(void)
{
  int count = 0;
  struct relation *rel, *cur;
  for (rel = relation_list.head; rel->next;) {
    count ++;
    if (count < MAX_RELATION_COUNT) {
      /* ǽMAX_RELATION_COUNTޤǤϤΤޤޤˤ */
      rel = rel->next;
      continue;
    }
    cur = rel->next;
    rel->next = cur->next;
    if (cur->used) {
      /* Ƭ˻äƤ */
      cur->used = 0;
      cur->next = relation_list.head;
      relation_list.head = cur;
    } else {
      /* ä */
      anthy_sfree(relation_ator, cur);
    }
  }
  save_to_record();
}

static void
free_allocator(void)
{
  anthy_free_allocator(relation_ator);
  relation_ator = 0;
}

static void
init_list(void)
{
  relation_list.head = 0;
  if (relation_ator) {
    free_allocator();
  }
  relation_ator = anthy_create_allocator(sizeof(struct relation), 0);
}

void
anthy_dic_reload_use_dic(void)
{
  init_list();
  if (anthy_select_section("WORD_RELATION", 0)) {
    return;
  }
  if (anthy_select_first_column()) {
    return ;
  }
  do {
    int nr = anthy_get_nr_values();
    xstr *to_xs, *from_xs = anthy_get_index_xstr();
    int to_id, from_id;
    int i;
    from_id = xstr_to_word_id(from_xs);
    for (i = 0; i < nr; i++) {
      to_xs = anthy_get_nth_xstr(i);
      to_id = xstr_to_word_id(to_xs);
      anthy_dic_register_relation(from_id, to_id);
    }
  } while (!anthy_select_next_column());
}

void
anthy_init_use_dic(void)
{
  init_list();
}

void
anthy_quit_use_dic(void)
{
  free_allocator();
}