399 int32 pip, silpen, fillpen;
401 dag = ckd_calloc(1,
sizeof(*dag));
410 dag->
dict = dict_init(NULL, NULL);
411 dag->
lmath = logmath_init(1.0001, 0, FALSE);
423 E_INFO(
"Reading DAG file: %s\n", file);
424 if ((fp = fopen_compchk(file, &ispipe)) == NULL) {
425 E_ERROR_SYSTEM(
"Failed to open DAG file '%s' for reading", file);
428 line = lineiter_start(fp);
432 E_ERROR(
"Premature EOF(%s)\n", file);
435 if (strncmp(line->buf,
"# getcwd: ", 10) != 0) {
436 E_ERROR(
"%s does not begin with '# getcwd: '\n%s", file, line->buf);
439 if ((line = lineiter_next(line)) == NULL) {
440 E_ERROR(
"Premature EOF(%s)\n", file);
443 if ((strncmp(line->buf,
"# -logbase ", 11) != 0)
444 || (sscanf(line->buf + 11,
"%lf", &lb) != 1)) {
445 E_WARN(
"%s: Cannot find -logbase in header\n", file);
449 if (dag->
lmath == NULL)
450 dag->
lmath = logmath_init(lb, 0, TRUE);
452 float32 pb = logmath_get_base(dag->
lmath);
453 if (fabs(lb - pb) >= 0.0001) {
454 E_WARN(
"Inconsistent logbases: %f vs %f: will compensate\n", lb, pb);
455 logratio = (float32)(log(lb) / log(pb));
456 E_INFO(
"Lattice log ratio: %f\n", logratio);
460 dag->
n_frames = dag_param_read(line,
"Frames");
462 E_ERROR(
"Frames parameter missing or invalid\n");
466 n_nodes = dag_param_read(line,
"Nodes");
468 E_ERROR(
"Nodes parameter missing or invalid\n");
473 darray = ckd_calloc(n_nodes,
sizeof(*darray));
474 for (i = 0; i < n_nodes; i++) {
477 int seqid, sf, fef, lef;
480 if ((line = lineiter_next(line)) == NULL) {
481 E_ERROR(
"Premature EOF while loading Nodes(%s)\n", file);
486 sscanf(line->buf,
"%d %255s %d %d %d", &seqid, wd, &sf, &fef,
488 E_ERROR(
"Cannot parse line: %s, value of count %d\n", line->buf, k);
492 w = dict_wordid(dag->
dict, wd);
494 if (dag->
search == NULL) {
495 char *ww = ckd_salloc(wd);
496 if (dict_word2basestr(ww) != -1) {
498 dict_add_word(dag->
dict, ww, NULL, 0);
501 w = dict_add_word(dag->
dict, wd, NULL, 0);
504 E_ERROR(
"Unknown word in line: %s\n", line->buf);
510 E_ERROR(
"Seqno error: %s\n", line->buf);
534 k = dag_param_read(line,
"Initial");
535 if ((k < 0) || (k >= n_nodes)) {
536 E_ERROR(
"Initial node parameter missing or invalid\n");
539 dag->
start = darray[k];
542 k = dag_param_read(line,
"Final");
543 if ((k < 0) || (k >= n_nodes)) {
544 E_ERROR(
"Final node parameter missing or invalid\n");
547 dag->
end = darray[k];
550 if ((k = dag_param_read(line,
"BestSegAscr")) < 0) {
551 E_ERROR(
"BestSegAscr parameter missing\n");
554 for (i = 0; i < k; i++) {
555 if ((line = lineiter_next(line)) == NULL) {
556 E_ERROR(
"Premature EOF while (%s) ignoring BestSegAscr\n",
563 while ((line = lineiter_next(line)) != NULL) {
564 if (line->buf[0] ==
'#')
566 if (0 == strncmp(line->buf,
"Edges", 5))
570 E_ERROR(
"Edges missing\n");
573 while ((line = lineiter_next(line)) != NULL) {
577 if (sscanf(line->buf,
"%d %d %d", &from, &to, &ascr) != 3)
583 if (logratio != 1.0f)
584 ascr = (int32)(ascr * logratio);
587 if (strcmp(line->buf,
"End\n") != 0) {
588 E_ERROR(
"Terminating 'End' missing\n");
592 fclose_comp(fp, ispipe);
598 if (dict_filler_word(dag->
dict, dag->
end->
wid))
600 ? ps_search_finish_wid(dag->
search)
601 : dict_wordid(dag->
dict, S3_FINISH_WORD);
604 dag_mark_reachable(dag->
end);