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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
/*
* Copyright (C) 2008-2009 SVOX AG, Baslerstr. 30, 8048 Zuerich, Switzerland
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file picokfst.c
*
* FST knowledge loading and access
*
* Copyright (C) 2008-2009 SVOX AG, Baslerstr. 30, 8048 Zuerich, Switzerland
* All rights reserved.
*
* History:
* - 2009-04-20 -- initial version
*
*/
#include "picoos.h"
#include "picodbg.h"
#include "picoknow.h"
#include "picokfst.h"
#ifdef __cplusplus
extern "C" {
#endif
#if 0
}
#endif
#define FileHdrSize 4 /* size of FST file header */
/* ************************************************************/
/* function to create specialized kb, */
/* to be used by picorsrc only */
/* ************************************************************/
/** object : FSTKnowledgeBase
* shortcut : kfst
* derived from : picoknow_KnowledgeBase
*/
typedef struct kfst_subobj * kfst_SubObj;
typedef struct kfst_subobj{
picoos_uint8 * fstStream; /* the byte stream base address */
picoos_int32 hdrLen; /* length of file header */
picoos_int32 transductionMode; /* transduction mode to be used for FST */
picoos_int32 nrClasses; /* nr of pair/transition classes in FST; class is in [1..nrClasses] */
picoos_int32 nrStates; /* nr of states in FST; state is in [1..nrState] */
picoos_int32 termClass; /* pair class of terminator symbol pair; probably obsolete */
picoos_int32 alphaHashTabSize; /* size of pair alphabet hash table */
picoos_int32 alphaHashTabPos; /* absolute address of the start of the pair alphabet */
picoos_int32 transTabEntrySize; /* size in bytes of each transition table entry */
picoos_int32 transTabPos; /* absolute address of the start of the transition table */
picoos_int32 inEpsStateTabPos; /* absolute address of the start of the input epsilon transition table */
picoos_int32 accStateTabPos; /* absolute address of the table of accepting states */
} kfst_subobj_t;
/* ************************************************************/
/* primitives for reading from byte stream */
/* ************************************************************/
/* Converts 'nrBytes' bytes starting at position '*pos' in byte stream 'stream' into unsigned number 'num'.
'*pos' is modified to the position right after the number */
static void FixedBytesToUnsignedNum (picoos_uint8 * stream, picoos_uint8 nrBytes, picoos_uint32 * pos, picoos_uint32 * num)
{
picoos_int32 i;
(*num) = 0;
for (i = 0; i < nrBytes; i++) {
(*num) = ((*num) << 8) + (picoos_uint32)stream[*pos];
(*pos)++;
}
}
/* Converts 'nrBytes' bytes starting at position '*pos' in byte stream 'stream' into signed number 'num'.
'*pos' is modified to the position right after the number */
static void FixedBytesToSignedNum (picoos_uint8 * stream, picoos_uint8 nrBytes, picoos_uint32 * pos, picoos_int32 * num)
{
picoos_int32 i;
picoos_uint32 val;
val = 0;
for (i = 0; i < nrBytes; i++) {
val = (val << 8) + (picoos_uint32)stream[*pos];
(*pos)++;
}
if (val % 2 == 1) {
/* negative number */
(*num) = -((picoos_int32)((val - 1) / 2)) - 1;
} else {
/* positive number */
(*num) = val / 2;
}
}
/* Converts varying-sized sequence of bytes starting at position '*pos' in byte stream 'stream'
into (signed) number 'num'. '*pos' is modified to the position right after the number. */
static void BytesToNum (picoos_uint8 * stream, picoos_uint32 * pos, picoos_int32 * num)
{
picoos_uint32 val;
picoos_uint32 b;
val = 0;
b = (picoos_uint32)stream[*pos];
(*pos)++;
while (b < 128) {
val = (val << 7) + b;
b = (picoos_uint32)stream[*pos];
(*pos)++;
}
val = (val << 7) + (b - 128);
if (val % 2 == 1) {
/* negative number */
(*num) = -((picoos_int32)((val - 1) / 2)) - 1;
} else {
/* positive number */
(*num) = val / 2;
}
}
/* ************************************************************/
/* setting up FST from byte stream */
/* ************************************************************/
static pico_status_t kfstInitialize(register picoknow_KnowledgeBase this,
picoos_Common common)
{
picoos_uint32 curpos;
picoos_int32 offs;
kfst_subobj_t * kfst;
PICODBG_DEBUG(("kfstInitialize -- start\n"));
if (NULL == this || NULL == this->subObj) {
return picoos_emRaiseException(common->em, PICO_EXC_KB_MISSING, NULL,
NULL);
}
kfst = (kfst_subobj_t *) this->subObj;
/* +CT+ */
kfst->fstStream = this->base;
PICODBG_TRACE(("base: %d\n",this->base));
kfst->hdrLen = FileHdrSize;
curpos = kfst->hdrLen;
BytesToNum(kfst->fstStream,& curpos,& kfst->transductionMode);
BytesToNum(kfst->fstStream,& curpos,& kfst->nrClasses);
BytesToNum(kfst->fstStream,& curpos,& kfst->nrStates);
BytesToNum(kfst->fstStream,& curpos,& kfst->termClass);
BytesToNum(kfst->fstStream,& curpos,& kfst->alphaHashTabSize);
BytesToNum(kfst->fstStream,& curpos,& offs);
kfst->alphaHashTabPos = kfst->hdrLen + offs;
BytesToNum(kfst->fstStream,& curpos,& kfst->transTabEntrySize);
BytesToNum(kfst->fstStream,& curpos,& offs);
kfst->transTabPos = kfst->hdrLen + offs;
BytesToNum(kfst->fstStream,& curpos,& offs);
kfst->inEpsStateTabPos = kfst->hdrLen + offs;
BytesToNum(kfst->fstStream,& curpos,& offs);
kfst->accStateTabPos = kfst->hdrLen + offs;
/* -CT- */
return PICO_OK;
}
static pico_status_t kfstSubObjDeallocate(register picoknow_KnowledgeBase this,
picoos_MemoryManager mm)
{
if (NULL != this) {
picoos_deallocate(mm, (void *) &this->subObj);
}
return PICO_OK;
}
/* calculates a small number of data (e.g. addresses) from kb for fast access.
* This data is encapsulated in a picokfst_FST that can later be retrieved
* with picokfst_getFST. */
pico_status_t picokfst_specializeFSTKnowledgeBase(picoknow_KnowledgeBase this,
picoos_Common common)
{
pico_status_t status;
if (NULL == this) {
return picoos_emRaiseException(common->em, PICO_EXC_KB_MISSING, NULL, NULL);
}
if (0 < this->size) {
/* not a dummy kb */
this->subDeallocate = kfstSubObjDeallocate;
this->subObj = picoos_allocate(common->mm, sizeof(kfst_subobj_t));
if (NULL == this->subObj) {
return picoos_emRaiseException(common->em, PICO_EXC_OUT_OF_MEM, NULL, NULL);
}
status = kfstInitialize(this, common);
if (PICO_OK != status) {
picoos_deallocate(common->mm,(void **)&this->subObj);
}
}
return PICO_OK;
}
/* ************************************************************/
/* FST type and getFST function */
/* ************************************************************/
/* return kb FST for usage in PU */
picokfst_FST picokfst_getFST(picoknow_KnowledgeBase this)
{
if (NULL == this) {
return NULL;
} else {
return (picokfst_FST) this->subObj;
}
}
/* ************************************************************/
/* FST access methods */
/* ************************************************************/
/* see description in header file */
extern picoos_uint8 picokfst_kfstGetTransductionMode(picokfst_FST this)
{
kfst_SubObj fst = (kfst_SubObj) this;
if (fst != NULL) {
return fst->transductionMode;
} else {
return 0;
}
}
/* see description in header file */
extern void picokfst_kfstGetFSTSizes (picokfst_FST this, picoos_int32 *nrStates, picoos_int32 *nrClasses)
{
kfst_SubObj fst = (kfst_SubObj) this;
if (fst != NULL) {
*nrStates = fst->nrStates;
*nrClasses = fst->nrClasses;
} else {
*nrStates = 0;
*nrClasses = 0;
}
}
/* see description in header file */
extern void picokfst_kfstStartPairSearch (picokfst_FST this, picokfst_symid_t inSym,
picoos_bool * inSymFound, picoos_int32 * searchState)
{
picoos_uint32 pos;
picoos_int32 offs;
picoos_int32 h;
picoos_int32 inSymCellPos;
picoos_int32 inSymX;
picoos_int32 nextSameHashInSymOffs;
kfst_SubObj fst = (kfst_SubObj) this;
(*searchState) = -1;
(*inSymFound) = 0;
h = inSym % fst->alphaHashTabSize;
pos = fst->alphaHashTabPos + (h * 4);
FixedBytesToSignedNum(fst->fstStream,4,& pos,& offs);
if (offs > 0) {
inSymCellPos = fst->alphaHashTabPos + offs;
pos = inSymCellPos;
BytesToNum(fst->fstStream,& pos,& inSymX);
BytesToNum(fst->fstStream,& pos,& nextSameHashInSymOffs);
while ((inSymX != inSym) && (nextSameHashInSymOffs > 0)) {
inSymCellPos = inSymCellPos + nextSameHashInSymOffs;
pos = inSymCellPos;
BytesToNum(fst->fstStream,& pos,& inSymX);
BytesToNum(fst->fstStream,& pos,& nextSameHashInSymOffs);
}
if (inSymX == inSym) {
/* input symbol found; state is set to position after symbol cell */
(*searchState) = pos;
(*inSymFound) = 1;
}
}
}
/* see description in header file */
extern void picokfst_kfstGetNextPair (picokfst_FST this, picoos_int32 * searchState,
picoos_bool * pairFound,
picokfst_symid_t * outSym, picokfst_class_t * pairClass)
{
picoos_uint32 pos;
picoos_int32 val;
kfst_SubObj fst = (kfst_SubObj) this;
if ((*searchState) < 0) {
(*pairFound) = 0;
(*outSym) = PICOKFST_SYMID_ILLEG;
(*pairClass) = -1;
} else {
pos = (*searchState);
BytesToNum(fst->fstStream,& pos,& val);
*outSym = (picokfst_symid_t)val;
if ((*outSym) != PICOKFST_SYMID_ILLEG) {
BytesToNum(fst->fstStream,& pos,& val);
*pairClass = (picokfst_class_t)val;
(*pairFound) = 1;
(*searchState) = pos;
} else {
(*pairFound) = 0;
(*outSym) = PICOKFST_SYMID_ILLEG;
(*pairClass) = -1;
(*searchState) = -1;
}
}
}
/* see description in header file */
extern void picokfst_kfstGetTrans (picokfst_FST this, picokfst_state_t startState, picokfst_class_t transClass,
picokfst_state_t * endState)
{
picoos_uint32 pos;
picoos_int32 index;
picoos_uint32 endStateX;
kfst_SubObj fst = (kfst_SubObj) this;
if ((startState < 1) || (startState > fst->nrStates) || (transClass < 1) || (transClass > fst->nrClasses)) {
(*endState) = 0;
} else {
index = (startState - 1) * fst->nrClasses + transClass - 1;
pos = fst->transTabPos + (index * fst->transTabEntrySize);
FixedBytesToUnsignedNum(fst->fstStream,fst->transTabEntrySize,& pos,& endStateX);
(*endState) = endStateX;
}
}
/* see description in header file */
extern void picokfst_kfstStartInEpsTransSearch (picokfst_FST this, picokfst_state_t startState,
picoos_bool * inEpsTransFound, picoos_int32 * searchState)
{
picoos_int32 offs;
picoos_uint32 pos;
kfst_SubObj fst = (kfst_SubObj) this;
(*searchState) = -1;
(*inEpsTransFound) = 0;
if ((startState > 0) && (startState <= fst->nrStates)) {
pos = fst->inEpsStateTabPos + (startState - 1) * 4;
FixedBytesToSignedNum(fst->fstStream,4,& pos,& offs);
if (offs > 0) {
(*searchState) = fst->inEpsStateTabPos + offs;
(*inEpsTransFound) = 1;
}
}
}
/* see description in header file */
extern void picokfst_kfstGetNextInEpsTrans (picokfst_FST this, picoos_int32 * searchState,
picoos_bool * inEpsTransFound,
picokfst_symid_t * outSym, picokfst_state_t * endState)
{
picoos_uint32 pos;
picoos_int32 val;
kfst_SubObj fst = (kfst_SubObj) this;
if ((*searchState) < 0) {
(*inEpsTransFound) = 0;
(*outSym) = PICOKFST_SYMID_ILLEG;
(*endState) = 0;
} else {
pos = (*searchState);
BytesToNum(fst->fstStream,& pos,& val);
*outSym = (picokfst_symid_t)val;
if ((*outSym) != PICOKFST_SYMID_ILLEG) {
BytesToNum(fst->fstStream,& pos,& val);
*endState = (picokfst_state_t)val;
(*inEpsTransFound) = 1;
(*searchState) = pos;
} else {
(*inEpsTransFound) = 0;
(*outSym) = PICOKFST_SYMID_ILLEG;
(*endState) = 0;
(*searchState) = -1;
}
}
}
/* see description in header file */
extern picoos_bool picokfst_kfstIsAcceptingState (picokfst_FST this, picokfst_state_t state)
{
picoos_uint32 pos;
picoos_uint32 val;
kfst_SubObj fst = (kfst_SubObj) this;
if ((state > 0) && (state <= fst->nrStates)) {
pos = fst->accStateTabPos + (state - 1);
FixedBytesToUnsignedNum(fst->fstStream,1,& pos,& val);
return (val == 1);
} else {
return 0;
}
}
#ifdef __cplusplus
}
#endif
/* End picofst.c */
|