53 int acount=0,ncount=0;
61 outf =
new ofstream(filename);
65 cerr <<
"lattice save: can't open lattice output file \"" 66 << filename <<
"\"" << endl;
71 for (n_ptr = lattice.
nodes.head(); n_ptr != 0; n_ptr = n_ptr->
next()){
73 for (a_ptr = lattice.
nodes(n_ptr)->arcs_out.head();
74 a_ptr != 0; a_ptr = a_ptr->
next())
79 *outf <<
"# " <<
"Generated by Edinburgh Speech Tools" << endl <<
"#" << endl;
80 *outf <<
"# Header" << endl;
81 *outf <<
"VERSION=1.1" << endl <<
"#" << endl;
82 *outf <<
"# Size line" << endl;
83 *outf <<
"N=" << ncount <<
" L=" << acount << endl;
94 *outf <<
"# Nodes" << endl;
96 *outf <<
"# Arcs" << endl;
100 for (n_ptr = lattice.
nodes.head(); n_ptr != 0; n_ptr = n_ptr->
next()){
105 *outf <<
"I=" << from << endl;
108 for (a_ptr = lattice.
nodes(n_ptr)->arcs_out.head();
109 a_ptr != 0; a_ptr = a_ptr->
next()){
116 *outf <<
"J=" << acount++ <<
" S=" << from <<
" E=" << to
117 <<
" W=!NULL" << endl;
120 *outf <<
"J=" << acount++ <<
" S=" << from <<
" E=" << to
148 arc_t *temp_arcs =
NULL;
156 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
158 cerr <<
"Can't open lattice input file " << filename << endl;
181 str = ts.
get().string();
198 startnode=atoi(right);
211 if( (narcs>0) && (nnodes>0) ){
213 if(temp_arcs !=
NULL){
214 cerr <<
"Error in lattice file : 2 size lines found" 215 <<
" : line " << ts.
linenum() << endl;
221 temp_arcs =
new arc_t[numarcs];
222 cerr <<
"size : " << numnodes <<
" " << numarcs << endl;
225 }
else if(nodenum>=0){
228 cerr <<
"Error in lattice file at line " 234 if(nodenum>=numnodes){
235 cerr <<
"Error in lattice file at line " 237 <<
" : node index (" << nodenum <<
") out of range" 247 cerr <<
"Error in lattice file at line " 254 cerr <<
"Error in lattice file at line " 256 <<
" : arc index (" << arcnum <<
") out of range" 262 if((startnode<0) || (startnode>=numnodes)){
263 cerr <<
"Error in lattice file at line " << ts.
linenum()
265 <<
" arc starts at out of range node " 266 << startnode << endl;
270 if((endnode<0) || (endnode>=numnodes)){
271 cerr <<
"Error in lattice file at line " << ts.
linenum()
273 <<
" arc ends at out of range node " 279 temp_arcs[arcindex].start = startnode;
280 temp_arcs[arcindex].end = endnode;
281 temp_arcs[arcindex].logprob = logprob;
282 temp_arcs[arcindex].word = word;
300 if(arcindex != numarcs){
301 cerr <<
"Error in lattice file at line " 302 <<
"found " << arcindex <<
" arcs, but expected " 307 if(nodeindex != numnodes){
308 cerr <<
"Error in lattice file at line " 309 <<
"found " << nodeindex <<
" nodes, but expected " 319 for(i=0;i<numarcs;i++){
321 if(seen_before.
lookup(temp_arcs[i].word) != (
void *)(1)){
322 seen_before.
add(temp_arcs[i].word,(
void *)(1));
323 list_nmap.
append(temp_arcs[i].word);
333 for(l_ptr=list_nmap.
head();l_ptr != 0; l_ptr=l_ptr->
next())
339 for(l_ptr=list_nmap.
head();l_ptr != 0; l_ptr=l_ptr->
next())
340 lattice.
nmap[i++] = list_nmap(l_ptr);
343 cerr <<
"Built nmap with " << i <<
" entries" << endl;
351 float error_margin = 1.0e-02;
353 for(i=0;i<numarcs;i++){
356 for(l_ptr=list_qmap.
head();l_ptr != 0; l_ptr=l_ptr->
next())
357 if(fabs(list_qmap(l_ptr) - temp_arcs[i].logprob) <= error_margin){
363 list_qmap.
append(temp_arcs[i].logprob);
369 for(l_ptr=list_qmap.
head();l_ptr != 0; l_ptr=l_ptr->
next())
370 if(fabs(list_qmap(l_ptr)) <= error_margin){
381 for(l_ptr=list_qmap.
head();l_ptr != 0; l_ptr=l_ptr->
next())
387 for(l_ptr=list_qmap.
head();l_ptr != 0; l_ptr=l_ptr->
next())
388 lattice.
qmap[i++] = list_qmap(l_ptr);
391 cerr <<
"Built qmap with " << i <<
" entries" << endl;
398 int nl = lattice.
nmap.
n();
399 int ql = lattice.
qmap.
n();
400 used =
new bool*[nl];
402 used[i] =
new bool[ql];
409 for(i=0;i<numarcs;i++){
437 cerr <<
"Alphabet has " << count <<
" symbols " << endl;
442 for(i=0;i<numnodes;i++){
445 lattice.
nodes.append(new_node);
451 for(j=0;j<numarcs;j++){
457 for (n_ptr =lattice. nodes.head(),count=0;
458 count<temp_arcs[j].start;
459 n_ptr = n_ptr->
next(),count++){
462 cerr <<
"Couldn't find 'from' node ";
463 for (i=0;i<lattice.
alphabet.length();i++)
472 if(from_node ==
NULL){
473 cerr <<
"Couldn't find from node, index " 474 << temp_arcs[j].start << endl;
479 for (n_ptr = lattice.
nodes.head(),count=0;
480 count<temp_arcs[j].end;
481 n_ptr = n_ptr->
next(),count++){
484 cerr <<
"Couldn't find 'to' node ";
492 cerr <<
"Couldn't find to node, index " 493 << temp_arcs[j].end << endl;
504 cerr <<
"Couldn't lookup symbol in alphabet !" << endl;
509 new_arc->
label = symbol;
510 new_arc->
to = to_node;
515 from_node->
arcs_out.append(new_arc);
520 for (n_ptr = lattice.
nodes.head(),count=0;
522 n_ptr = n_ptr->
next()){
524 if(lattice.
nodes(n_ptr)->arcs_out.head() ==
NULL){
530 cerr <<
"found " << count <<
" final nodes" << endl;
534 cerr <<
"Lattice loaded !" << endl;
symbol_t * alphabet_index_to_symbol(int index)
EST_TokenStream & get(EST_Token &t)
get next token in stream
float end(const EST_Item &item)
void qsort(EST_TList< T > &a)
int contains(const char *s, ssize_t pos=-1) const
Does it contain this substring?
EST_String nmap_index_to_name(int index)
int alphabet_index_lookup(int nmap_index, int qmap_index)
STATIC void left(STATUS Change)
float qmap_index_to_value(int index)
void add(const EST_String &key, void *item)
Add item indexed by key, overwriting previous contents.
EST_TList< Node * > final_nodes
bool save(Lattice &lattice, EST_String filename)
void close(void)
Close stream.
STATIC void right(STATUS Change)
EST_TVector< EST_String > nmap
bool load(Lattice &lattice, EST_String filename)
int qmap_value_to_index(float value)
int open(const EST_String &filename)
open a EST_TokenStream for a file.
void resize(ssize_t n, int set=1)
int linenum(void) const
returns line number of EST_TokenStream
EST_TVector< symbol_t > alphabet
int nmap_name_to_index(const EST_String &name)
void append(const T &item)
add item onto end of list
float start(const EST_Item &item)
EST_TList< Arc * > arcs_out
A string tree index class for indexing arbitrary objects by strings of characters.
EST_String after(int pos, int len=1) const
Part after pos+len.
EST_String before(int pos, int len=0) const
Part before position.
INLINE ssize_t n() const
number of items in vector.
EST_TList< Node * > nodes
void * lookup(const EST_String &key) const
Find contents index by key, 0 if there is not contents.
void clear(void)
remove all items in list
void resize(int n, int set=1)
resize vector