summaryrefslogtreecommitdiff
path: root/srec/ca/syn_srec.c
blob: 8999463129d9bd695ff18058ae557f4334dbbf3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*---------------------------------------------------------------------------*
 *  syn_srec.c  *
 *                                                                           *
 *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
 *                                                                           *
 *  Licensed under the Apache License, Version 2.0 (the 'License');          *
 *  you may not use this file except in compliance with the License.         *
 *                                                                           *
 *  You may obtain a copy of the License at                                  *
 *      http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                           *
 *  Unless required by applicable law or agreed to in writing, software      *
 *  distributed under the License is distributed on an 'AS IS' BASIS,        *
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * 
 *  See the License for the specific language governing permissions and      *
 *  limitations under the License.                                           *
 *                                                                           *
 *---------------------------------------------------------------------------*/

#include <stdlib.h>
#include <string.h>
#ifndef _RTT
#include <stdio.h>
#endif

#ifdef unix
#include <unistd.h>
#endif
#include <assert.h>

#ifndef _RTT
#include "duk_io.h"
#endif

#include "simapi.h"
#include "pendian.h"
#include "portable.h"
#include "srec_context.h"
#include "ESR_Session.h"

static void free_buffer(char* buffer, const char* allocname)
{
  FREE(buffer);
}

int CA_AttachArbdataToSyntax(CA_Syntax* syntax, CA_Arbdata* allotree)
{
  int rc;
  rc = FST_AttachArbdata(syntax->synx, (srec_arbdata*)allotree);
  return rc;
}

int CA_AddWordToSyntax(CA_Syntax* syntax, const char* slot,
                       const char *phrase, const char* pronunciation,
                       const int weight)
{
  int rc;
  rc = FST_AddWordToGrammar(syntax->synx, slot, phrase, pronunciation, weight);
  return rc;
}

int CA_ResetSyntax(CA_Syntax* syntax)
{
  int rc;
  rc = FST_ResetGrammar(syntax->synx);
  return rc;
}

int  CA_CompileSyntax(CA_Syntax *hSyntax)
{
  return FST_PrepareContext(hSyntax->synx);
}

int CA_LoadSyntaxAsExtensible(CA_Syntax *hSyntax, /*CA_Arbdata* arbdata,*/
                              char *synbase, int num_words_to_add)
{
  int rc;
  TRY_CA_EXCEPT
  ASSERT(hSyntax);
  if (hSyntax->setup_count > 0)
    SERVICE_ERROR(SYNTAX_ALREADY_SETUP);

  rc = FST_LoadContext(synbase, &hSyntax->synx, num_words_to_add);
  return rc ? 1 : 0;
  BEG_CATCH_CA_EXCEPT
  END_CATCH_CA_EXCEPT(hSyntax)
}

/* this belongs part of the srec_context! */
/**
 * @todo document
 */
typedef struct
{
  asr_int32_t image_format;
  asr_int32_t image_size;
  asr_int32_t sizes_signature;
}
context_image_header;

int CA_LoadSyntaxFromImage(CA_Syntax *hSyntax, const LCHAR* filename)
{
  int result;
  PFile* fp = NULL;
  ESR_BOOL isLittleEndian;

  /*
#if __BYTE_ORDER==__LITTLE_ENDIAN
   isLittleEndian = ESR_TRUE;
#else
   isLittleEndian = ESR_FALSE;
#endif
  */
  isLittleEndian = ESR_TRUE;

  fp = pfopen ( filename, L("rb") );
/*  CHKLOG(rc, PFileSystemCreatePFile(filename, isLittleEndian, &fp));
  CHKLOG(rc, PFileOpen(fp, L("rb")));*/

  if ( fp == NULL )
    goto CLEANUP;

  result = FST_LoadContextFromImage(&hSyntax->synx, fp);
  pfclose(fp);
  return result ? 1 : 0;
CLEANUP:
  if (fp)
    pfclose (fp);
  return 1;
}

