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);
93 EST_warning(
"EST_Relation: no relation to append to");
102 nn = p_tail->insert_after(si);
127 EST_warning(
"EST_Relation: no relation to prepend to");
130 else if (p_head == 0)
136 nn = p_head->insert_before(si);
154 for (i=0,node=p_head; node; node=node->next())
162 s->evaluate_features();
169 for (nn = p_head; nn != 0; nn = nnn)
180 p_head = node->next();
182 p_tail = node->prev();
188 for (
EST_Item *s = p_head; s; s = next_item(s))
199 if (from.
root() != 0)
202 copy_node_tree(from.
root(),to_root);
208 bool evaluate_ff)
const
211 return save_esps_label(&outf,*
this,evaluate_ff);
212 else if (type ==
"htk")
213 return save_htk_label(&outf,*
this);
216 EST_warning(
"EST_Relation: unsupported type: \"%s\"", (
const char *)type);
223 bool evaluate_ff)
const
226 return save_esps_label(filename,*
this,evaluate_ff);
227 else if (type ==
"htk")
228 return save_htk_label(filename,*
this);
231 EST_warning(
"EST_Relation: unsupported type: \"%s\"", (
const char *)type);
237 bool evaluate_ff)
const
239 return save(filename,
"esps",evaluate_ff);
247 outf <<
"Relation " <<
name() <<
" ; ";
250 save_items(p_head,outf,contents,nodenames,node_count);
251 outf <<
"End_of_Relation" << endl;
255 EST_write_status EST_Relation::save_items(
EST_Item *node,
259 int &node_count)
const
268 myname = node_count++;
278 save_items(n->down(),outf,cnames,nodenames,node_count);
279 outf << nodenames.val(n) <<
" " <<
280 (n->contents() == 0 ? 0 : cnames.val(n->contents())) <<
" " <<
281 (n->up() == 0 ? 0 : nodenames.val(n->up())) <<
" " <<
282 (n->down() == 0 ? 0 : nodenames.val(n->down())) <<
" " <<
283 (n->next() == 0 ? 0 : nodenames.val(n->next())) <<
" " <<
284 (n->prev() == 0 ? 0 : nodenames.val(n->prev())) << endl;
295 if (ts.
get() !=
"Relation")
298 " no new Relation" << endl;
299 return misc_read_error;
301 p_name = ts.
get().string();
305 " semicolon missing after Relation name \"" <<
306 p_name <<
"\"" << endl;
307 return misc_read_error;
309 if (
f.
load(ts) != format_ok)
310 return misc_read_error;
311 if (load_items(ts,contents) != format_ok)
312 return misc_read_error;
322 if (ts.
get() !=
"Relation")
325 " no new Relation" << endl;
326 return misc_read_error;
328 p_name = ts.
get().string();
332 " semicolon missing after Relation name \"" <<
333 p_name <<
"\"" << endl;
334 return misc_read_error;
336 if (
f.
load(ts) != format_ok)
337 return misc_read_error;
338 if (load_items(ts,contents) != format_ok)
339 return misc_read_error;
344 void EST_Relation::node_tidy_up_val(
int &k,
EST_Val &v)
357 void EST_Relation::node_tidy_up(
int &k,
EST_Item *node)
378 EST_read_status r = format_ok;
383 while (ts.
peek() !=
"End_of_Relation")
385 int name = atoi(ts.
get().string());
388 node = get_item_from_name(nodenames,name);
390 EST_error(
"Unknown item %d", name);
399 siname = atoi(ts.
get().string());
407 " node's item contents" << siname <<
" doesn't exist\n";
412 node->set_contents(icontent(v));
415 node->u = get_item_from_name(nodenames,atoi(ts.
get().string()));
416 node->d = get_item_from_name(nodenames,atoi(ts.
get().string()));
417 node->n = get_item_from_name(nodenames,atoi(ts.
get().string()));
418 node->p = get_item_from_name(nodenames,atoi(ts.
get().string()));
425 int numlinks = atoi(ts.
get().string());
427 for (
int i=0;i<numlinks;++i)
429 EST_Item * item = get_item_from_name(nodenames,atoi(ts.
get().string()));
430 node->link_feats.
set_val(
"link" + itoString(i),est_val(item));
442 p_head = get_item_from_name(nodenames,1);
443 p_tail = p_head->last();
444 if (!p_head->verify())
447 " nodes do not form consistent graph" << endl;
459 nodenames.map(node_tidy_up_val);
476 EST_read_status r = format_ok;
481 while (ts.
peek() !=
"End_of_Relation")
483 int name = atoi(ts.
get().string());
486 node = get_item_from_name(nodenames,name);
488 EST_error(
"Unknown item %d", name);
497 siname = atoi(ts.
get().string());
504 " node's stream item " << siname <<
" doesn't exist\n";
509 node->set_contents(c);
512 node->u = get_item_from_name(nodenames,atoi(ts.
get().string()));
513 node->d = get_item_from_name(nodenames,atoi(ts.
get().string()));
514 node->n = get_item_from_name(nodenames,atoi(ts.
get().string()));
515 node->p = get_item_from_name(nodenames,atoi(ts.
get().string()));
521 int numlinks = atoi(ts.
get().string());
523 for (
int i=0;i<numlinks;++i)
525 EST_Item * item = get_item_from_name(nodenames,atoi(ts.
get().string()));
537 p_head = get_item_from_name(nodenames,1);
538 p_tail = p_head->last();
539 if (!p_head->verify())
542 " nodes do not form consistent graph" << endl;
552 for(
int ni=0; ni<nodenames.length(); ni++)
556 node_tidy_up(ni, node);
576 nodenames.
add_item(name,est_val(node));
592 if (name >= nodenames.
length())
594 nodenames.
resize(name*2, 1);
601 nodenames[
name] = node;
613 f.
set(
"filename",filename);
616 r = load_esps_label(ts,*
this);
617 else if (type ==
"ogi")
618 r = load_ogi_label(ts,*
this);
619 else if (type ==
"htk")
620 r = load_sample_label(ts,*
this,10000000);
621 else if ((type ==
"ascii") || (type ==
"timit"))
622 r = load_sample_label(ts,*
this,1);
623 else if (type ==
"words")
624 r = load_words_label(ts,*
this);
626 r = load_esps_label(ts,*
this);
638 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
640 cerr <<
"load_relation: can't open relation input file "
642 return misc_read_error;
644 r =
load(filename, ts, type);
656 for (n = h->first_leaf(); n != 0; n=n->next_leaf())
661 EST_Item *EST_Relation::first_leaf()
const
663 return head()->first_leaf();
666 EST_Item *EST_Relation::last_leaf()
const
668 return head()->last_leaf();
692 ostream& operator << (ostream &s,
const EST_Relation &a)