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 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
|
/*************************************************************************/
/* */
/* Centre for Speech Technology Research */
/* University of Edinburgh, UK */
/* Copyright (c) 1998 */
/* All Rights Reserved. */
/* */
/* Permission is hereby granted, free of charge, to use and distribute */
/* this software and its documentation without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of this work, and to */
/* permit persons to whom this work is furnished to do so, subject to */
/* the following conditions: */
/* 1. The code must retain the above copyright notice, this list of */
/* conditions and the following disclaimer. */
/* 2. Any modifications must be clearly marked as such. */
/* 3. Original authors' names are not deleted. */
/* 4. The authors' names are not used to endorse or promote products */
/* derived from this software without specific prior written */
/* permission. */
/* */
/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
/* THIS SOFTWARE. */
/* */
/*************************************************************************/
/* Author : Alan W Black */
/* Date : May 1998 */
/*-----------------------------------------------------------------------*/
/* Linguistic items (e.g. words, phones etc) held as part of Relations */
/* */
/* These objects may be held in relations within an utterance. They */
/* fact contain two sections, a structural part and a contents part */
/* (EST_Item_Content) though the user will usually only see the whole */
/* object. The content part may be shared between linguistic items in */
/* other relations, e.g. the word item may appear both in the word */
/* relation and the syntax relation. */
/* */
/* Each linguistic item is in a particular relation but it is easy */
/* to link to that item in another relation. Traversal of the relation */
/* for an item in it is trivial. */
/* */
/*=======================================================================*/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <fstream>
#include "ling_class/EST_Item.h"
#include "ling_class/EST_Relation.h"
#include "ling_class/EST_Utterance.h"
#include "EST_TKVL.h"
#include "EST_UList.h"
#include "EST_string_aux.h"
#include "ling_class_init.h"
/* Class initialisation. This is where you should register
* feature functions and so on.
*/
void EST_Item::class_init(void)
{
#ifdef EST_DEBUGGING
cerr << "Calling EST_Item init\n";
#endif
ling_class_init::use();
EST_register_feature_functions(standard);
#ifdef EST_DEBUGGING
cerr << "Finished EST_Item init\n";
#endif
}
EST_Item::EST_Item()
{
p_relation = 0;
p_contents = 0;
n=p=u=d=0;
set_contents(0);
}
void EST_Item::copy(const EST_Item &s)
{
// You can't really do this in general as a node is doubly
// linked to its neighbours and item. Copying all the fields would
// mean it was no longer true (unless you copied everything).
// So all you get for this is a *copy* of the contents (not a reference
// to)
p_relation = 0;
p_contents = 0;
n=p=u=d=0;
set_contents(0); // get an empty contents structure
*p_contents = *s.p_contents;
}
EST_Item::EST_Item(const EST_Item &i)
{
copy(i);
}
EST_Item::~EST_Item()
{
// Delete this item and its daughters (and the contents with no
// other links)
// Assumes a tree structure
EST_Item *ds,*nds;
// Tidy up pointers to this
if (n != 0)
{
n->p = p;
n->u = u; // when deleting first daughter.
}
if (p != 0) p->n = n;
if (u != 0) u->d = n;
if (p_relation)
{
if (p_relation->p_head == this)
p_relation->p_head = n;
if (p_relation->p_tail == this)
p_relation->p_tail = p;
}
// A little cleverer with the daughters
for (ds=d; ds != 0; ds=nds)
{
nds=ds->n;
delete ds;
}
unref_contents();
}
EST_Item::EST_Item(EST_Relation *rel)
{
p_relation = rel;
p_contents = 0;
n=p=u=d=0;
}
// all internal ids are found by getting the next id number from
// the utterance and prefixing a "_" to show that this is internally
// generated.
static void assign_id(EST_Item *s)
{
if (s->f_present("id"))
return;
EST_Utterance *u = get_utt(s);
if (u != 0)
s->set("id", "_" + itoString(u->next_id()));
}
EST_Item::EST_Item(EST_Relation *rel, EST_Item *li)
{
p_relation = rel;
p_contents = 0;
n=p=u=d=0;
if (li)
set_contents(li->contents());
else
set_contents(0);
assign_id(this);
}
void EST_Item::evaluate_features()
{
evaluate(this,p_contents->f);
}
void EST_Item::unref_contents()
{
// Unref the related contents to this item, delete if no-one else is
// referencing it
if (p_contents != 0)
{
if (p_contents->unref_relation(relation_name()))
delete p_contents;
p_contents = 0;
}
}
void EST_Item::unref_all()
{
// Unreference this item from all its relations, deleting its contents
p_contents->unref_and_delete();
}
const EST_String &EST_Item::relation_name() const
{
return ((this == 0) || (p_relation == 0)) ?
EST_String::Empty : p_relation->name();
}
void EST_Item::set_contents(EST_Item_Content *new_contents)
{
// This function is for internal use only, general use of this
// is likely to be unsafe.
EST_Item_Content *c;
if (new_contents == 0)
c = new EST_Item_Content;
else
c = new_contents;
if (p_contents != c)
{
unref_contents();
p_contents = c;
EST_Item *nn_item = p_contents->Relation(relation_name());
if (nn_item) // this is already linked to this relation
{ // can't recurse on set_contents
nn_item->p_contents = new EST_Item_Content;
nn_item->p_contents->relations.add_item(relation_name(),
est_val(nn_item));
}
p_contents->relations.add_item(relation_name(),est_val(this));
}
}
int EST_Item::length() const
{
int i=0;
EST_Item *nn = (EST_Item *)(void *)this;
for (; nn; nn=nn->n,i++);
return i;
}
EST_Item *EST_Item::insert_after(EST_Item *si)
{
// Create a new item and add it after t, and return it.
// Include the cross link from this new item's contents to si, and
// from si's relations fields back to the new node
EST_Item *new_node = new EST_Item(p_relation,si);
new_node->p = this;
new_node->n = this->n;
if (new_node->n != 0)
new_node->n->p = new_node;
this->n = new_node;
if (p_relation && (p_relation->p_tail == this))
p_relation->p_tail = new_node;
return new_node;
}
EST_Item *EST_Item::insert_before(EST_Item *si)
{
// Create a new node and add it before this, and return it.
EST_Item *new_node = new EST_Item(p_relation,si);
new_node->n = this;
new_node->p = this->p;
if (new_node->p != 0)
new_node->p->n = new_node;
this->p = new_node;
// This makes an assumption that we represent trees with only
// the first daughter pointing to the parent
if (this->u)
{
new_node->u = this->u;
new_node->u->d = new_node;
this->u = 0;
}
if (p_relation && (p_relation->p_head == this))
p_relation->p_head = new_node;
return new_node;
}
EST_Item *EST_Item::insert_below(EST_Item *si)
{
// Create a new node and add it below this, and return it.
EST_Item *new_node = new EST_Item(p_relation,si);
new_node->u = this;
new_node->d = this->d;
if (new_node->d != 0)
new_node->d->u = new_node;
this->d = new_node;
return new_node;
}
EST_Item *EST_Item::insert_above(EST_Item *si)
{
// Create a new node and add it above this, and return it.
EST_Item *new_node = new EST_Item(p_relation,si);
new_node->d = this;
new_node->u = this->u;
if (new_node->u != 0)
new_node->u->d = new_node;
this->u = new_node;
if (p_relation && (p_relation->p_head == this))
p_relation->p_head = new_node;
if (p_relation && (p_relation->p_tail == this))
p_relation->p_tail = new_node;
return new_node;
}
EST_Item *EST_Item::insert_parent(EST_Item *si)
{
// Insert new parent here, by added a new below node and moving
// the contents down to it.
insert_below(0);
d->set_contents(grab_contents());
if (si != 0)
set_contents(si->grab_contents());
else
set_contents(0);
return this;
}
EST_Item *inext(const EST_Item *x)
{
if (x == NULL)
return NULL;
else
return x->n;
}
EST_Item *iprev(const EST_Item *x)
{
if (x == NULL)
return NULL;
else
return x->p;
}
EST_Item *idown(const EST_Item *x)
{
if (x == NULL)
return NULL;
else
return x->d;
}
EST_Item *iup(const EST_Item *x)
{
if (x == NULL)
return NULL;
else
return x->u;
}
EST_Item *last(const EST_Item *x)
{
// To get round the const access to this
EST_Item *node = (EST_Item *)(void *)x;
for (; node && inext(node) != 0; node=inext(node));
return node;
}
EST_Item *first(const EST_Item *x)
{
// To get round the const access to this
EST_Item *node = (EST_Item *)(void *)x;
for (; node && iprev(node) != 0; node=iprev(node));
return node;
}
EST_Item *top(const EST_Item *x)
{
EST_Item *node = (EST_Item *)(void *)x;
for (; node && parent(node) != 0; node=parent(node));
return node;
}
EST_Item *next_leaf(const EST_Item *x)
{
if (x == NULL) return NULL;
if (inext(x) != NULL)
return first_leaf(inext(x));
else
return next_leaf(parent(x));
}
EST_Item *next_item(const EST_Item *x)
{
// For traversal through a relation, in pre-order (root then daughters)
if (x == NULL)
return NULL;
else if (idown(x) != NULL)
return idown(x);
else if (inext(x) != NULL)
return inext(x);
else
{ // at the right most leaf so go up until you find a parent with a next
for (EST_Item *pp = parent(x); pp != 0; pp = parent(pp))
if (inext(pp)) return inext(pp);
return NULL;
}
}
EST_Item *first_leaf(const EST_Item *x)
{
// Leafs are defined as those nodes with no daughters
if (x == NULL) return NULL;
if (idown(x) == NULL)
return (EST_Item *)(void *)x;
else
return first_leaf(idown(x));
}
EST_Item *last_leaf(const EST_Item *x)
{
// Leafs are defined as those nodes with no daughters
if (x == NULL) return NULL;
if (inext(x))
return last_leaf(last(x));
else if (idown(x))
return last_leaf(idown(x));
else
return (EST_Item *)(void *)x;
}
EST_Item *first_leaf_in_tree(const EST_Item *root)
{
if (root == NULL) return NULL;
return first_leaf(root);
}
EST_Item *last_leaf_in_tree(const EST_Item *root)
{
if (root == NULL) return NULL;
if (idown(root) == NULL)
return (EST_Item *)(void *)root;
else
return last_leaf(idown(root));
}
EST_Item *EST_Item::append_daughter(EST_Item *si)
{
EST_Item *nnode;
EST_Item *its_downs;
EST_Item *c = NULL;
// Because we don't distinguish forests properly we need
// to do nasty things if this si is already associated to a
// this relation and its "in the top list"
if (si)
c = si->as_relation(relation_name());
if (in_list(c,p_relation->head()))
{
// need to save its daughters to put on the new node
its_downs = c->d;
c->d = 0; // otherwise it could delete its sub tree
if (its_downs) its_downs->u = 0;
if (d == 0)
nnode = insert_below(si);
else
nnode = last(d)->insert_after(si);
// put daughters back on the new item
if (its_downs)
{
its_downs->u = nnode;
nnode->d = its_downs;
}
delete c; // delete its old form from the top level
}
else if (d == 0)
nnode = insert_below(si);
else
nnode = last(d)->insert_after(si);
return nnode;
}
EST_Item *EST_Item::prepend_daughter(EST_Item *si)
{
EST_Item *nnode;
EST_Item *its_downs;
// Because we don't distinguish forests properly we need
// to do nasty things if this si is already associated to a
// this relation and its "in the top list"
EST_Item *c = si->as_relation(relation_name());
if (in_list(c,p_relation->head()))
{
// need to save its daughters to put on the new node
its_downs = c->d;
c->d = 0; // otherwise it could delete its sub tree
if (its_downs) its_downs->u = 0;
if (d == 0)
nnode = insert_below(si);
else
nnode = d->insert_before(si);
// put daughters back on the new item
if (its_downs)
{
its_downs->u = nnode;
nnode->d = its_downs;
}
delete c; // delete its old form from the top level
}
else if (d == 0)
nnode = insert_below(si);
else
nnode = d->insert_before(si);
return nnode;
}
EST_Item *EST_Item::grab_daughters()
{
EST_Item *dd = d;
if (dd)
{
dd->u = 0;
d = 0;
}
return dd;
}
EST_Item_Content *EST_Item::grab_contents(void)
{
// Unreference contents, but don't delete them if that's the
// last reference. It is the caller's responsibility to deal
// with these contents, typically they are just about to be set
// as contents of someone else so are only orphaned for a short
// time
EST_Item_Content *c = contents();
c->unref_relation(relation_name());
p_contents = 0;
set_contents(0); // can't sit without contents
return c;
}
void copy_node_tree(EST_Item *from, EST_Item *to)
{
// Copy this node and all its siblings and daughters
if (inext(from) != 0)
copy_node_tree(inext(from),to->insert_after(inext(from)));
if (idown(from) != 0)
copy_node_tree(idown(from),to->insert_below(idown(from)));
}
void copy_node_tree_contents(EST_Item *from, EST_Item *to)
{
// Copy this node and all its siblings and daughters
// also copy the item's contents
if (inext(from) != 0)
{
EST_Item i = *inext(from); // copies the contents
copy_node_tree_contents(inext(from),to->insert_after(&i));
}
if (idown(from) != 0)
{
EST_Item i = *idown(from);
copy_node_tree_contents(idown(from),to->insert_below(&i));
}
}
int EST_Item::verify() const
{
// Return FALSE if this node and its neighbours aren't
// properly linked
if (((d == 0) || (d->u == this)) &&
((n == 0) || (n->p == this)))
{
if ((d) && (!d->verify()))
return FALSE;
if ((n) && (!n->verify()))
return FALSE;
return TRUE;
}
else
return FALSE;
}
EST_Item *append_daughter(EST_Item *n, EST_Item *p)
{
return n->append_daughter(p);
}
EST_Item *append_daughter(EST_Item *n,const char *relname, EST_Item *p)
{
return append_daughter(as(n,relname),p);
}
EST_Item *prepend_daughter(EST_Item *n, EST_Item *p)
{
return n->prepend_daughter(p);
}
EST_Item *prepend_daughter(EST_Item *n, const char *relname, EST_Item *p)
{
return prepend_daughter(as(n,relname),p);
}
void remove_item(EST_Item *l, const char *relname)
{
EST_Item *lr = l->as_relation(relname);
EST_Relation *r = lr->relation();
if ((lr != 0) && (r != 0))
r->remove_item(lr);
}
EST_Item &EST_Item::operator=(const EST_Item &s)
{
copy(s);
return *this;
}
ostream& operator << (ostream &s, const EST_Item &a)
{
a.features().save(s);
return s;
}
void evaluate(EST_Item *a,EST_Features &f)
{
EST_Features::RwEntries p;
for(p.begin(f); p; ++p)
if (p->v.type() == val_type_featfunc)
{
if (featfunc(p->v) != NULL)
p->v = (featfunc(p->v))(a);
else
{
fprintf(stderr, "NULL %s function\n", (const char *) p->k );
p->v = EST_Features::feature_default_value;
}
}
}
VAL_REGISTER_CLASS_NODEL(item,EST_Item)
|