43#include "ling_class/EST_Relation.h"
44#include "ling_class/EST_Item.h"
45#include "relation_io.h"
74 tmp_name =
f.
S(
"name",
"");
76 f.
set(
"name", tmp_name);
82 copy_node_tree_contents(r.
root(),to_root);
97 nn = p_tail->insert_after(si);
126 nn = p_head->insert_before(si);
142 for (i=0,node=p_head; node; node=inext(node))
150 s->evaluate_features();
157 for (nn = p_head; nn != 0; nn = nnn)
168 p_head = inext(node);
170 p_tail = iprev(node);
176 for (
EST_Item *s = p_head; s; s = next_item(s))
187 if (from.
root() != 0)
190 copy_node_tree(from.
root(),to_root);
196 bool evaluate_ff)
const
199 return save_esps_label(&outf,*
this,evaluate_ff);
200 else if (type ==
"htk")
201 return save_htk_label(&outf,*
this);
204 EST_warning(
"EST_Relation: unsupported type: \"%s\"", (
const char *)type);
211 bool evaluate_ff)
const
214 return save_esps_label(filename,*
this,evaluate_ff);
215 else if (type ==
"htk")
216 return save_htk_label(filename,*
this);
219 EST_warning(
"EST_Relation: unsupported type: \"%s\"", (
const char *)type);
225 bool evaluate_ff)
const
227 return save(filename,
"esps",evaluate_ff);
235 outf <<
"Relation " <<
name() <<
" ; ";
238 save_items(p_head,outf,contents,nodenames,node_count);
239 outf <<
"End_of_Relation" << endl;
243EST_write_status EST_Relation::save_items(
EST_Item *node,
247 int &node_count)
const
256 myname = node_count++;
266 save_items(idown(n),outf,cnames,nodenames,node_count);
267 outf << nodenames.
val(n) <<
" " <<
268 (n->contents() == 0 ? 0 : cnames.
val(n->contents())) <<
" " <<
269 (iup(n) == 0 ? 0 : nodenames.
val(iup(n))) <<
" " <<
270 (idown(n) == 0 ? 0 : nodenames.
val(idown(n))) <<
" " <<
271 (inext(n) == 0 ? 0 : nodenames.
val(inext(n))) <<
" " <<
272 (iprev(n) == 0 ? 0 : nodenames.
val(iprev(n))) << endl;
283 if (ts.
get() !=
"Relation")
286 " no new Relation" << endl;
287 return misc_read_error;
289 p_name = ts.
get().string();
293 " semicolon missing after Relation name \"" <<
294 p_name <<
"\"" << endl;
295 return misc_read_error;
297 if (
f.
load(ts) != format_ok)
298 return misc_read_error;
299 if (load_items(ts,contents) != format_ok)
300 return misc_read_error;
310 if (ts.
get() !=
"Relation")
313 " no new Relation" << endl;
314 return misc_read_error;
316 p_name = ts.
get().string();
320 " semicolon missing after Relation name \"" <<
321 p_name <<
"\"" << endl;
322 return misc_read_error;
324 if (
f.
load(ts) != format_ok)
325 return misc_read_error;
326 if (load_items(ts,contents) != format_ok)
327 return misc_read_error;
332void EST_Relation::node_tidy_up_val(
int &k,
EST_Val &v)
345void EST_Relation::node_tidy_up(
int &k,
EST_Item *node)
366 EST_read_status r = format_ok;
371 while (ts.
peek() !=
"End_of_Relation")
373 int name = atoi(ts.
get().string());
376 node = get_item_from_name(nodenames,
name);
378 EST_error(
"Unknown item %d",
name);
387 siname = atoi(ts.
get().string());
395 " node's item contents" << siname <<
" doesn't exist\n";
400 node->set_contents(icontent(v));
403 node->u = get_item_from_name(nodenames,atoi(ts.
get().string()));
404 node->d = get_item_from_name(nodenames,atoi(ts.
get().string()));
405 node->n = get_item_from_name(nodenames,atoi(ts.
get().string()));
406 node->p = get_item_from_name(nodenames,atoi(ts.
get().string()));
413 int numlinks = atoi(ts.
get().string());
415 for (
int i=0;i<numlinks;++i)
417 EST_Item * item = get_item_from_name(nodenames,atoi(ts.
get().string()));
418 node->link_feats.
set_val(
"link" + itoString(i),est_val(item));
430 p_head = get_item_from_name(nodenames,1);
431 p_tail = last(p_head);
432 if (!p_head->verify())
435 " nodes do not form consistent graph" << endl;
447 nodenames.
map(node_tidy_up_val);
464 EST_read_status r = format_ok;
469 while (ts.
peek() !=
"End_of_Relation")
471 int name = atoi(ts.
get().string());
474 node = get_item_from_name(nodenames,
name);
476 EST_error(
"Unknown item %d",
name);
485 siname = atoi(ts.
get().string());
492 " node's stream item " << siname <<
" doesn't exist\n";
497 node->set_contents(c);
500 node->u = get_item_from_name(nodenames,atoi(ts.
get().string()));
501 node->d = get_item_from_name(nodenames,atoi(ts.
get().string()));
502 node->n = get_item_from_name(nodenames,atoi(ts.
get().string()));
503 node->p = get_item_from_name(nodenames,atoi(ts.
get().string()));
509 int numlinks = atoi(ts.
get().string());
511 for (
int i=0;i<numlinks;++i)
513 EST_Item * item = get_item_from_name(nodenames,atoi(ts.
get().string()));
525 p_head = get_item_from_name(nodenames,1);
527 p_tail = last(p_head);
528 if (p_head && !p_head->verify())
531 " nodes do not form consistent graph" << endl;
541 for(
int ni=0; ni<nodenames.
length(); ni++)
545 node_tidy_up(ni, node);
590 nodenames[
name] = node;
602 f.
set(
"filename",filename);
605 r = load_esps_label(ts,*
this);
606 else if (type ==
"ogi")
607 r = load_ogi_label(ts,*
this);
608 else if (type ==
"htk")
609 r = load_sample_label(ts,*
this,10000000);
610 else if ((type ==
"ascii") || (type ==
"timit"))
611 r = load_sample_label(ts,*
this,1);
612 else if (type ==
"words")
613 r = load_words_label(ts,*
this);
615 r = load_esps_label(ts,*
this);
627 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
629 cerr <<
"load_relation: can't open relation input file "
631 return misc_read_error;
633 r =
load(filename, ts, type);
645 for (n = first_leaf(h); n != 0; n=next_leaf(n))
void set(const EST_String &name, int ival)
EST_read_status load(EST_TokenStream &ts)
load features from already opened EST_TokenStream
EST_write_status save(ostream &outf) const
save features in already opened ostream
const EST_String S(const EST_String &path) const
EST_Relation * relation(void) const
The relation of this particular item.
void set_val(const EST_String &name, const EST_Val &sval)
void remove_item_feature(const EST_String &name)
void remove_item(EST_Item *item)
EST_read_status load(const EST_String &filename, const EST_String &type="esps")
EST_write_status save(const EST_String &filename, bool evaluate_ff=false) const
EST_Utterance * utt(void)
const EST_String & name() const
void evaluate_item_features()
V & val(const K &key, int &found) const
int add_item(const K &key, const V &value, int no_search=0)
Add an entry to the table.
const int length() const
number of key value pairs in list
int add_item(const K &rkey, const V &rval, int no_search=0)
add key-val pair to list
const V & val(const K &rkey, bool m=0) const
return value according to key (const)
void map(void(*func)(K &, V &))
apply function to each pair
void resize(int n, int set=1)
INLINE int length() const
number of items in vector.
const EST_String pos_description()
A string describing current position, suitable for error messages.
EST_Token & peek(void)
peek at next token
void close(void)
Close stream.
int open(const EST_String &filename)
open a \Ref{EST_TokenStream} for a file.
EST_TokenStream & get(EST_Token &t)
get next token in stream