~ubuntu-branches/ubuntu/saucy/sphinxtrain/saucy

« back to all changes in this revision

Viewing changes to src/libs/libio/topo_read.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) 1995-2000 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: topo_read.c
 
39
 * 
 
40
 * Description: 
 
41
 *      Read an ASCII model topology file.  This file contains
 
42
 *      an adjacency matrix with non-zero elements for all
 
43
 *      allowable transitions where a row represents a source state
 
44
 *      and a column represents a sink state.
 
45
 *
 
46
 *      If the adjacency matrix contains values that are all equal,
 
47
 *      the matrix can be normalized to obtain a uniform transition
 
48
 *      probability matrix.
 
49
 *
 
50
 * Author: 
 
51
 *      Eric H. Thayer (eht@cs.cmu.edu)
 
52
 *********************************************************************/
 
53
 
 
54
/* try to put header files in local to global order to
 
55
   try to flush out hidden dependencies */
 
56
#include <s3/topo_read.h>
 
57
 
 
58
#include <sphinxbase/pio.h>
 
59
#include <s3/common.h>
 
60
#include <s3/s3.h>
 
61
 
 
62
#include <stdio.h>
 
63
#include <assert.h>
 
64
#include <string.h>
 
65
 
 
66
#define TOPO_FILE_VERSION       "0.1"
 
67
 
 
68
/*********************************************************************
 
69
 *
 
70
 * Function: 
 
71
 *      topo_read
 
72
 * 
 
73
 * Description: 
 
74
 *      This routine reads an ASCII transition matrix which may then be
 
75
 *      used to determine the topology of the models used in the system.
 
76
 *
 
77
 * Traceability: 
 
78
 * 
 
79
 * Function Inputs: 
 
80
 * 
 
81
 * Global Inputs: 
 
82
 *      None
 
83
 * 
 
84
 * Return Values: 
 
85
 *      S3_SUCCESS is returned upon successful completion
 
86
 *      S3_ERROR is returned upon an error condition
 
87
 * 
 
88
 * Global Outputs: 
 
89
 *      None
 
90
 * 
 
91
 * Errors: 
 
92
 * 
 
93
 * Pre-Conditions: 
 
94
 * 
 
95
 * Post-Conditions: 
 
96
 * 
 
97
 * Design: 
 
98
 * 
 
99
 * Notes: 
 
100
 * 
 
101
 *********************************************************************/
 
102
int32
 
103
topo_read(float32 ***tmat,
 
104
          uint32 *n_state_pm,
 
105
          const char *topo_file_name)
 
106
{
 
107
    float32 **out;
 
108
    FILE *fp;
 
109
    lineiter_t *li = NULL;
 
110
    uint32 n_state;
 
111
    uint32 i, j;
 
112
    float32 row_sum;
 
113
 
 
114
    assert(topo_file_name != NULL);
 
115
 
 
116
    fp = fopen(topo_file_name, "r");
 
117
    if (fp == NULL) {
 
118
        E_WARN_SYSTEM("Unable to open %s for reading\n", topo_file_name);
 
119
 
 
120
        goto error;
 
121
    }
 
122
    
 
123
    li = lineiter_start_clean(fp);
 
124
    
 
125
    if (li == NULL) {
 
126
        E_ERROR("EOF encounted while reading version number in %s!?\n", topo_file_name);
 
127
 
 
128
        goto error;
 
129
    }
 
130
 
 
131
    if (strcmp(li->buf, TOPO_FILE_VERSION) != 0) {
 
132
        E_ERROR("Topo file version in %s is %s.  Expected %s\n",
 
133
                topo_file_name, li->buf, TOPO_FILE_VERSION);
 
134
 
 
135
        goto error;
 
136
    }
 
137
 
 
138
    li = lineiter_next(li);
 
139
    if (li == NULL) {
 
140
        E_ERROR("EOF encountered while reading n_state in %s!?\n", topo_file_name);
 
141
 
 
142
        goto error;
 
143
    }
 
144
 
 
145
    sscanf(li->buf, "%d\n", &n_state);
 
146
 
 
147
    /* Support Request 1504066: robust reading of topo file in
 
148
       SphinxTrain
 
149
           
 
150
       When user put 
 
151
       0.1
 
152
       1.0 1.0 1.0 0.0
 
153
       1.0 1.0 1.0 0.0
 
154
       1.0 1.0 1.0 1.0 
 
155
       
 
156
       instead of 
 
157
       
 
158
       0.1
 
159
       4
 
160
       1.0 1.0 1.0 0.0
 
161
       1.0 1.0 1.0 0.0
 
162
       1.0 1.0 1.0 1.0
 
163
       
 
164
       topo_read will misread 1.0 into n_state as 1.  And the 
 
165
       generated transition matrix will corrupt bw as well. This 
 
166
       problem is now fixed. 
 
167
    */
 
168
 
 
169
    if(n_state==1) {
 
170
        E_ERROR("n_state =1, if you are using a transition matrix with more than 1 state, this error might show that there is format issue in your input topology file.  You are recommended to use perl/make_topology.pl to generate the topo file instead.\n");
 
171
        goto error;
 
172
    }
 
173
 
 
174
    out = (float **)ckd_calloc_2d(n_state-1, n_state, sizeof(float32));
 
175
 
 
176
    for (i = 0; i < n_state-1; i++) {
 
177
        row_sum = 0.0;
 
178
        for (j = 0; j < n_state; j++) {
 
179
            fscanf(fp, "%f", &out[i][j]);
 
180
            row_sum += out[i][j];
 
181
        }
 
182
        for (j = 0; j < n_state; j++) {
 
183
            out[i][j] /= row_sum;
 
184
        }
 
185
    }
 
186
    
 
187
    *tmat = out;
 
188
    *n_state_pm = n_state;
 
189
 
 
190
    fclose(fp);
 
191
    lineiter_free(li);
 
192
    return S3_SUCCESS;
 
193
 
 
194
error:    
 
195
    if (fp) fclose(fp);
 
196
    lineiter_free(li);
 
197
    return S3_ERROR;
 
198
}
 
199
 
 
200
 
 
201
/*
 
202
 * Log record.  Maintained by RCS.
 
203
 *
 
204
 * $Log$
 
205
 * Revision 1.4  2004/07/21  18:05:40  egouvea
 
206
 * Changed the license terms to make it the same as sphinx2 and sphinx3.
 
207
 * 
 
208
 * Revision 1.3  2001/04/05 20:02:31  awb
 
209
 * *** empty log message ***
 
210
 *
 
211
 * Revision 1.2  2000/09/29 22:35:13  awb
 
212
 * *** empty log message ***
 
213
 *
 
214
 * Revision 1.1  2000/09/24 21:38:31  awb
 
215
 * *** empty log message ***
 
216
 *
 
217
 * Revision 1.1  97/03/17  15:01:49  eht
 
218
 * Initial revision
 
219
 * 
 
220
 *
 
221
 */