| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 1999 */ |
| 6 |
/* All Rights Reserved. */ |
| 7 |
/* */ |
| 8 |
/* Permission is hereby granted, free of charge, to use and distribute */ |
| 9 |
/* this software and its documentation without restriction, including */ |
| 10 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 11 |
/* distribute, sublicense, and/or sell copies of this work, and to */ |
| 12 |
/* permit persons to whom this work is furnished to do so, subject to */ |
| 13 |
/* the following conditions: */ |
| 14 |
/* 1. The code must retain the above copyright notice, this list of */ |
| 15 |
/* conditions and the following disclaimer. */ |
| 16 |
/* 2. Any modifications must be clearly marked as such. */ |
| 17 |
/* 3. Original authors' names are not deleted. */ |
| 18 |
/* 4. The authors' names are not used to endorse or promote products */ |
| 19 |
/* derived from this software without specific prior written */ |
| 20 |
/* permission. */ |
| 21 |
/* */ |
| 22 |
/* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */ |
| 23 |
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */ |
| 24 |
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */ |
| 25 |
/* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */ |
| 26 |
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */ |
| 27 |
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */ |
| 28 |
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */ |
| 29 |
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */ |
| 30 |
/* THIS SOFTWARE. */ |
| 31 |
/* */ |
| 32 |
/*************************************************************************/ |
| 33 |
/* Author: Alan W Black ([email protected]) */ |
| 34 |
/* Date: December 1999 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Items (and Item Contents) */ |
| 38 |
/* */ |
| 39 |
/*************************************************************************/ |
| 40 |
#include "cst_alloc.h" |
| 41 |
#include "cst_item.h" |
| 42 |
#include "cst_relation.h" |
| 43 |
#include "cst_utterance.h" |
| 44 |
|
| 45 |
/* Define functions for using items, rels and utts as vals */ |
| 46 |
CST_VAL_REGISTER_TYPE_NODEL(relation,cst_relation) |
| 47 |
CST_VAL_REGISTER_TYPE_NODEL(item,cst_item) |
| 48 |
CST_VAL_REGISTER_TYPE(utterance,cst_utterance) |
| 49 |
CST_VAL_REGISTER_FUNCPTR(itemfunc,cst_itemfunc) |
| 50 |
|
| 51 |
cst_item *new_item_relation(cst_relation *r,cst_item *i) |
| 52 |
{ |
| 53 |
cst_item *ni; |
| 54 |
|
| 55 |
ni = cst_utt_alloc(r->utterance, cst_item, 1); |
| 56 |
ni->contents = 0; |
| 57 |
ni->n = ni->p = ni->u = ni->d = 0; |
| 58 |
ni->relation = r; |
| 59 |
item_contents_set(ni,i); |
| 60 |
return ni; |
| 61 |
} |
| 62 |
|
| 63 |
void item_contents_set(cst_item *current, cst_item *i) |
| 64 |
{ |
| 65 |
cst_item_contents *c = 0; |
| 66 |
cst_item *nn_item; |
| 67 |
if (i == 0) |
| 68 |
c = new_item_contents(current); |
| 69 |
else |
| 70 |
c = i->contents; |
| 71 |
if (c != current->contents) |
| 72 |
{ |
| 73 |
item_unref_contents(current); |
| 74 |
current->contents = c; |
| 75 |
/* If this contents is already in this relation */ |
| 76 |
/* empty the other reference */ |
| 77 |
if (feat_present(current->contents->relations,current->relation->name)) |
| 78 |
{ /* oops this is already in this relation */ |
| 79 |
nn_item = val_item(feat_val(current->contents->relations, |
| 80 |
current->relation->name)); |
| 81 |
feat_set(nn_item->contents->relations, |
| 82 |
current->relation->name, |
| 83 |
item_val(nn_item)); |
| 84 |
|
| 85 |
} |
| 86 |
/* Add back reference */ |
| 87 |
feat_set(current->contents->relations, |
| 88 |
current->relation->name, |
| 89 |
item_val(current)); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void delete_item(cst_item *item) |
| 94 |
{ |
| 95 |
cst_item *ds, *nds; |
| 96 |
|
| 97 |
if (item->n != NULL) |
| 98 |
{ |
| 99 |
item->n->p = item->p; |
| 100 |
item->n->u = item->u; /* in trees if this is first daughter */ |
| 101 |
} |
| 102 |
if (item->p != NULL) item->p->n = item->n; |
| 103 |
if (item->u != NULL) item->u->d = item->n; /* when first daughter */ |
| 104 |
|
| 105 |
if (item->relation) |
| 106 |
{ |
| 107 |
if (item->relation->head == item) |
| 108 |
item->relation->head = item->n; |
| 109 |
if (item->relation->tail == item) |
| 110 |
item->relation->tail = item->p; |
| 111 |
} |
| 112 |
|
| 113 |
/* Delete all the daughters of item */ |
| 114 |
for (ds = item->d; ds; ds=nds) |
| 115 |
{ |
| 116 |
nds = ds->n; |
| 117 |
delete_item(ds); |
| 118 |
} |
| 119 |
|
| 120 |
item_unref_contents(item); |
| 121 |
cst_utt_free(item->relation->utterance, item); |
| 122 |
} |
| 123 |
|
| 124 |
void item_unref_contents(cst_item *item) |
| 125 |
{ |
| 126 |
/* unreference this item from contents, and delete contents */ |
| 127 |
/* if no one else is referencing it */ |
| 128 |
|
| 129 |
if (item && item->contents) |
| 130 |
{ |
| 131 |
feat_remove(item->contents->relations,item->relation->name); |
| 132 |
if (feat_length(item->contents->relations) == 0) |
| 133 |
{ |
| 134 |
delete_features(item->contents->relations); |
| 135 |
delete_features(item->contents->features); |
| 136 |
cst_utt_free(item->relation->utterance,item->contents); |
| 137 |
} |
| 138 |
item->contents = NULL; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
cst_item_contents *new_item_contents(cst_item *i) |
| 143 |
{ |
| 144 |
cst_item_contents *ic; |
| 145 |
|
| 146 |
ic = cst_utt_alloc(i->relation->utterance,cst_item_contents,1); |
| 147 |
ic->features = new_features_local(i->relation->utterance->ctx); |
| 148 |
ic->relations = new_features_local(i->relation->utterance->ctx); |
| 149 |
|
| 150 |
return ic; |
| 151 |
} |
| 152 |
|
| 153 |
cst_item *item_as(const cst_item *i,const char *rname) |
| 154 |
{ |
| 155 |
/* return i as relation rname or null */ |
| 156 |
const cst_val *v; |
| 157 |
|
| 158 |
if (i == NULL) |
| 159 |
return NULL; |
| 160 |
else |
| 161 |
{ |
| 162 |
v = feat_val(i->contents->relations,rname); |
| 163 |
if (v != NULL) |
| 164 |
return val_item(v); |
| 165 |
else |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
/********************************************************************/ |
| 171 |
/* List relation related functions */ |
| 172 |
/********************************************************************/ |
| 173 |
|
| 174 |
cst_item *item_next(const cst_item *i) |
| 175 |
{ |
| 176 |
if (i == NULL) |
| 177 |
return NULL; |
| 178 |
else |
| 179 |
return i->n; |
| 180 |
} |
| 181 |
|
| 182 |
cst_item *item_prev(const cst_item *i) |
| 183 |
{ |
| 184 |
if (i == NULL) |
| 185 |
return NULL; |
| 186 |
else |
| 187 |
return i->p; |
| 188 |
} |
| 189 |
|
| 190 |
cst_item *item_append(cst_item *current, cst_item *ni) |
| 191 |
{ |
| 192 |
cst_item *rni = 0; |
| 193 |
|
| 194 |
if (ni && (ni->relation == current->relation)) |
| 195 |
{ |
| 196 |
/* got to delete it first as an item can't be in a relation twice */ |
| 197 |
} |
| 198 |
else |
| 199 |
rni = new_item_relation(current->relation,ni); |
| 200 |
|
| 201 |
rni->n = current->n; |
| 202 |
if (current->n != NULL) |
| 203 |
current->n->p = rni; |
| 204 |
rni->p = current; |
| 205 |
current->n = rni; |
| 206 |
|
| 207 |
if (current->relation->tail == current) |
| 208 |
current->relation->tail = rni; |
| 209 |
|
| 210 |
return rni; |
| 211 |
} |
| 212 |
|
| 213 |
cst_item *item_prepend(cst_item *current, cst_item *ni) |
| 214 |
{ |
| 215 |
cst_item *rni = 0; |
| 216 |
|
| 217 |
if (ni && (ni->relation == current->relation)) |
| 218 |
{ |
| 219 |
/* got to delete it first as an item can't be in a relation twice */ |
| 220 |
} |
| 221 |
else |
| 222 |
rni = new_item_relation(current->relation,ni); |
| 223 |
|
| 224 |
rni->p = current->p; |
| 225 |
if (current->p != NULL) |
| 226 |
current->p->n = rni; |
| 227 |
rni->n = current; |
| 228 |
current->p = rni; |
| 229 |
|
| 230 |
if (current->u) /* in a tree */ |
| 231 |
{ |
| 232 |
current->u->d = rni; |
| 233 |
rni->u = current->u; |
| 234 |
current->u = NULL; |
| 235 |
} |
| 236 |
|
| 237 |
if (current->relation->head == current) |
| 238 |
current->relation->head = rni; |
| 239 |
|
| 240 |
return rni; |
| 241 |
} |
| 242 |
|
| 243 |
/********************************************************************/ |
| 244 |
/* Tree relation related functions */ |
| 245 |
/********************************************************************/ |
| 246 |
cst_item *item_parent(const cst_item *i) |
| 247 |
{ |
| 248 |
const cst_item *n; |
| 249 |
|
| 250 |
for (n=i; item_prev(n); n=item_prev(n)); |
| 251 |
if (n == NULL) |
| 252 |
return NULL; |
| 253 |
else |
| 254 |
return n->u; |
| 255 |
} |
| 256 |
|
| 257 |
cst_item *item_daughter(const cst_item *i) |
| 258 |
{ |
| 259 |
if (i == NULL) |
| 260 |
return NULL; |
| 261 |
else |
| 262 |
return i->d; |
| 263 |
} |
| 264 |
|
| 265 |
cst_item *item_nth_daughter(const cst_item *i,int n) |
| 266 |
{ |
| 267 |
int d; |
| 268 |
cst_item *p; |
| 269 |
|
| 270 |
for (d=0,p=item_daughter(i); p && (d < n); p=item_next(p),d++); |
| 271 |
return p; |
| 272 |
} |
| 273 |
|
| 274 |
cst_item *item_last_daughter(const cst_item *i) |
| 275 |
{ |
| 276 |
cst_item *p; |
| 277 |
|
| 278 |
for (p=item_daughter(i); item_next(p); p=item_next(p)); |
| 279 |
return p; |
| 280 |
} |
| 281 |
|
| 282 |
cst_item *item_first(const cst_item *i) |
| 283 |
{ |
| 284 |
cst_item *p; |
| 285 |
|
| 286 |
for (p=(cst_item *)(void *)i; item_prev(p); p=item_prev(p)); |
| 287 |
return p; |
| 288 |
} |
| 289 |
|
| 290 |
cst_item *item_last(const cst_item *i) |
| 291 |
{ |
| 292 |
cst_item *p; |
| 293 |
|
| 294 |
for (p=(cst_item *)(void *)i; item_next(p); p=item_next(p)); |
| 295 |
return p; |
| 296 |
} |
| 297 |
|
| 298 |
cst_item *item_add_daughter(cst_item *i,cst_item *nd) |
| 299 |
{ |
| 300 |
cst_item *p,*rnd; |
| 301 |
|
| 302 |
p = item_last_daughter(i); |
| 303 |
|
| 304 |
if (p) |
| 305 |
rnd=item_append(p,nd); |
| 306 |
else |
| 307 |
{ /* first new daughter */ |
| 308 |
if (nd && (nd->relation == i->relation)) |
| 309 |
{ |
| 310 |
/* got to delete it first as nd can't be in a relation twice */ |
| 311 |
cst_errmsg("item_add_daughter: already in relation\n"); |
| 312 |
return 0; |
| 313 |
} |
| 314 |
else |
| 315 |
rnd = new_item_relation(i->relation,nd); |
| 316 |
|
| 317 |
rnd->u = i; |
| 318 |
i->d = rnd; |
| 319 |
} |
| 320 |
|
| 321 |
return rnd; |
| 322 |
} |
| 323 |
|
| 324 |
/********************************************************************/ |
| 325 |
/* Feature functions */ |
| 326 |
/********************************************************************/ |
| 327 |
|
| 328 |
int item_feat_present(const cst_item *i,const char *name) |
| 329 |
{ |
| 330 |
return feat_present(item_feats(i),name); |
| 331 |
} |
| 332 |
|
| 333 |
int item_feat_remove(const cst_item *i,const char *name) |
| 334 |
{ |
| 335 |
return feat_remove(item_feats(i),name); |
| 336 |
} |
| 337 |
|
| 338 |
cst_features *item_feats(const cst_item *i) |
| 339 |
{ |
| 340 |
return (i ? i->contents->features : NULL); |
| 341 |
} |
| 342 |
|
| 343 |
const cst_val *item_feat(const cst_item *i,const char *name) |
| 344 |
{ |
| 345 |
return feat_val(item_feats(i),name); |
| 346 |
} |
| 347 |
|
| 348 |
int item_feat_int(const cst_item *i,const char *name) |
| 349 |
{ |
| 350 |
return feat_int(item_feats(i),name); |
| 351 |
} |
| 352 |
|
| 353 |
float item_feat_float(const cst_item *i,const char *name) |
| 354 |
{ |
| 355 |
return feat_float(item_feats(i),name); |
| 356 |
} |
| 357 |
|
| 358 |
const char *item_feat_string(const cst_item *i,const char *name) |
| 359 |
{ |
| 360 |
return feat_string(item_feats(i),name); |
| 361 |
} |
| 362 |
|
| 363 |
void item_set(const cst_item *i,const char *name,const cst_val *val) |
| 364 |
{ |
| 365 |
feat_set(item_feats(i),name,val); |
| 366 |
} |
| 367 |
|
| 368 |
void item_set_int(const cst_item *i,const char *name,int val) |
| 369 |
{ |
| 370 |
feat_set_int(item_feats(i),name,val); |
| 371 |
} |
| 372 |
|
| 373 |
void item_set_float(const cst_item *i,const char *name,float val) |
| 374 |
{ |
| 375 |
feat_set_float(item_feats(i),name,val); |
| 376 |
} |
| 377 |
|
| 378 |
void item_set_string(const cst_item *i,const char *name,const char *val) |
| 379 |
{ |
| 380 |
feat_set_string(item_feats(i),name,val); |
| 381 |
} |
| 382 |
|
| 383 |
cst_utterance *item_utt(const cst_item *i) |
| 384 |
{ |
| 385 |
if (i && i->relation) |
| 386 |
return i->relation->utterance; |
| 387 |
else |
| 388 |
return NULL; |
| 389 |
} |
| 390 |
|
| 391 |
int item_equal(const cst_item *a, const cst_item *b) |
| 392 |
{ |
| 393 |
if ((a == b) || |
| 394 |
(a && b && (a->contents == b->contents))) |
| 395 |
return TRUE; |
| 396 |
else |
| 397 |
return FALSE; |
| 398 |
} |