~ubuntu-branches/ubuntu/vivid/sphinxtrain/vivid

« back to all changes in this revision

Viewing changes to src/libs/libio/s3phseg_io.c

  • Committer: Package Import Robot
  • Author(s): Samuel Thibault
  • Date: 2013-01-02 04:10:21 UTC
  • Revision ID: package-import@ubuntu.com-20130102041021-ynsizmz33fx02hea
Tags: upstream-1.0.8
ImportĀ upstreamĀ versionĀ 1.0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ====================================================================
 
2
 * Copyright (c) 2006 Carnegie Mellon University.  All rights 
 
3
 * reserved.
 
4
 *
 
5
 * Redistribution and use in source and binary forms, with or without
 
6
 * modification, are permitted provided that the following conditions
 
7
 * are met:
 
8
 *
 
9
 * 1. Redistributions of source code must retain the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer. 
 
11
 *
 
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
 
15
 *    distribution.
 
16
 *
 
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.
 
20
 *
 
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.
 
32
 *
 
33
 * ====================================================================
 
34
 *
 
35
 */
 
36
/*********************************************************************
 
37
 *
 
38
 * File: s3phseg_io.c
 
39
 * 
 
40
 * Description: 
 
41
 *     SPHINX-III phone segmentation file I/O functions
 
42
 *
 
43
 * Author: 
 
44
 *     David Huggins-Daines (dhuggins@cs.cmu.edu)
 
45
 *********************************************************************/
 
46
 
 
47
#include <s3/s3phseg_io.h>
 
48
#include <sphinxbase/ckd_alloc.h>
 
49
#include <s3/s3.h>
 
50
#include <stdio.h>
 
51
#include <string.h>
 
52
 
 
53
int
 
54
s3phseg_read(const char *fn,
 
55
             acmod_set_t *acmod_set,
 
56
             s3phseg_t **out_phseg)
 
57
{
 
58
        FILE *fp;
 
59
        char txt[512];
 
60
        s3phseg_t *plist = NULL;
 
61
        int n;
 
62
 
 
63
        if ((fp = fopen(fn, "r")) == NULL) {
 
64
                E_ERROR("Failed to open phseg file %s\n", fn);
 
65
                return S3_ERROR;
 
66
        }
 
67
        /* Should be a header of column names */
 
68
        while ((n = fscanf(fp, "%511s", txt))) {
 
69
                if (n == EOF) {
 
70
                        E_ERROR("Failed to read column headers from phseg file\n");
 
71
                        goto error_out;
 
72
                }
 
73
                if (!strcmp(txt, "Phone"))
 
74
                        break;
 
75
        }
 
76
        /* Get each line */
 
77
        while (!feof(fp)) {
 
78
                unsigned int sf, ef;
 
79
                int score;
 
80
                acmod_id_t ci, phone;
 
81
                s3phseg_t *phseg;
 
82
                char *c, *cc;
 
83
 
 
84
                n = fscanf(fp, "%u %u %d", &sf, &ef, &score);
 
85
                if (n < 3) /* We probably hit EOF or "Total score" */
 
86
                        break;
 
87
                fgets(txt, sizeof(txt), fp);
 
88
                /* Remove newline. */
 
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, ' ')))
 
94
                        *c = '\0';
 
95
                if ((ci = phone = acmod_set_name2id(acmod_set, txt)) == NO_ACMOD) {
 
96
                        E_ERROR("Unknown CI phone (%s) in phseg file\n", txt);
 
97
                        goto error_out;
 
98
                }
 
99
                /* Restore the space and find the triphone if necessary. */
 
100
                if (c) *c = ' ';
 
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. */
 
107
                                phone = ci;
 
108
                        }
 
109
                }
 
110
                phseg = ckd_calloc(1, sizeof(*phseg));
 
111
                phseg->next = plist;
 
112
                phseg->phone = phone;
 
113
                phseg->sf = sf;
 
114
                phseg->ef = ef;
 
115
                phseg->score = score;
 
116
                plist = phseg;
 
117
        }
 
118
        fclose(fp);
 
