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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/*
* ʸμΩ(Ƭޤ)³
* 졢ưʤɤ°Υѥɤ롣
* ѥϥդȤեѰդ롣
*
*
* +------+
* | |
* |branch+--cond--+--transition--> node
* | | +--transition--> node
* | NODE |
* | |
* |branch+--cond-----transition--> node
* | |
* |branch+--cond-----transition--> node
* | |
* +------+
*
* Copyright (C) 2000-2007 TABATA Yusuke
* Copyright (C) 2006 YOSHIDA Yuichi
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include <anthy/anthy.h>
#include <anthy/conf.h>
#include <anthy/ruleparser.h>
#include <anthy/xstr.h>
#include <anthy/filemap.h>
#include <anthy/logger.h>
#include <anthy/segclass.h>
#include <anthy/splitter.h>
#include <anthy/wtype.h>
#include <anthy/diclib.h>
#include "wordborder.h"
/* ܥ */
static struct dep_dic ddic;
static void
match_branch(struct splitter_context *sc,
struct word_list *tmpl,
xstr *xs, struct dep_branch *db);
static void
match_nodes(struct splitter_context *sc,
struct word_list *wl,
xstr follow_str, int node);
static int
anthy_xstrcmp_with_ondisk(xstr *xs,
ondisk_xstr *dxs)
{
int *d = (int *)dxs;
int len = anthy_dic_ntohl(d[0]);
int i;
xchar c;
if (len != xs->len) {
return 1;
}
d++;
for (i = 0; i < len; i++) {
c = anthy_dic_ntohl(d[i]);
if (xs->str[i] != c) {
return 1;
}
}
return 0;
}
static ondisk_xstr *
anthy_next_ondisk_xstr(ondisk_xstr *dxs)
{
int *d = (int *)dxs;
int len = anthy_dic_ntohl(d[0]);
return &d[len+1];
}
static int
anthy_ondisk_xstr_len(ondisk_xstr *dxs)
{
int *d = (int *)dxs;
return anthy_dic_ntohl(d[0]);
}
/*
* ƥΡɤˤܾƥȤ
*
* wl Ωword_list
* follow_str Ωʹߤʸ
* node 롼ֹ
*/
static void
match_nodes(struct splitter_context *sc,
struct word_list *wl,
xstr follow_str, int node)
{
struct dep_node *dn = &ddic.nodes[node];
struct dep_branch *db;
int i,j;
/* ƥ롼 */
for (i = 0; i < dn->nr_branch; i++) {
ondisk_xstr *dep_xs;
db = &dn->branch[i];
dep_xs = db->xstrs;
/* ܾ */
for (j = 0; j < db->nr_strs;
j++, dep_xs = anthy_next_ondisk_xstr(dep_xs)) {
xstr cond_xs;
/* °ܾĹȤɬ */
if (follow_str.len < anthy_ondisk_xstr_len(dep_xs)) {
continue;
}
/* ܾʬڤФ */
cond_xs.str = follow_str.str;
cond_xs.len = anthy_ondisk_xstr_len(dep_xs);
/* ܾӤ */
if (!anthy_xstrcmp_with_ondisk(&cond_xs, dep_xs)) {
/* ܾmatch */
struct word_list new_wl = *wl;
struct part_info *part = &new_wl.part[PART_DEPWORD];
xstr new_follow;
part->len += cond_xs.len;
new_follow.str = &follow_str.str[cond_xs.len];
new_follow.len = follow_str.len - cond_xs.len;
/* ܤƤߤ */
match_branch(sc, &new_wl, &new_follow, db);
}
}
}
}
/*
* ܤ¹ԤƤߤ
*
* tmpl ޤǤ˹word_list
* xs Ĥʸ
* db Ĵbranch
*/
static void
match_branch(struct splitter_context *sc,
struct word_list *tmpl,
xstr *xs, struct dep_branch *db)
{
struct part_info *part = &tmpl->part[PART_DEPWORD];
int i;
/* ˥ȥ饤 */
for (i = 0; i < db->nr_transitions; i++) {
/**/
int head_pos = tmpl->head_pos; /* ʻξ */
int features = tmpl->mw_features;
enum dep_class dc = part->dc;
/**/
struct dep_transition *transition = &db->transition[i];
tmpl->tail_ct = anthy_dic_ntohl(transition->ct);
/* ܤγѷʻ */
if (anthy_dic_ntohl(transition->dc) != DEP_NONE) {
part->dc = anthy_dic_ntohl(transition->dc);
}
/* ̾첽ưʻ̾ */
if (anthy_dic_ntohl(transition->head_pos) != POS_NONE) {
tmpl->head_pos = anthy_dic_ntohl(transition->head_pos);
}
if (transition->weak) {
tmpl->mw_features |= MW_FEATURE_WEAK_CONN;
}
/* ܤü */
if (anthy_dic_ntohl(transition->next_node)) {
/* */
match_nodes(sc, tmpl, *xs, anthy_dic_ntohl(transition->next_node));
} else {
struct word_list *wl;
/*
* üΡɤãΤǡ
* word_listȤƥߥå
*/
wl = anthy_alloc_word_list(sc);
*wl = *tmpl;
wl->len += part->len;
/**/
anthy_commit_word_list(sc, wl);
}
/* ᤷ */
part->dc = dc;
tmpl->head_pos = head_pos;
tmpl->mw_features = features;
}
}
/**
*/
void
anthy_scan_node(struct splitter_context *sc,
struct word_list *tmpl,
xstr *follow, int node)
{
/* °դƤʤ֤鸡Ϥ */
match_nodes(sc, tmpl, *follow, node);
}
static void
read_xstr(struct dep_dic* ddic, int* offset)
{
int len = anthy_dic_ntohl(*(int*)&ddic->file_ptr[*offset]);
*offset += sizeof(int);
*offset += sizeof(xchar) * len;
}
static void
read_branch(struct dep_dic* ddic, struct dep_branch* branch, int* offset)
{
int i;
/* ܾοɤ */
branch->nr_strs = anthy_dic_ntohl(*(int*)&ddic->file_ptr[*offset]);
*offset += sizeof(int);
/* ܾʸɤ */
branch->xstrs = (ondisk_xstr *)&ddic->file_ptr[*offset];
for (i = 0; i < branch->nr_strs; ++i) {
read_xstr(ddic, offset);
}
branch->nr_transitions = anthy_dic_ntohl(*(int*)&ddic->file_ptr[*offset]);
*offset += sizeof(int);
branch->transition = (struct dep_transition*)&ddic->file_ptr[*offset];
*offset += sizeof(struct dep_transition) * branch->nr_transitions;
}
static void
read_node(struct dep_dic* ddic, struct dep_node* node, int* offset)
{
int i;
node->nr_branch = anthy_dic_ntohl(*(int*)&ddic->file_ptr[*offset]);
*offset += sizeof(int);
node->branch = malloc(sizeof(struct dep_branch) * node->nr_branch);
for (i = 0; i < node->nr_branch; ++i) {
read_branch(ddic, &node->branch[i], offset);
}
}
static void
read_file(void)
{
int i;
int offset = 0;
ddic.file_ptr = anthy_file_dic_get_section("dep_dic");
/* ǽ˥롼ο */
ddic.nrRules = anthy_dic_ntohl(*(int*)&ddic.file_ptr[offset]);
offset += sizeof(int);
/* ƥ롼 */
ddic.rules = (struct ondisk_wordseq_rule*)&ddic.file_ptr[offset];
offset += sizeof(struct ondisk_wordseq_rule) * ddic.nrRules;
/* Ρɤο */
ddic.nrNodes = anthy_dic_ntohl(*(int*)&ddic.file_ptr[offset]);
offset += sizeof(int);
/* ƥΡɤɤ߹ */
ddic.nodes = malloc(sizeof(struct dep_node) * ddic.nrNodes);
for (i = 0; i < ddic.nrNodes; ++i) {
read_node(&ddic, &ddic.nodes[i], &offset);
}
}
int
anthy_get_nr_dep_rule()
{
return ddic.nrRules;
}
void
anthy_get_nth_dep_rule(int index, struct wordseq_rule *rule)
{
/* եξǡФ */
struct ondisk_wordseq_rule *r = &ddic.rules[index];
rule->wt = anthy_get_wtype(r->wt[0], r->wt[1], r->wt[2],
r->wt[3], r->wt[4], r->wt[5]);
rule->node_id = anthy_dic_ntohl(r->node_id);
}
int
anthy_init_depword_tab()
{
read_file();
return 0;
}
void
anthy_quit_depword_tab(void)
{
int i;
for (i = 0; i < ddic.nrNodes; i++) {
struct dep_node* node = &ddic.nodes[i];
free(node->branch);
}
free(ddic.nodes);
}
|