summaryrefslogtreecommitdiff
path: root/src-worddic/wtype.c
blob: df29de3487162581a57c36ad4ec7ba7abb8773b6 (plain)
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
/*
 * 品詞型を管理する
 * 中身はwtype_tの内部のレイアウトに強く依存する。
 *
 * Copyright (C) 2000-2007 TABATA Yusuke
 */
/*
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library 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
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 */
#include <stdio.h>
#include <string.h>

#include <anthy/wtype.h>
#include "dic_main.h"

wtype_t anthy_wt_none, anthy_wt_all;

struct wttable {
  const char *name;
  int pos;
  int cos;
  int scos;
  int cc;
  int ct;/*カ変など*/
  int flags;
};

/* 品詞の日本語の名前を品詞に変換するテーブル */
static struct wttable pos_name_tab[]= {
#include "ptab.h"
};

/* 辞書中の品詞の名前を品詞に変換するテーブル */
static struct wttable wt_name_tab[]= {
#include "wtab.h"
};

static struct wttable *
find_wttab(struct wttable *array, const char *name)
{
  struct wttable *w;
  for (w = array; w->name; w++) {
    if (!strcmp(w->name, name)) {
      return w;
    }
  }
  return NULL;
}

void
anthy_init_wtypes(void)
{
  anthy_wt_all.pos = POS_NONE;
  anthy_wt_all.cc = CC_NONE;
  anthy_wt_all.ct = CT_NONE;
  anthy_wt_all.cos = COS_NONE;
  anthy_wt_all.scos = SCOS_NONE;
  anthy_wt_all.wf = WF_NONE;

  anthy_wt_none = anthy_wt_all;
  anthy_wt_none.pos = POS_INVAL;
}

/*
 * 返り値には品詞の名前
 * tには品詞が返される
 */
const char *
anthy_type_to_wtype(const char *s, wtype_t *t)
{
  struct wttable *w;
  if (s[0] != '#') {
    *t = anthy_wt_none;
    return NULL;
  }
  w = find_wttab(wt_name_tab, s);
  if (!w) {
    *t = anthy_wt_all;
    return NULL;
  }
  *t = anthy_get_wtype(w->pos, w->cos, w->scos, w->cc, w->ct, w->flags);
  return w->name;
}

wtype_t
anthy_init_wtype_by_name(const char *name)
{
  struct wttable *p;
  p = find_wttab(pos_name_tab, name);

  if (p) {
    return anthy_get_wtype(p->pos, p->cos, p->scos, p->cc, p->ct, p->flags);
  }

  printf("Failed to find wtype(%s).\n", name);
  return anthy_wt_all;
}

/* 二つの品詞が完全に一致しているかどうか */
int
anthy_wtype_equal(wtype_t lhs, wtype_t rhs)
{
  if (lhs.pos == rhs.pos &&
      lhs.cos == rhs.cos &&
      lhs.scos == rhs.scos &&
      lhs.cc == rhs.cc &&
      lhs.ct == rhs.ct &&
      lhs.wf == rhs.wf) {
    return 1;
  } else {
    return 0;
  }
}


/* n は hs の一部かどうか? */
int
anthy_wtype_include(wtype_t hs, wtype_t n)
{
  /*printf("POS %d,%d\n", hs.type[WT_POS], n.type[WT_POS]);*/
  if (hs.pos != POS_NONE &&
      hs.pos != n.pos) {
    return 0;
  }
  if (hs.cc != CC_NONE &&
      hs.cc != n.cc) {
    return 0;
  }
  if (hs.ct != CT_NONE &&
      hs.ct != n.ct) {
    return 0;
  }
  if (hs.cos != COS_NONE &&
      hs.cos != n.cos) {
    return 0;
  }
  if (hs.scos != SCOS_NONE &&
      hs.scos != n.scos) {
    return 0;
  }
  return 1;
}

int
anthy_wtype_get_cc(wtype_t t)
{
  return t.cc;
}

int
anthy_wtype_get_ct(wtype_t t)
{
  return t.ct;
}

int
anthy_wtype_get_pos(wtype_t t)
{
  return t.pos;
}

int
anthy_wtype_get_cos(wtype_t t)
{
  return t.cos;
}

int
anthy_wtype_get_scos(wtype_t t)
{
  return t.scos;
}

int
anthy_wtype_get_wf(wtype_t t)
{
  return t.wf;
}

int
anthy_wtype_get_indep(wtype_t t)
{
  return t.wf & WF_INDEP;
}

int
anthy_wtype_get_meisi(wtype_t w)
{
  return w.wf & WF_MEISI;
}

int
anthy_wtype_get_sv(wtype_t w)
{
  return w.wf & WF_SV;
}

int
anthy_wtype_get_ajv(wtype_t w)
{
  return w.wf & WF_AJV;
}

void
anthy_wtype_set_cc(wtype_t *w, int cc)
{
  w->cc = cc;
}

void
anthy_wtype_set_ct(wtype_t *w, int ct)
{
  w->ct = ct;
}

void
anthy_wtype_set_pos(wtype_t *w, int pos)
{
  w->pos = pos;
}

void
anthy_wtype_set_cos(wtype_t *w, int cs)
{
  w->cos = cs;
}

void
anthy_wtype_set_scos(wtype_t *w, int sc)
{
  w->scos = sc;
}

void
anthy_wtype_set_dep(wtype_t *w, int isDep)
{
  if (isDep) {
    w->wf &= (~WF_INDEP);
  }else{
    w->wf |= WF_INDEP;
  }
}

void
anthy_print_wtype(wtype_t w)
{
  printf("(POS=%d,COS=%d,SCOS=%d,CC=%d,CT=%d,flags=%d)\n",
	 anthy_wtype_get_pos(w),
	 anthy_wtype_get_cos(w),
	 anthy_wtype_get_scos(w),
	 anthy_wtype_get_cc(w),
	 anthy_wtype_get_ct(w),
	 anthy_wtype_get_wf(w));
}

wtype_t
anthy_get_wtype_with_ct(wtype_t base, int ct)
{
  wtype_t w;

  w = base;
  w.ct = ct;

  return w;
}

wtype_t
anthy_get_wtype(int pos, int cos, int scos, int cc, int ct, int wf)
{
  wtype_t w;

  w.pos = pos;
  w.cos = cos;
  w.scos = scos;
  w.cc = cc;
  w.ct = ct;
  w.wf = wf;

  return w;
}