Exemplo n.º 1
0
int
init(model_def_t **out_imdef,
     pset_t **out_pset,
     uint32 *out_n_pset,
     dtree_t ****out_tree,
     uint32 *out_n_seno)
{
    model_def_t *imdef;
    uint32 p, s;
    uint32 n_ci, n_state;
    char fn[MAXPATHLEN+1];
    const char *a_fn;
    FILE *fp;
    dtree_t ***tree, *tr;
    pset_t *pset;
    uint32 n_pset;
    uint32 n_seno;
    const char *treedir;
    uint32 ts_id;
    int allphones;

    a_fn = cmd_ln_str("-imoddeffn");
    if (a_fn == NULL)
	E_FATAL("Specify -imoddeffn\n");
    if (model_def_read(&imdef, a_fn) != S3_SUCCESS) {
	return S3_ERROR;
    }
    *out_imdef = imdef;

    a_fn = cmd_ln_str("-psetfn");
    E_INFO("Reading: %s\n", a_fn);
    *out_pset = pset = read_pset_file(a_fn, imdef->acmod_set, &n_pset);
    *out_n_pset = n_pset;

    allphones = cmd_ln_int32("-allphones");
    if (allphones)
      n_ci = 1;
    else
      n_ci = acmod_set_n_ci(imdef->acmod_set);

    treedir = cmd_ln_str("-treedir");
    tree = (dtree_t ***)ckd_calloc(n_ci, sizeof(dtree_t **));
    *out_tree = tree;

    ts_id = imdef->n_tied_ci_state;
    for (p = 0, n_seno = 0; p < n_ci; p++) {
	if (allphones || !acmod_set_has_attrib(imdef->acmod_set, p, "filler")) {
	    const char *pname;

	    if (allphones) {
		n_state = imdef->defn[acmod_set_n_ci(imdef->acmod_set)].n_state;
		pname = "ALLPHONES";
	    }
	    else {
		n_state = imdef->defn[p].n_state;
		pname = acmod_set_id2name(imdef->acmod_set, p);
	    }
	    tree[p] = (dtree_t **)ckd_calloc(n_state, sizeof(dtree_t *));

	    for (s = 0; s < n_state-1; s++) {
		E_INFO("%s-%u: offset %u\n",
		       pname, s, ts_id);

		sprintf(fn, "%s/%s-%u.dtree",
			treedir, pname, s);
		fp = fopen(fn, "r");
		if (fp == NULL) {
		    E_FATAL_SYSTEM("Unable to open %s for reading", fn);
		}
		tree[p][s] = tr = read_final_tree(fp, pset, n_pset);

		label_leaves(&tr->node[0], &ts_id);

		fclose(fp);

		n_seno += cnt_leaf(&tr->node[0]);
	    }
	}
    }

    assert(n_seno == (ts_id - imdef->n_tied_ci_state));

    E_INFO("n_seno= %u\n", ts_id);

    *out_n_seno = n_seno;

    return S3_SUCCESS;
}
int
s3phseg_read(const char *fn,
	     acmod_set_t *acmod_set,
	     s3phseg_t **out_phseg)
{
	FILE *fp;
	char txt[512];
	s3phseg_t *plist = NULL;
	int n;

	if ((fp = fopen(fn, "r")) == NULL) {
		E_ERROR("Failed to open phseg file %s\n", fn);
		return S3_ERROR;
	}
	/* Should be a header of column names */
	while ((n = fscanf(fp, "%511s", txt))) {
		if (n == EOF) {
			E_ERROR("Failed to read column headers from phseg file\n");
			goto error_out;
		}
		if (!strcmp(txt, "Phone"))
			break;
	}
	/* Get each line */
	while (!feof(fp)) {
		unsigned int sf, ef;
		int score;
		acmod_id_t ci, phone;
		s3phseg_t *phseg;
		char *c, *cc;

		n = fscanf(fp, "%u %u %d", &sf, &ef, &score);
		if (n < 3) /* We probably hit EOF or "Total score" */
			break;
		fgets(txt, sizeof(txt), fp);
		/* Remove newline. */
		if (txt[strlen(txt)-1] == '\n')
			txt[strlen(txt)-1] = '\0';
		/* Find the base phone. */
		cc = txt + strspn(txt, " \t");
		if ((c = strchr(cc, ' ')))
			*c = '\0';
		if ((ci = phone = acmod_set_name2id(acmod_set, txt)) == NO_ACMOD) {
			E_ERROR("Unknown CI phone (%s) in phseg file\n", txt);
			goto error_out;
		}
		/* Restore the space and find the triphone if necessary. */
		if (c) *c = ' ';
		if (acmod_set->n_multi != 0
		    && !acmod_set_has_attrib(acmod_set, phone, "filler")) {
			if ((phone = acmod_set_name2id(acmod_set, txt)) == NO_ACMOD) {
				/* This might be too verbose. */
				E_WARN("Unknown triphone (%s) in phseg file\n", txt);
				/* Back off to CI phone. */
				phone = ci;
			}
		}
		phseg = ckd_calloc(1, sizeof(*phseg));
		phseg->next = plist;
		phseg->phone = phone;
		phseg->sf = sf;
		phseg->ef = ef;
		phseg->score = score;
		plist = phseg;
	}
	fclose(fp);
	if (out_phseg) {
		s3phseg_t *next, *last = NULL;
		/* Now reverse the list. */
		while (plist) {
			next = plist->next;
			*out_phseg = plist;
			(*out_phseg)->next = last;
			last = *out_phseg;
			plist = next;
		}
	}
	else
		s3phseg_free(plist);
	return S3_SUCCESS;
error_out:
	fclose(fp);
	return S3_ERROR;
}