42 #include "EST_lattice.h"
43 #include "EST_types.h"
44 #include "EST_Token.h"
45 #include "EST_StringTrie.h"
51 int acount=0,ncount=0;
59 outf =
new ofstream(filename);
63 cerr <<
"lattice save: can't open lattice output file \""
64 << filename <<
"\"" << endl;
69 for (n_ptr = lattice.nodes.head(); n_ptr != 0; n_ptr = n_ptr->next()){
71 for (a_ptr = lattice.nodes(n_ptr)->arcs_out.head();
72 a_ptr != 0; a_ptr = a_ptr->next())
77 *outf <<
"# " <<
"Generated by Edinburgh Speech Tools" << endl <<
"#" << endl;
78 *outf <<
"# Header" << endl;
79 *outf <<
"VERSION=1.1" << endl <<
"#" << endl;
80 *outf <<
"# Size line" << endl;
81 *outf <<
"N=" << ncount <<
" L=" << acount << endl;
92 *outf <<
"# Nodes" << endl;
94 *outf <<
"# Arcs" << endl;
99 for (n_ptr = lattice.nodes.head(); n_ptr != 0; n_ptr = n_ptr->next()){
101 from=lattice.node_index(lattice.nodes(n_ptr));
104 *outf <<
"I=" << from << endl;
107 for (a_ptr = lattice.nodes(n_ptr)->arcs_out.head();
108 a_ptr != 0; a_ptr = a_ptr->next()){
110 to = lattice.node_index(lattice.nodes(n_ptr)->arcs_out(a_ptr)->to);
112 symbol = lattice.alphabet_index_to_symbol(lattice.nodes(n_ptr)->arcs_out(a_ptr)->label);
114 if(lattice.nodes(n_ptr)->arcs_out(a_ptr)->label == lattice.e_move_symbol_index){
115 *outf <<
"J=" << acount++ <<
" S=" << from <<
" E=" << to
116 <<
" W=!NULL" << endl;
119 *outf <<
"J=" << acount++ <<
" S=" << from <<
" E=" << to
120 <<
" l=" << lattice.qmap_index_to_value(symbol->qmap_index)
121 <<
" W=" << lattice.nmap_index_to_name(symbol->nmap_index)
147 arc_t *temp_arcs = NULL;
155 if (((filename ==
"-") ? ts.
open(cin) : ts.
open(filename)) != 0)
157 cerr <<
"Can't open lattice input file " << filename << endl;
180 str = ts.
get().string();
197 startnode=atoi(right);
210 if( (narcs>0) && (nnodes>0) ){
212 if(temp_arcs != NULL){
213 cerr <<
"Error in lattice file : 2 size lines found"
214 <<
" : line " << ts.
linenum() << endl;
220 temp_arcs =
new arc_t[numarcs];
221 cerr <<
"size : " << numnodes <<
" " << numarcs << endl;
224 }
else if(nodenum>=0){
227 cerr <<
"Error in lattice file at line "
233 if(nodenum>=numnodes){
234 cerr <<
"Error in lattice file at line "
236 <<
" : node index (" << nodenum <<
") out of range"
246 cerr <<
"Error in lattice file at line "
253 cerr <<
"Error in lattice file at line "
255 <<
" : arc index (" << arcnum <<
") out of range"
261 if((startnode<0) || (startnode>=numnodes)){
262 cerr <<
"Error in lattice file at line " << ts.
linenum()
264 <<
" arc starts at out of range node "
265 << startnode << endl;
269 if((endnode<0) || (endnode>=numnodes)){
270 cerr <<
"Error in lattice file at line " << ts.
linenum()
272 <<
" arc ends at out of range node "
278 temp_arcs[arcindex].start = startnode;
279 temp_arcs[arcindex].end = endnode;
280 temp_arcs[arcindex].logprob = logprob;
281 temp_arcs[arcindex].word = word;
299 if(arcindex != numarcs){
300 cerr <<
"Error in lattice file at line "
301 <<
"found " << arcindex <<
" arcs, but expected "
306 if(nodeindex != numnodes){
307 cerr <<
"Error in lattice file at line "
308 <<
"found " << nodeindex <<
" nodes, but expected "
318 for(i=0;i<numarcs;i++){
320 if(seen_before.
lookup(temp_arcs[i].word) != (
void *)(1)){
321 seen_before.
add(temp_arcs[i].word,(
void *)(1));
322 list_nmap.
append(temp_arcs[i].word);
332 for(l_ptr=list_nmap.head();l_ptr != 0; l_ptr=l_ptr->next())
338 for(l_ptr=list_nmap.head();l_ptr != 0; l_ptr=l_ptr->next())
339 lattice.nmap[i++] = list_nmap(l_ptr);
342 cerr <<
"Built nmap with " << i <<
" entries" << endl;
350 float error_margin = 1.0e-02;
352 for(i=0;i<numarcs;i++){
355 for(l_ptr=list_qmap.head();l_ptr != 0; l_ptr=l_ptr->next())
356 if(fabs(list_qmap(l_ptr) - temp_arcs[i].logprob) <= error_margin){
362 list_qmap.
append(temp_arcs[i].logprob);
368 for(l_ptr=list_qmap.head();l_ptr != 0; l_ptr=l_ptr->next())
369 if(fabs(list_qmap(l_ptr)) <= error_margin){
380 for(l_ptr=list_qmap.head();l_ptr != 0; l_ptr=l_ptr->next())
386 for(l_ptr=list_qmap.head();l_ptr != 0; l_ptr=l_ptr->next())
387 lattice.qmap[i++] = list_qmap(l_ptr);
390 cerr <<
"Built qmap with " << i <<
" entries" << endl;
397 int nl = lattice.nmap.
n();
398 int ql = lattice.qmap.
n();
399 used =
new bool*[nl];
401 used[i] =
new bool[ql];
408 for(i=0;i<numarcs;i++){
413 used[lattice.nmap_name_to_index(temp_arcs[i].word)][lattice.qmap_value_to_index(temp_arcs[i].logprob)] =
true;
422 lattice.alphabet.resize(count);
432 lattice.alphabet[count++] = *sym;
436 cerr <<
"Alphabet has " << count <<
" symbols " << endl;
441 for(i=0;i<numnodes;i++){
444 lattice.nodes.append(new_node);
450 for(j=0;j<numarcs;j++){
456 for (n_ptr =lattice. nodes.head(),count=0;
457 count<temp_arcs[j].start;
458 n_ptr = n_ptr->next(),count++){
461 cerr <<
"Couldn't find 'from' node ";
468 if(from_node == NULL){
469 cerr <<
"Couldn't find from node, index "
470 << temp_arcs[j].start << endl;
475 for (n_ptr = lattice.nodes.head(),count=0;
476 count<temp_arcs[j].end;
477 n_ptr = n_ptr->next(),count++){
480 cerr <<
"Couldn't find 'to' node ";
488 cerr <<
"Couldn't find to node, index "
489 << temp_arcs[j].end << endl;
494 int word_index = lattice.nmap_name_to_index(temp_arcs[j].word);
497 int symbol = lattice.alphabet_index_lookup(word_index,
498 lattice.qmap_value_to_index(temp_arcs[j].logprob));
500 cerr <<
"Couldn't lookup symbol in alphabet !" << endl;
505 new_arc->label = symbol;
506 new_arc->to = to_node;
508 if(to_node->name.head() == NULL)
509 to_node->name.
append(word_index);
511 from_node->arcs_out.append(new_arc);
516 for (n_ptr = lattice.nodes.head(),count=0;
518 n_ptr = n_ptr->next()){
520 if(lattice.nodes(n_ptr)->arcs_out.head() == NULL){
521 lattice.final_nodes.append(lattice.nodes(n_ptr));
526 cerr <<
"found " << count <<
" final nodes" << endl;
530 cerr <<
"Lattice loaded !" << endl;