1 /* FILE: netw_dump.cpp 2 * DATE MODIFIED: 31-Aug-07 3 * DESCRIPTION: Part of the SREC graph compiler project source files. 4 * 5 * Copyright 2007, 2008 Nuance Communciations, Inc. * 6 * * 7 * Licensed under the Apache License, Version 2.0 (the 'License'); * 8 * you may not use this file except in compliance with the License. * 9 * * 10 * You may obtain a copy of the License at * 11 * http://www.apache.org/licenses/LICENSE-2.0 * 12 * * 13 * Unless required by applicable law or agreed to in writing, software * 14 * distributed under the License is distributed on an 'AS IS' BASIS, * 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 16 * See the License for the specific language governing permissions and * 17 * limitations under the License. * 18 * * 19 *---------------------------------------------------------------------------*/ 20 21 #include <fstream> 22 #include <iostream> 23 #include <sstream> 24 25 #include <string> 26 27 #include "netw_arc.h" 28 #include "sub_grph.h" 29 30 #include "grxmldoc.h" 31 32 void SubGraph::RemapForSortedOutput ( GRXMLDoc &p_Doc ) 33 { 34 int origIndex, sortedIndex; 35 36 for (int ii= 0; ii < numArc; ii++) { 37 origIndex= arc[ii]->GetInput(); 38 if (origIndex >= 0 && p_Doc.findSortedLabelIndex (origIndex, sortedIndex )) 39 arc[ii]->AssignInput (sortedIndex); 40 } 41 return; 42 } 43 44 void SubGraph::WriteForwardGraphFile( std::string & fileName, GRXMLDoc &p_Doc ) 45 { 46 // Creates file of forward graph - the {name}.G.txt file 47 int loc; 48 std::string inLabel; 49 std::ofstream outfile; 50 std::string label; 51 std::string separator = "\t"; 52 std::string Eps = "eps"; 53 54 RemapForSortedOutput (p_Doc); 55 SortLanguageForMin (); 56 p_Doc.sortLabels(); 57 outfile.open ( fileName.c_str() ); 58 for (int ii= 0; ii < numArc; ii++) { 59 inLabel=""; 60 loc = forwardList[ii]; 61 switch ( arc[loc]->inputLabel ) { 62 case TERMINAL_LABEL: // Terminal transition 63 outfile << arc[loc]->fromId << std::endl; 64 break; 65 case NONE_LABEL: // Null transition 66 outfile << arc[loc]->fromId << separator << arc[loc]->toId << separator << Eps << std::endl; 67 break; 68 case TAG_LABEL: // Tag transition 69 case WB_LABEL: // Word boundary transition 70 case BEGINSCOPE_LABEL: // Start of scope 71 case ENDSCOPE_LABEL: // End of scope 72 case BEGINRULE_LABEL: // Start of rule 73 case ENDRULE_LABEL: // End of rule 74 case DISCARD_LABEL: // Discard (internal) 75 break; 76 default: 77 { 78 // if (!p_Doc.findLabel( arc[loc]->inputLabel, inLabel ) ) { 79 if (!p_Doc.findSortedLabel( arc[loc]->inputLabel, inLabel ) ) { 80 std::stringstream ss; 81 ss << arc[loc]->inputLabel; 82 inLabel = ss.str(); 83 } 84 outfile << arc[loc]->fromId << separator << arc[loc]->toId << separator << inLabel.c_str() << std::endl; 85 } 86 break; 87 } // switch 88 89 } 90 outfile.close(); 91 92 SortLanguage (); 93 return; 94 } 95 96 97 void SubGraph::WriteForwardGraphWithSemantic ( std::string & fileName, GRXMLDoc &p_Doc ) 98 { 99 int loc; 100 std::string inLabel, outLabel; 101 std::string tag; 102 std::string Eps = "eps"; 103 std::string OpenBrace = "{"; 104 std::string CloseBrace = "}"; 105 std::string Separator ="\t"; 106 107 std::ofstream outfile; 108 std::string label; 109 std::string separator = "\t"; 110 outfile.open ( fileName.c_str() ); 111 112 RemapForSortedOutput (p_Doc); 113 SortLanguageForMin (); 114 p_Doc.sortLabels(); 115 for ( int ii= 0; ii < numArc; ii++ ) { 116 loc= forwardList[ii]; 117 inLabel = ""; 118 switch ( arc[loc]->inputLabel ) { 119 case BEGINRULE_LABEL: // Start of rule 120 inLabel = Eps; 121 outLabel = OpenBrace; 122 break; 123 case ENDRULE_LABEL: // End of rule 124 { 125 inLabel = Eps; 126 if (!p_Doc.findRule( arc[loc]->outputLabel, outLabel ) ) { 127 std::stringstream ss; 128 ss << arc[loc]->outputLabel; 129 outLabel = "(" + ss.str() + ")"; 130 } 131 outLabel = outLabel + CloseBrace; 132 } 133 break; 134 case NONE_LABEL: // Null transition 135 inLabel = Eps; 136 outLabel= Eps; 137 break; 138 case TAG_LABEL: // Tag transition 139 inLabel = Eps; 140 { 141 std::stringstream ss; 142 ss << SCRIPT_LABEL_PREFIX << arc[loc]->outputLabel; 143 outLabel = ss.str(); 144 } 145 break; 146 case TERMINAL_LABEL: // Terminal transition 147 outLabel = ""; 148 break; 149 case WB_LABEL: // Word boundary transition 150 case BEGINSCOPE_LABEL: // Start of scope 151 case ENDSCOPE_LABEL: // End of scope 152 case DISCARD_LABEL: // Discard (internal) 153 break; 154 default: 155 // Input label 156 // if (!p_Doc.findLabel( arc[loc]->inputLabel, inLabel ) ) { 157 if (!p_Doc.findSortedLabel( arc[loc]->inputLabel, inLabel ) ) { 158 inLabel = arc[loc]->inputLabel; 159 } 160 161 // Output label 162 if (arc[loc]->outputLabel == -1) 163 outLabel= Eps; 164 else { 165 std::stringstream ss; 166 ss << SCRIPT_LABEL_PREFIX << arc[loc]->outputLabel; 167 outLabel = ss.str(); 168 } 169 break; 170 } 171 if ( outLabel.empty() ) 172 outfile << arc[loc]->fromId << std::endl; 173 else { 174 outfile << arc[loc]->fromId << Separator << arc[loc]->toId << Separator << inLabel.c_str() << Separator << outLabel.c_str() << std::endl; 175 p_Doc.addOLabelToOList( outLabel); 176 } 177 } 178 outfile.close(); 179 180 return; 181 } 182 183 void SubGraph::WriteHMMGraphFile( std::string & fileName, GRXMLDoc &p_Doc ) 184 { 185 // Creates file of forward graph - the {name}.G.txt file 186 int loc; 187 std::string inLabel; 188 std::string outLabel; 189 std::string phLabel; 190 std::string metaname; 191 std::string wtw; 192 std::ofstream outfile; 193 std::string label; 194 std::string separator = "\t"; 195 std::string Eps = "eps"; 196 std::string Pau = "-pau-"; 197 std::string Pau2 = "-pau2-"; 198 bool bRes; 199 200 metaname = "word_penalty"; 201 bRes = p_Doc.findMeta(metaname, wtw); 202 203 outfile.open ( fileName.c_str() ); 204 for (int ii= 0; ii < numArc; ii++) { 205 inLabel=""; 206 loc = forwardList[ii]; 207 switch ( arc[loc]->inputLabel ) { 208 case TERMINAL_LABEL: // Terminal transition 209 outfile << arc[loc]->fromId << std::endl; 210 break; 211 case NONE_LABEL: // Null transition 212 if (arc[loc]->outputLabel >= 0) { 213 if (!p_Doc.findLabel( arc[loc]->outputLabel, outLabel ) ) { 214 std::stringstream ss; 215 ss << arc[loc]->outputLabel; 216 outLabel = ss.str(); 217 } 218 outfile << arc[loc]->fromId << separator << arc[loc]->toId << separator << Eps << separator << outLabel.c_str() << std::endl; 219 } 220 else 221 outfile << arc[loc]->fromId << separator << arc[loc]->toId << separator << Eps << separator << Eps << std::endl; 222 break; 223 case WB_LABEL: // Word boundary transition 224 if (arc[loc]->outputLabel >= 0) { 225 if (!p_Doc.findLabel( arc[loc]->outputLabel, outLabel ) ) { 226 std::stringstream ss; 227 ss << arc[loc]->outputLabel; 228 outLabel = ss.str(); 229 } 230 outfile << arc[loc]->fromId << separator << arc[loc]->toId << separator << ".wb" << separator << outLabel.c_str() << std::endl; 231 } 232 else 233 outfile << arc[loc]->fromId << separator << arc[loc]->toId << separator << ".wb" << separator << Eps << std::endl; 234 break; 235 case TAG_LABEL: // Tag transition 236 case BEGINSCOPE_LABEL: // Start of scope 237 case ENDSCOPE_LABEL: // End of scope 238 case BEGINRULE_LABEL: // Start of rule 239 case ENDRULE_LABEL: // End of rule 240 case DISCARD_LABEL: // Discard (internal) 241 break; 242 default: 243 { 244 if (arc[loc]->outputLabel >= 0) { 245 if (!p_Doc.findLabel( arc[loc]->outputLabel, outLabel ) ) { 246 std::stringstream ss; 247 ss << arc[loc]->outputLabel; 248 outLabel = ss.str(); 249 } 250 } 251 else if (arc[loc]->outputLabel == INITIAL_LABEL) 252 outLabel= Pau; 253 else if (arc[loc]->outputLabel == FINAL_LABEL) 254 outLabel= Pau2; 255 else 256 outLabel= Eps; 257 } 258 break; 259 } // switch 260 261 } 262 outfile.close(); 263 return; 264 } 265 266 void SubGraph::WritePhonemeGraphFile( std::string & fileName, GRXMLDoc &p_Doc ) 267 { 268 // Creates file of forward graph - the {name}.G.txt file 269 int loc; 270 std::string inLabel; 271 std::string outLabel; 272 std::ofstream outfile; 273 std::string label; 274 std::string separator = "\t"; 275 std::string Eps = "eps"; 276 277 outfile.open ( fileName.c_str() ); 278 for (int ii= 0; ii < numArc; ii++) { 279 inLabel=""; 280 loc = forwardList[ii]; 281 switch ( arc[loc]->inputLabel ) { 282 case TERMINAL_LABEL: // Terminal transition 283 outfile << arc[loc]->fromId << std::endl; 284 break; 285 case NONE_LABEL: // Null transition 286 if (arc[loc]->outputLabel >= 0) { 287 if (!p_Doc.findLabel( arc[loc]->outputLabel, outLabel ) ) { 288 std::stringstream ss; 289 ss << arc[loc]->outputLabel; 290 outLabel = ss.str(); 291 } 292 outfile << arc[loc]->fromId << separator << arc[loc]->toId << separator << Eps << separator << outLabel.c_str() << std::endl; 293 } 294 else 295 outfile << arc[loc]->fromId << separator << arc[loc]->toId << separator << Eps << separator << Eps << std::endl; 296 break; 297 case WB_LABEL: // Word boundary transition 298 if (arc[loc]->outputLabel >= 0) { 299 if (!p_Doc.findLabel( arc[loc]->outputLabel, outLabel ) ) { 300 std::stringstream ss; 301 ss << arc[loc]->outputLabel; 302 outLabel = ss.str(); 303 } 304 outfile << arc[loc]->fromId << separator << arc[loc]->toId << separator << ".wb" << separator << outLabel.c_str() << std::endl; 305 } 306 else 307 outfile << arc[loc]->fromId << separator << arc[loc]->toId << separator << ".wb" << separator << Eps << std::endl; 308 break; 309 case TAG_LABEL: // Tag transition 310 case BEGINSCOPE_LABEL: // Start of scope 311 case ENDSCOPE_LABEL: // End of scope 312 case BEGINRULE_LABEL: // Start of rule 313 case ENDRULE_LABEL: // End of rule 314 case DISCARD_LABEL: // Discard (internal) 315 break; 316 default: 317 if ( arc[loc]->inputLabel >= 0) { 318 319 } 320 else { 321 // Note negative index 322 if (!p_Doc.findLabel( -arc[loc]->inputLabel, inLabel ) ) { 323 std::stringstream ss; 324 ss << arc[loc]->inputLabel; 325 inLabel = ss.str(); 326 } 327 outfile << arc[loc]->fromId << separator << arc[loc]->toId << separator << inLabel.c_str() << separator << Eps << std::endl; 328 } 329 break; 330 } // switch 331 332 } 333 outfile.close(); 334 335 return; 336 } 337 338 339 void SubGraph::PrintWithLabels( GRXMLDoc &p_Doc ) 340 { 341 int loc; 342 std::string inLabel, outLabel; 343 std::string label; 344 345 printf ("Graph %s (%d %d)\n", title, startId, lastId); 346 for (int ii= 0; ii < numArc; ii++) { 347 loc= forwardList[ii]; 348 if (!p_Doc.findLabel( arc[loc]->inputLabel, inLabel ) ) { 349 inLabel = arc[loc]->inputLabel; 350 //std::stringstream ss; 351 //ss << arc[loc]->inputLabel; 352 //inLabel = ss.str(); 353 } 354 if (!p_Doc.findTag( arc[loc]->outputLabel, outLabel ) ) { 355 outLabel = arc[loc]->outputLabel; 356 //std::stringstream ss; 357 //ss << arc[loc]->outputLabel; 358 //outLabel = ss.str(); 359 } 360 std::cout << arc[loc]->fromId <<" " << arc[loc]->toId << " " << inLabel.c_str() <<" " << outLabel.c_str() << std::endl; 361 } 362 363 return; 364 } 365 366 void NUANArc::Dump(GRXMLDoc &p_Doc ) 367 { 368 // I need a handle to the grxml doc object in order to access labels 369 printf ("%d %d %d %d\n", fromId, toId, inputLabel, outputLabel); 370 return; 371 } 372 373 374 375