File: evalborder.c

package info (click to toggle)
anthy 1%3A0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 23,596 kB
  • sloc: ansic: 24,444; sh: 4,186; lisp: 1,265; makefile: 238
file content (187 lines) | stat: -rw-r--r-- 4,613 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
/*
 * 文節の境界を検出する。
 *
 * metawordの選択にはビタビアルゴリズムを使う
 *
 * anthy_eval_border() で指定された領域を文節に分割する
 *
 * Funded by IPA未踏ソフトウェア創造事業 2001 10/29
 * Copyright (C) 2000-2003 TABATA Yusuke, UGAWA Tomoharu
 */
#include <stdio.h>
#include <stdlib.h>

#include <anthy/alloc.h>
#include <anthy/splitter.h>
#include "wordborder.h"

static int
border_check(struct meta_word* mw,
	     int from,
	     int border)
{
  if (mw->from < border) {
    /* 先頭の文節の中から始まるmwは文節区切りにぴったりあっていないとダメ */
    if (mw->from == from && mw->from + mw->len == border) {
      return 1;
    } else {
      return 0;
    }
  } else {
    /* 後ろの文節は無条件に使用可能 */
    return 1;
  }
}

/*
 * 再帰的にmetawordが使用可能かチェックする
 */
static void
metaword_constraint_check(struct splitter_context *sc,
			  struct meta_word *mw,
			  int from, 
			  int border)
{
  if (!mw) return;
  if (mw->can_use != unchecked) return;

  switch(anthy_metaword_type_tab[mw->type].check){
  case MW_CHECK_SINGLE:
    mw->can_use = border_check(mw, from, border) ? ok : ng;
    break;
  case MW_CHECK_BORDER:
    {
      struct meta_word* mw1 = mw->mw1;
      struct meta_word* mw2 = mw->mw2;

      if (mw1&&mw2&&mw1->from + mw1->len == border) {
	/* ちょうど境目にマークが入ってる */
	mw->can_use = ng;
	break;
      }
      if (mw1)
	metaword_constraint_check(sc, mw1, from, border);
      if (mw2)
	metaword_constraint_check(sc, mw2, mw2->from, border);
      
      if ((!mw1 || mw1->can_use == ok) && (!mw2 || mw2->can_use == ok)) {
	mw->can_use = ok;
      } else {
	mw->can_use = ng;
      }
    }
    break;
  case MW_CHECK_WRAP:
    metaword_constraint_check(sc, mw->mw1, from, border);
    mw->can_use = mw->mw1->can_use;
    break;
  case MW_CHECK_NUMBER:
    {
      struct meta_word* itr = mw;
      mw->can_use = ok;
      
      /* 個々の文節の一つでも文節区切りをまたがっていれば、この複合語は使えない */
      for (; itr && itr->type == MW_NUMBER; itr = itr->mw2) {
	struct meta_word* mw1 = itr->mw1;
	if (!border_check(mw1, from, border)) {
	  mw->can_use = ng;
	  break;
	}
      }
    }
    break;
  case MW_CHECK_COMPOUND:
    {
      struct meta_word* itr = mw;
      mw->can_use = ok;
      
      /* 個々の文節の一つでも文節区切りをまたがっていれば、この複合語は使えない */
      for (; itr && (itr->type == MW_COMPOUND_HEAD || itr->type == MW_COMPOUND); itr = itr->mw2) {
	struct meta_word* mw1 = itr->mw1;
	if (!border_check(mw1, from, border)) {
	  mw->can_use = ng;
	  break;
	}
      }
    }
    break;
  case MW_CHECK_OCHAIRE:
    {
      struct meta_word* mw1;
      if (border_check(mw, from, border)) {
	for (mw1 = mw; mw1; mw1 = mw1->mw1) {
	  mw1->can_use = ok;
	}
      } else {
	for (mw1 = mw; mw1; mw1 = mw1->mw1) {
	  mw1->can_use = ng;
	}	
      }
    }
    break;
  case MW_CHECK_NONE:
    break;
  default:
    printf("try to check unknown type of metaword (%d).\n", mw->type);
  }
}

/*
 * 全てのmetawordについて使用できるかどうかをチェックする
 */
static void
metaword_constraint_check_all(struct splitter_context *sc,
			      int from, int to,
			      int border)
{
  int i;
  struct word_split_info_cache *info;
  info = sc->word_split_info;

  /* まずuncheckedにする */
  for (i = from; i < to; i ++) {
    struct meta_word *mw;
    for (mw = info->cnode[i].mw;
	 mw; mw = mw->next) {
      mw->can_use = unchecked;
    }
  }

  /* 次に合成されたmetawordについてチェック */
  for (i = from; i < to; i ++) {
    struct meta_word *mw;
    for (mw = info->cnode[i].mw; mw; mw = mw->next) {
      metaword_constraint_check(sc, mw, from, border);
    }
  }
}

/*
 * ここから文節境界をマークする
 */
void
anthy_eval_border(struct splitter_context *sc, int from, int from2, int to)
{
  struct meta_word *mw;
  int nr;

  /* 文節候補のうち使えるもののみ選択 */
  metaword_constraint_check_all(sc, from, to, from2);

  /* fromとfrom2の間をカバーするmeta_wordがあるかどうかを探す。
   * あれば、fromから解析を行い、なければfrom2から解析をする。
   */
  nr = 0;
  for (mw = sc->word_split_info->cnode[from].mw; mw; mw = mw->next) {
    if (mw->can_use == ok) {
      nr ++;
      break;
    }
  }
  if (nr == 0) {
    from = from2;
  }

  /* 文節の境界を設定する */
  anthy_mark_borders(sc, from, to);
}