int CA_DumpSyntax(CA_Syntax* hSyntax, const char* basename)
{
  int result, totrc = 0;
  PFile* fp;
  char buf[256];
  /*ESR_ReturnCode rc;*/

  sprintf(buf, "%s.PCLG.txt", basename);
  fp = pfopen ( buf, L("wb") );
/*  CHKLOG(rc, PFileSystemCreatePFile(buf, ESR_TRUE, &fp));
  CHKLOG(rc, PFileOpen(fp, L("wb")));*/

  if ( fp == NULL )
    goto CLEANUP;

  result = FST_DumpGraph(hSyntax->synx, fp);
  pfclose(fp);
  totrc += result;

  sprintf(buf, "%s.map", basename);
  fp = pfopen ( buf, L("wb") );
/*  CHKLOG(rc, PFileSystemCreatePFile(buf, ESR_TRUE, &fp));
  CHKLOG(rc, PFileOpen(fp, L("wb")));*/

  if ( fp == NULL )
    goto CLEANUP;

  result = FST_DumpWordMap(fp, hSyntax->synx->olabels);
  pfclose(fp);
  totrc += result;

  sprintf(buf, "%s.Grev2.det.txt", basename);
  fp = pfopen ( buf, L("wb") );
/*  CHKLOG(rc, PFileSystemCreatePFile(buf, ESR_TRUE, &fp));
  CHKLOG(rc, PFileOpen(fp, L("wb")));*/

  if ( fp == NULL )
    goto CLEANUP;

  if (0)
    result = FST_DumpReverseWordGraph(hSyntax->synx, fp);
  pfclose(fp);
  totrc += result;

  return totrc ? 1 : 0;
CLEANUP:
  return 0;
}

int CA_DumpSyntaxAsImage(CA_Syntax* hSyntax, const char* imagename, int version_number)
{
  int result;
  PFile* fp;
  /*ESR_ReturnCode rc;*/
  ESR_BOOL isLittleEndian;

  isLittleEndian = ESR_TRUE;

  fp = pfopen ( imagename, L("wb") );
/*  CHKLOG(rc, PFileSystemCreatePFile(imagename, isLittleEndian, &fp));
  CHKLOG(rc, PFileOpen(fp, L("wb")));*/

  if ( fp == NULL )
    goto CLEANUP;

  if (version_number == 2)
  {
    result = FST_DumpContextAsImageV2(hSyntax->synx, fp);
  }
  else
  {
    PLogError("invalid version number %d\n", version_number);
    result = FST_FAILED_ON_INVALID_ARGS;
  }
  pfclose(fp);
  return result ? 1 : 0;
CLEANUP:
  return 0;
}

/* from syn_file.c */

CA_Syntax *CA_AllocateSyntax(void)
{
  CA_Syntax *hSyntax = NULL;
  TRY_CA_EXCEPT
  hSyntax = (CA_Syntax *) CALLOC_CLR(1, sizeof(CA_Syntax), "ca.hSyntax");
  hSyntax->has_groups = False;
  hSyntax->has_rules = False;
  hSyntax->setup_count = 0;
  hSyntax->ca_rtti = CA_SYNTAX_SIGNATURE;
  hSyntax->synx = 0;
  return (hSyntax);

  BEG_CATCH_CA_EXCEPT
  END_CATCH_CA_EXCEPT(hSyntax)
}

void CA_FreeSyntax(CA_Syntax *hSyntax)
{
  TRY_CA_EXCEPT
  ASSERT(hSyntax);
  if (hSyntax->setup_count > 0)
    SERVICE_ERROR(SYNTAX_ALREADY_SETUP);
  /* todo: free hSyntax internals */
  FST_UnloadContext((srec_context*)(hSyntax->synx));
  hSyntax->synx = 0;
  FREE((char *) hSyntax);
  return;

  BEG_CATCH_CA_EXCEPT
  END_CATCH_CA_EXCEPT(hSyntax)
}

CA_Arbdata* CA_LoadArbdata(const char* filename)
{
  CA_Arbdata* ca_arbdata = NULL;
  int rc;

#ifdef SREC_ENGINE_VERBOSE_LOGGING
  PLogMessage(L("CA_LoadArbdata ... from file %s"), filename);
#endif
  rc = read_arbdata_from_stream((srec_arbdata**) & ca_arbdata, (char *)filename, 0);
  return ca_arbdata;
}

