Loading...
Searching...
No Matches
item_aux.cc
/*************************************************************************/
/* */
/* 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 */
/*-----------------------------------------------------------------------*/
/* Various auxiliary item/relation functions for tree manipulation */
/* */
/*=======================================================================*/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <fstream>
#include "ling_class/EST_Item.h"
#include "ling_class/EST_Item_Content.h"
#include "ling_class/EST_Relation.h"
int in_list(const EST_Item *c,const EST_Item *l)
{
const EST_Item *i;
for (i=l; i != 0; i=inext(i))
if (i == c)
return TRUE;
return FALSE;
}
int in_tree(const EST_Item *c,const EST_Item *t)
{
EST_Item *i;
if (t == c)
return TRUE;
else
{
for (i=daughter1(t); i != 0; i=next_sibling(i))
if (in_tree(c,i))
return TRUE;
return FALSE;
}
}
void remove_item_list(EST_Relation *rel, EST_Item *item)
{
if (item==NULL)
return;
EST_Item *p = iprev(item);
EST_Item *n = inext(item);
rel->remove_item(item);
EST_Item::splice(p,n);
}
int merge_item(EST_Item *from, EST_Item *to)
{
// Make all references to from be references to to and merge
// from's features into to
EST_Item *i;
merge_features(to->features(),from->features());
EST_Litem *r;
for (r = from->relations().list.head(); r; r=r->next())
{
i = item(from->relations().list(r).v);
if (i != from)
i->set_contents(to->contents());
}
from->set_contents(to->contents());
return TRUE;
}
void merge_features(EST_Item *to, EST_Item *from, int keep_id)
{
// Merge item features, but with option of preserving ids
EST_String keep;
if (keep_id) keep = to->S("id", "0");
merge_features(to->features(),from->features());
if (keep_id) to->set("id", keep);
}
int move_item(EST_Item *from, EST_Item *to)
{
// from's contents be to's contents, deleting from and all
// its daughters from to's relation.
EST_Item *rfrom = from->as_relation(to->relation_name());
to->set_contents(from->contents());
if (rfrom != 0) // from is current in this relation
delete rfrom; // so delete it and its daughters
return TRUE;
}
int move_sub_tree(EST_Item *from, EST_Item *to)
{
// make from's contents be to's contents, delete all of to's
// daughters and rebuild from's descendants beneath to.
EST_Item *rfrom = from->as_relation(to->relation_name());
EST_Item *d,*r,*nr;
if (in_tree(to,from))
return FALSE; // can't do that
to->set_contents(from->contents());
// Remove current daughters, but don't delete them
// until after the copy in case from is within to's daughters
d = to->grab_daughters();
if (rfrom == d)
d = inext(d);
if ((rfrom != 0) && (daughter1(rfrom)))
{ // copy the descendant structure
copy_node_tree(daughter1(rfrom),to->insert_below(daughter1(rfrom)));
delete rfrom;
}
for (r=d; r; r=nr)
{
nr = inext(r);
delete r;
}
return TRUE;
}
int exchange_sub_trees(EST_Item *from,EST_Item *to)
{
// Take contents of from and its daughters and replace
// them with contents of to and its daughters (and the reverse)
EST_Item *rfrom = from->as_relation(to->relation_name());
if ((!rfrom) || (in_tree(rfrom,to)) || (in_tree(to,rfrom)))
return FALSE; // one or other in the other
EST_Item_Content *toc = to->grab_contents();
EST_Item_Content *fromc = rfrom->grab_contents();
EST_Item *from_d = rfrom->grab_daughters();
EST_Item *to_d = to->grab_daughters();
to->set_contents(fromc);
rfrom->set_contents(toc);
if (from_d)
copy_node_tree(from_d,to->insert_below(from_d));
if (to_d)
copy_node_tree(to_d,from->insert_below(to_d));
return TRUE;
}
EST_Item *item_jump(EST_Item *from, const EST_String &to)
{
// This function jumps around standard festival relation structures.
// Designed to be fast rather than anything else.
// Behaviour is undefined for non standard structures.
// Gives the first of non-unique items.
int f=0,t=0;
if (to == "Segment")
t=1;
else if (to == "Syllable")
t=2;
else if (to == "Word")
t=3;
else if (to == "IntEvent")
t=4;
if (from->in_relation("Segment"))
f=1;
else if (from->in_relation("Syllable"))
f=2;
else if (from->in_relation("Word"))
f=3;
else if (from->in_relation("IntEvent"))
f=4;
if ( t == 0 || f == 0 )
return 0;
if ( t == f )
return from;
switch(f) {
case 1:
// from Segment
switch(t) {
case 2:
// Syllable
return(iup(from->as_relation("SylStructure"))->as_relation("Syllable"));
case 3:
// Word
return(iup(iup(from->as_relation("SylStructure")))->as_relation("Word"));
case 4:
// IntEvent
return(idown(iup(from->as_relation("SylStructure"))->as_relation("Intonation"))->as_relation("IntEvent"));
}
case 2:
// from Syllable
switch(t) {
case 1:
// Segment
return(idown(from->as_relation("SylStructure"))->as_relation("Segment"));
case 3:
// Word
return(iup(from->as_relation("SylStructure"))->as_relation("Word"));
// IntEvent
case 4:
return(idown(from->as_relation("Intonation"))->as_relation("IntEvent"));
}
case 3:
// from Word
switch(t) {
case 1:
// Segment
return(idown(idown(from->as_relation("SylStructure")))->as_relation("Segment"));
case 2:
// Syllable
return(idown(from->as_relation("SylStructure"))->as_relation("Syllable"));
case 4:
return(idown(idown(from->as_relation("SylStructure"))->as_relation("Intonation"))->as_relation("IntEvent"));
}
case 4:
// from IntEvent
switch(t) {
case 1:
// Segment
return(idown(iup(from->as_relation("Intonation"))->as_relation("SylStructure"))->as_relation("Segment"));
case 2:
// Syllable
return(iup(from->as_relation("Intonation"))->as_relation("Syllable"));
case 3:
// Word
return(iup(iup(from->as_relation("Intonation"))->as_relation("SylStructure"))->as_relation("Word"));
}
}
return NULL;
}
EST_Item_Content
Definition EST_Item_Content.h:67
EST_Item
Definition EST_Item.h:83
EST_Item::in_relation
int in_relation(const EST_String &relname) const
TRUE if this item is in named relation.
Definition EST_Item.h:306
EST_Item::set
void set(const EST_String &name, int ival)
Definition EST_Item.h:179
EST_Item::relations
EST_TKVL< EST_String, EST_Val > & relations()
Access to the relation links.
Definition EST_Item.h:310
EST_Item::S
const EST_String S(const EST_String &name) const
Definition EST_Item.h:143
EST_Item::relation_name
const EST_String & relation_name() const
The relation name of this particular item.
Definition EST_Item.cc:199
EST_Item::as_relation
EST_Item * as_relation(const char *relname) const
View item from another relation (const char *) method.
Definition EST_Item.h:302
EST_Relation
Definition EST_Relation.h:68
EST_Relation::remove_item
void remove_item(EST_Item *item)
Definition EST_Relation.cc:165
EST_String
Definition EST_String.h:70
EST_TKVL::list
EST_TList< EST_TKVI< K, V > > list
Linked list of key-val pairs. Don't use this as it will be made private in the future.
Definition EST_TKVL.h:93