119
        if (out_phseg) {
 
120
                s3phseg_t *next, *last = NULL;
 
121
                /* Now reverse the list. */
 
122
                while (plist) {
 
123
                        next = plist->next;
 
124
                        *out_phseg = plist;
 
125
                        (*out_phseg)->next = last;
 
126
                        last = *out_phseg;
 
127
                        plist = next;
 
128
                }
 
129
        }
 
130
        else
 
131
                s3phseg_free(plist);
 
132
        return S3_SUCCESS;
 
133
error_out:
 
134
        fclose(fp);
 
135
        return S3_ERROR;
 
136
}
 
137
 
 
138
int
 
139
s3phseg_write(const char *fn,
 
140
              acmod_set_t *acmod_set,
 
141
              s3phseg_t *phseg)
 
142
{
 
143
        FILE *fp;
 
144
 
 
145
        if ((fp = fopen(fn, "w")) == NULL)
 
146
                return S3_ERROR;
 
147
 
 
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));
 
154
        }
 
155
        fclose (fp);
 
156
 
 
157
        return S3_SUCCESS;
 
158
}
 
159
 
 
160
void
 
161
s3phseg_free(s3phseg_t *phseg)
 
162
{
 
163
        s3phseg_t *next;
 
164
 
 
165
        while (phseg) {
 
166
                next = phseg->next;
 
167
                ckd_free(phseg);
 
168
                phseg = next;
 
169
        }
 
170
}
 
171
 
 
172
int
 
173
s3lattice_read(const char *fn,
 
174
               s3lattice_t **lattice)
 
175
{
 
176
  FILE *fp;
 
177
  uint32 id;
 
178
  char line[1024], temp[16];
 
179
  s3lattice_t *out_lattice;
 
180
  uint32 i, j, n;
 
181
  
 
182
  if ((fp = fopen(fn, "r")) == NULL) {
 
183
    E_ERROR("Failed to open lattice file %s\n", fn);
 
184
    return S3_ERROR;
 
185
  }
 
186
  
 
187
  out_lattice = ckd_calloc(1, sizeof(*out_lattice));
 
188
  
 
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");
 
194
    goto error_out;
 
195
  }
 
196
  fgets(line, sizeof(line), fp);
 
197
  n = sscanf(line, "%d", &out_lattice->n_arcs);
 
198
  if (n!=1) {
 
199
    E_ERROR("Lattice Format Error, missing Total arcs\n");
 
200
    goto error_out;
 
201
  }
 
202
  if (out_lattice->n_arcs == 0) {
 
203
    E_ERROR("No arc exits in the lattice\n");
 
204
    goto error_out;
 
205
  }
 
206
 
 
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");
 
211
    goto error_out;
 
212
  }
 
213
  fgets(line, sizeof(line), fp);
 
214
  n = sscanf(line, "%d", &out_lattice->n_true_arcs);
 
215
  if (n!=1) {
 
216
    E_ERROR("Lattice Format Error, missing True arcs\n");
 
217
    goto error_out;
 
218
  }
 
219
  if (out_lattice->n_true_arcs == 0) {
 
220
    E_ERROR("No arc from the numerator lattice\n");
 
221
    goto error_out;
 
222
  }
 
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");
 
225
    goto error_out;
 
226
  }
 
227
 
 
228
  /* read parameter lists */
 
229
  fgets(line, sizeof(line), fp);
 
230
  if (strstr(line, "arc_id") == NULL) {
 
231
    E_ERROR("Lattice Format Error\n");
 
232
    goto error_out;
 
233
  }
 
234
  
 
235
  /* allocate memory for arcs */
 
236
  out_lattice->arc = ckd_calloc(out_lattice->n_arcs, sizeof(*out_lattice->arc));
 
237
  
 
238
  i = 0;
 
239
  /* Get each 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 */
 
247
    
 
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");
 
252
      goto error_out;
 
253
    }
 
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]);
 
257
    
 
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");
 
262
      goto error_out;
 
263
    }
 
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]);
 
267
    
 
268
    i++;
 
269
  }
 
270
  fclose(fp);
 
271
  
 
272
  *lattice = out_lattice;
 
273
  
 
274
  return S3_SUCCESS;
 
275
 error_out:
 
276
  fclose(fp);
 
277
  return S3_ERROR;
 
278
}