unsigned int CA_ArbdataGetModelVersionID(CA_Arbdata* ca_arbdata)
{
  return version_arbdata_models((srec_arbdata*)ca_arbdata);
}

int CA_ArbdataGetModelIdsForPron(CA_Arbdata* ca_arbdata, 
								 const char* pronunciation,  /* WB assumed at the edges */
								 int pronunciation_len,
								 modelID* pmodelIds)
{
	srec_arbdata *allotree = (srec_arbdata*)ca_arbdata;
	return get_modelids_for_pron( allotree,
                         pronunciation, pronunciation_len,
                          pmodelIds);
}

int CA_ArbdataGetModelIdsForPIC(CA_Arbdata* ca_arbdata, const char lphon, 
								const char cphon, 
								const char rphon)
{
  phonemeID lphon_ID, cphon_ID, rphon_ID;
  srec_arbdata *allotree = (srec_arbdata*)ca_arbdata;
  if(lphon==WBPHONEME_CODE){
#if !USE_WWTRIPHONE
    lphon_ID = (phonemeID)allotree->phoneme_index[ SILENCE_CODE];
#else
    lphon_ID = WBPHONEME_CODE; //(phonemeID)allotree->phoneme_index[ WBPHONEME_CODE];
#endif
  }
  else
    lphon_ID = (phonemeID)allotree->phoneme_index[(const unsigned char)lphon];
  cphon_ID = (phonemeID)allotree->phoneme_index[(const unsigned char)cphon];
  if(rphon==WBPHONEME_CODE){
#if !USE_WWTRIPHONE
    rphon_ID = (phonemeID)allotree->phoneme_index[ SILENCE_CODE];
#else
    rphon_ID = WBPHONEME_CODE; //(phonemeID)allotree->phoneme_index[ WBPHONEME_CODE];
#endif
  }
  else
    rphon_ID = (phonemeID)allotree->phoneme_index[(const unsigned char)rphon];
  return (modelID)get_modelid_for_pic(allotree, lphon_ID, cphon_ID, rphon_ID);
}

void CA_FreeArbdata(CA_Arbdata* ca_arbdata)
{
  free_buffer((char*)ca_arbdata, "srec.arbdata");
}
/* from syn_basi.c */

int CA_SetupSyntaxForRecognizer(CA_Syntax *hSyntax, CA_Recog *hRecog)
{
  int rc;
  const char* rule = "ROOT";
  rc = activate_grammar_for_recognition(hRecog->recm, hSyntax->synx, rule);
  return rc;
}

int CA_IsEnrollmentSyntax(CA_Syntax *hSyntax)
{
  return FST_IsVoiceEnrollment( hSyntax->synx);
}

int CA_CeilingSyntaxForRecognizer(CA_Syntax *hSyntax, CA_Recog *hRecog)
{
  if(!hSyntax || !hRecog) 
	  return 1;
  if(!hSyntax->synx || !hRecog->recm) 
	  return 1;
  hSyntax->synx->max_searchable_nodes = hRecog->recm->max_fsm_nodes;
  hSyntax->synx->max_searchable_arcs  = hRecog->recm->max_fsm_arcs;
  return 0;
}

/* checks if the phrase is in the grammar node, 0=in 1=not.in */
int CA_CheckTranscription(CA_Syntax *hSyntax, const char *transcription, int no)
{
  int rc;
  char literal[512];
  size_t max_literal_len = 512;
  srec_context* context = hSyntax->synx;
  rc = FST_CheckPath(context, transcription, literal, max_literal_len);
  if (rc == 0) strcpy((char*)transcription, literal);
  return rc;
}

/*---------------------------------------------------------------------------*
 *                                                                           *
 * do nothing functions                                                      *
 *                                                                           *
 *---------------------------------------------------------------------------*/

/* from syn_basi.c */


void CA_ClearSyntaxForRecognizer(CA_Syntax *hSyntax, CA_Recog *hRecog)
{
  int rc;
  (void) hSyntax; /* not used */
  rc = clear_grammars_for_recognition(hRecog->recm);
  return;
}