1
/* ====================================================================
2
* Copyright (c) 2006 Carnegie Mellon University. All rights
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
12
* 2. Redistributions in binary form must reproduce the above copyright
13
* notice, this list of conditions and the following disclaimer in
14
* the documentation and/or other materials provided with the
17
* This work was supported in part by funding from the Defense Advanced
18
* Research Projects Agency and the National Science Foundation of the
19
* United States of America, and the CMU Sphinx Speech Consortium.
21
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
22
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
25
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
* ====================================================================
36
/*********************************************************************
41
* SPHINX-III phone segmentation file I/O functions
44
* David Huggins-Daines (dhuggins@cs.cmu.edu)
45
*********************************************************************/
47
#include <s3/s3phseg_io.h>
48
#include <sphinxbase/ckd_alloc.h>
54
s3phseg_read(const char *fn,
55
acmod_set_t *acmod_set,
56
s3phseg_t **out_phseg)
60
s3phseg_t *plist = NULL;
63
if ((fp = fopen(fn, "r")) == NULL) {
64
E_ERROR("Failed to open phseg file %s\n", fn);
67
/* Should be a header of column names */
68
while ((n = fscanf(fp, "%511s", txt))) {
70
E_ERROR("Failed to read column headers from phseg file\n");
73
if (!strcmp(txt, "Phone"))
84
n = fscanf(fp, "%u %u %d", &sf, &ef, &score);
85
if (n < 3) /* We probably hit EOF or "Total score" */
87
fgets(txt, sizeof(txt), fp);
89
if (txt[strlen(txt)-1] == '\n')
90
txt[strlen(txt)-1] = '\0';
91
/* Find the base phone. */
92
cc = txt + strspn(txt, " \t");
93
if ((c = strchr(cc, ' ')))
95
if ((ci = phone = acmod_set_name2id(acmod_set, txt)) == NO_ACMOD) {
96
E_ERROR("Unknown CI phone (%s) in phseg file\n", txt);
99
/* Restore the space and find the triphone if necessary. */
101
if (acmod_set->n_multi != 0
102
&& !acmod_set_has_attrib(acmod_set, phone, "filler")) {
103
if ((phone = acmod_set_name2id(acmod_set, txt)) == NO_ACMOD) {
104
/* This might be too verbose. */
105
E_WARN("Unknown triphone (%s) in phseg file\n", txt);
106
/* Back off to CI phone. */
110
phseg = ckd_calloc(1, sizeof(*phseg));
112
phseg->phone = phone;
115
phseg->score = score;
120
s3phseg_t *next, *last = NULL;
121
/* Now reverse the list. */
125
(*out_phseg)->next = last;
139
s3phseg_write(const char *fn,
140
acmod_set_t *acmod_set,
145
if ((fp = fopen(fn, "w")) == NULL)
148
fprintf (fp, "\t%5s %5s %9s %s\n",
149
"SFrm", "EFrm", "SegAScr", "Phone");
150
for (; phseg; phseg = phseg->next) {
151
fprintf(fp, "\t%5d %5d %9d %s\n",
152
phseg->sf, phseg->ef, phseg->score,
153
acmod_set_id2name(acmod_set, phseg->phone));
161
s3phseg_free(s3phseg_t *phseg)
173
s3lattice_read(const char *fn,
174
s3lattice_t **lattice)
178
char line[1024], temp[16];
179
s3lattice_t *out_lattice;
182
if ((fp = fopen(fn, "r")) == NULL) {
183
E_ERROR("Failed to open lattice file %s\n", fn);
187
out_lattice = ckd_calloc(1, sizeof(*out_lattice));
189
/* process file head */
190
/* read the number of total arcs */
191
fgets(line, sizeof(line), fp);
192
if (strstr(line, "Total arcs") == NULL) {
193
E_ERROR("Lattice Format Error, missing Total arcs\n");
196
fgets(line, sizeof(line), fp);
197
n = sscanf(line, "%d", &out_lattice->n_arcs);
199
E_ERROR("Lattice Format Error, missing Total arcs\n");
202
if (out_lattice->n_arcs == 0) {
203
E_ERROR("No arc exits in the lattice\n");
207
/* read the number of true arcs */
208
fgets(line, sizeof(line), fp);
209
if (strstr(line, "True arcs") == NULL) {
210
E_ERROR("Lattice Format Error, missing True arcs\n");
213
fgets(line, sizeof(line), fp);
214
n = sscanf(line, "%d", &out_lattice->n_true_arcs);
216
E_ERROR("Lattice Format Error, missing True arcs\n");
219
if (out_lattice->n_true_arcs == 0) {
220
E_ERROR("No arc from the numerator lattice\n");
223
if (out_lattice->n_true_arcs > out_lattice->n_arcs) {
224
E_ERROR("The number of arcs from numerator lattice is larger than the number of total arcs\n");
228
/* read parameter lists */
229
fgets(line, sizeof(line), fp);
230
if (strstr(line, "arc_id") == NULL) {
231
E_ERROR("Lattice Format Error\n");
235
/* allocate memory for arcs */
236
out_lattice->arc = ckd_calloc(out_lattice->n_arcs, sizeof(*out_lattice->arc));
240
while (fscanf(fp, "%d", &id) != EOF) {/* arc id */
241
fscanf(fp, "%s", out_lattice->arc[i].word);/* word */
242
fscanf(fp, "%d", &out_lattice->arc[i].sf);/* start frame */
243
fscanf(fp, "%d", &out_lattice->arc[i].ef);/* end frame */
244
fscanf(fp, "%lf", &out_lattice->arc[i].lm_score);/* LM score */
245
fscanf(fp, "%d", &out_lattice->arc[i].n_prev_arcs);/* num of previous arcs */
246
fscanf(fp, "%d", &out_lattice->arc[i].n_next_arcs);/* num of succeeding arcs */
248
/* read preceding arc ids */
249
fscanf(fp, "%s", temp);/* move over '<' */
250
if (out_lattice->arc[i].n_prev_arcs == 0) {
251
E_ERROR("No preceding arc exits\n");
254
out_lattice->arc[i].prev_arcs = ckd_calloc(out_lattice->arc[i].n_prev_arcs, sizeof(int));
255
for (j=0; j<out_lattice->arc[i].n_prev_arcs; j++)
256
fscanf(fp, "%d", &out_lattice->arc[i].prev_arcs[j]);
258
/* read succeeding arc ids */
259
fscanf(fp, "%s", temp);/* move over '<' */
260
if (out_lattice->arc[i].n_next_arcs == 0) {
261
E_ERROR("No succeeding arc exits\n");
264
out_lattice->arc[i].next_arcs = ckd_calloc(out_lattice->arc[i].n_next_arcs, sizeof(int));
265
for (j=0; j<out_lattice->arc[i].n_next_arcs; j++)
266
fscanf(fp, "%d", &out_lattice->arc[i].next_arcs[j]);
272
*lattice = out_lattice;