1/*---------------------------------------------------------------------------*
2 *  dictTest.c  *
3 *                                                                           *
4 *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
5 *                                                                           *
6 *  Licensed under the Apache License, Version 2.0 (the 'License');          *
7 *  you may not use this file except in compliance with the License.         *
8 *                                                                           *
9 *  You may obtain a copy of the License at                                  *
10 *      http://www.apache.org/licenses/LICENSE-2.0                           *
11 *                                                                           *
12 *  Unless required by applicable law or agreed to in writing, software      *
13 *  distributed under the License is distributed on an 'AS IS' BASIS,        *
14 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
15 *  See the License for the specific language governing permissions and      *
16 *  limitations under the License.                                           *
17 *                                                                           *
18 *---------------------------------------------------------------------------*/
19
20#include "ESR_Locale.h"
21#include "LCHAR.h"
22#include "pstdio.h"
23#include "PFileSystem.h"
24#include "PANSIFileSystem.h"
25#include "plog.h"
26#include "pmemory.h"
27#include "ESR_Session.h"
28#include "SR_Session.h"
29#include "SR_Vocabulary.h"
30
31#define MAX_LINE_LENGTH     512
32#define MAX_PRONS_LENGTH 1024
33
34static ESR_ReturnCode InitSession ( LCHAR *parfilename );
35static ESR_ReturnCode ShutdownSession ( void );
36
37void usage()
38{
39  LPRINTF("usage: dictTest [-words words.txt] [-vocab dictionary.ok] [-itest input.tst] [-out output.txt] [-locale en-us|en-gb|fr-fr|de-de] [-parfile baseline.par]\n");
40}
41
42void doGetProns(SR_Vocabulary *vocab, LCHAR* phrase, size_t len, FILE* fout)
43{
44  ESR_ReturnCode rc;
45  LCHAR prons[MAX_PRONS_LENGTH];
46
47  rc = SR_VocabularyGetPronunciation(vocab, phrase, prons, &len);
48  // rc = vocab->getPronunciation(vocab, phrase, prons, &len);
49
50  if (rc != ESR_SUCCESS)
51    LFPRINTF(fout,"ERROR: %s\n", ESR_rc2str(rc));
52  else {
53    size_t len_used;
54    LCHAR *pron = 0;
55    for(len_used=0; len_used<len; ) {
56      pron = &prons[0]+len_used;
57      len_used += LSTRLEN(pron)+1;
58      LFPRINTF(fout,"%s : %s\n", phrase, pron);
59    }
60  }
61}
62
63//parses the input file, runs phoneme tests and produces output to be parsed by perl script
64void doInputTestPhonemes(SR_Vocabulary *vocab, PFile* fin, FILE* fout)
65{
66#if 0
67  //waste of space with all of these arrays, they are too large, but leave for now
68  ESR_ReturnCode rc;
69  LCHAR line[2 * MAX_PRONS_LENGTH];
70  LCHAR phoneme[MAX_PRONS_LENGTH];
71  LCHAR* phrase;
72  LCHAR* expectedPhoneme;
73    LCHAR** tokenArray;
74  size_t len;
75
76  //read through the test file parsing it into the variables
77  while(!pfeof(fin))
78  {
79    pfgets(line, MAX_LINE_LENGTH, fin);
80
81        rc = ESR_ProcessLinearToCommandLineTokens(line, &tokenArray, &len);
82        if (rc!=ESR_SUCCESS || len!=2)
83        {
84          LFPRINTF(fout, "ERROR: INVALID FORMAT for input line\n");
85            continue;
86        }
87        phrase = tokenArray[0];
88        expectedPhoneme = tokenArray[1];
89
90      LPRINTF( "expected %s\n", expectedPhoneme);
91
92        len = MAX_PRONS_LENGTH;
93        rc = vocab->getPronunciation(vocab, phrase, phoneme, &len);
94
95        if(rc != ESR_SUCCESS)
96            LFPRINTF(fout,"ERROR: %s\n", ESR_rc2str(rc));
97        else
98        {
99            LFPRINTF(fout,"%s|%s|%s|", phrase, expectedPhoneme, phoneme);
100
101            if(LSTRCMP(expectedPhoneme, phoneme) == 0)
102                LFPRINTF(fout,"PASSED\n");
103            else
104                LFPRINTF(fout,"FAILED\n");
105        }
106  }
107#endif
108}
109
110int main(int argc, char **argv)
111{
112  LCHAR phrase[MAX_LINE_LENGTH];
113  SR_Vocabulary *vocab = 0;
114  LCHAR vocabfile[MAX_LINE_LENGTH];
115  LCHAR outfilename[MAX_LINE_LENGTH];
116  LCHAR testfilename[MAX_LINE_LENGTH];
117  LCHAR parfilename[MAX_LINE_LENGTH];
118  LCHAR wordfile[MAX_LINE_LENGTH];
119  LCHAR locale[MAX_LINE_LENGTH];
120  LCHAR ptemp[MAX_LINE_LENGTH];
121  LCHAR* p;
122  ESR_ReturnCode rc;
123  int i;
124  PFile* fin = 0;
125  FILE* fout = stdout;
126  size_t len;
127  ESR_BOOL bSession = ESR_FALSE;
128
129  LCHAR *env_sdk_path;
130  LCHAR *env_lang;
131
132  CHKLOG(rc, PMemInit());
133/*  CHKLOG(rc, PFileSystemCreate());
134    CHKLOG(rc, PANSIFileSystemCreate());
135    CHKLOG(rc, PANSIFileSystemAddPath(L("/dev/ansi"), L("/")));*/
136
137    /* Set ANSI file-system as default file-system */
138/*  CHKLOG(rc, PANSIFileSystemSetDefault(ESR_TRUE));*/
139    /* Set virtual current working directory to native current working directory */
140/*  len = P_PATH_MAX;
141    CHKLOG(rc, PANSIFileSystemGetcwd(cwd, &len));
142    CHKLOG(rc, PFileSystemChdir(cwd));*/
143
144    fout = stdout;
145  *vocabfile = 0;
146  *wordfile = 0;
147  *locale = 0;
148  *outfilename = 0;
149  *testfilename = 0;
150  *parfilename = 0;
151
152  /* get some phrases from the user */
153  LPRINTF("\nDictation Test Program for esr (Nuance Communications, 2007)\n");
154
155  if(argc != 1 && argc != 3 && argc != 5 && argc != 7 && argc != 9)
156  {
157    usage();
158        rc = 1;
159    goto CLEANUP;
160  }
161
162  for(i=1; i<argc; i++)
163  {
164    if(!LSTRCMP(argv[i], L("-words")))
165      LSTRCPY(wordfile, argv[++i]);
166    else if(!LSTRCMP(argv[i], L("-vocab")))
167      LSTRCPY(vocabfile, argv[++i]);
168    else if(!LSTRCMP(argv[i], L("-locale")))
169      LSTRCPY(locale, argv[++i]);
170    else if(!LSTRCMP(argv[i], L("-out")))
171      LSTRCPY(outfilename, argv[++i]);
172    else if(!LSTRCMP(argv[i], L("-itest")))
173      LSTRCPY(testfilename, argv[++i]);
174    else if(!LSTRCMP(argv[i], L("-parfile")) || !LSTRCMP(argv[i], L("-par")) )
175      LSTRCPY(parfilename, argv[++i]);
176    else {
177      usage();
178      rc = 1;
179      goto CLEANUP;
180    }
181  }
182
183  if ( *parfilename == L('\0') )
184  {
185    LPRINTF ( "Warning: No parfile defined in the command line.\n" );
186    LPRINTF ( "Looking for the default parfile, $ESRSDK/config/$ESRLANG/baseline.par...\n" );
187
188    env_sdk_path =  LGETENV(L("ESRSDK"));
189    if ( env_sdk_path != NULL )
190    {
191      LSPRINTF ( parfilename, L("%s/config/"), env_sdk_path );
192      env_lang = LGETENV(L("ESRLANG"));
193      if ( env_lang != NULL )
194      {
195         LSTRCAT ( parfilename, env_lang );
196         LSTRCAT ( parfilename, L("/baseline.par") );
197      }
198      else
199      {
200        LPRINTF("Error: An environment variable ESRLANG should be defined.\n");
201        goto CLEANUP;
202      }
203    }
204    else
205    {
206      LPRINTF("Error: An environment variable ESRSDK should be defined.\n");
207      goto CLEANUP;
208    }
209  }
210
211  rc = InitSession( parfilename );
212  if ( rc != ESR_SUCCESS )
213  {
214    LPRINTF("Error: %s\n", ESR_rc2str(rc));
215    goto CLEANUP;
216  }
217  bSession = ESR_TRUE;
218
219  if (*vocabfile == 0)
220  {
221    len = sizeof(vocabfile);
222    rc = ESR_SessionGetLCHAR ( L("cmdline.vocabulary"), vocabfile, &len );
223    env_sdk_path =  LGETENV(L("ESRSDK"));
224    if ( env_sdk_path != NULL )
225      {
226	LSPRINTF ( parfilename, L("%s/config/"), env_sdk_path );
227	env_lang = LGETENV(L("ESRLANG"));
228	if ( env_lang != NULL )
229	  {
230	    LSTRCAT ( parfilename, env_lang );
231	    LSTRCAT ( parfilename, L("/baseline.par") );
232	  }
233	else
234	  {
235	    LPRINTF("Error: An environment variable ESRLANG should be defined.\n");
236	    goto CLEANUP;
237	  }
238      }
239    else
240      {
241	LPRINTF("Error: An environment variable ESRSDK should be defined.\n");
242	goto CLEANUP;
243      }
244
245    strcpy(ptemp, env_sdk_path);
246    strcat(ptemp,"/config/");
247    strcat(ptemp,env_lang);
248    strcat(ptemp,"/");
249    strcat(ptemp,vocabfile);
250    strcpy(vocabfile,ptemp);
251    if ( rc == ESR_SUCCESS )
252    {
253      len = sizeof(vocabfile);
254       rc = ESR_SessionPrefixWithBaseDirectory(vocabfile, &len);
255    }
256    else
257    {
258       *vocabfile = 0;
259    }
260  }
261
262  if (*vocabfile)
263    rc = SR_VocabularyLoad(vocabfile, &vocab);
264  else if (*locale)
265  {
266    ESR_Locale localeTag;
267
268    rc = ESR_str2locale(locale, &localeTag);
269    if (rc != ESR_SUCCESS)
270    {
271      LPRINTF("Error: %s\n",ESR_rc2str(rc));
272      goto CLEANUP;
273    }
274    rc = SR_VocabularyCreate(localeTag, &vocab);
275  }
276  else
277    rc = SR_VocabularyCreate(ESR_LOCALE_EN_US, &vocab);
278
279  if (rc != ESR_SUCCESS)
280  {
281    LPRINTF("Error: %s\n",ESR_rc2str(rc));
282    goto CLEANUP;
283  }
284
285  if (*outfilename) /* output file */
286  {
287    if  ((fout = fopen(outfilename,"w")) == NULL)
288    {
289      LPRINTF("Could not open file: %s\n",outfilename);
290      rc = 1;
291      goto CLEANUP;
292    }
293  }
294
295  if (*wordfile) /* file mode */
296  {
297    if ((fin = pfopen(wordfile,"r")) == NULL)
298    {
299      LPRINTF("Could not open file: %s\n", wordfile);
300      goto CLEANUP;
301    }
302    while (pfgets(phrase, MAX_LINE_LENGTH, fin)!=NULL)
303    {
304      lstrtrim(phrase);
305      doGetProns(vocab, phrase, MAX_PRONS_LENGTH, fout);
306    }
307
308  }
309  else if (*testfilename) /* test file mode */
310  {
311    if ((fin = pfopen(testfilename,"r")) == NULL)
312    {
313      LPRINTF("Could not open file: %s\n", testfilename);
314      rc = 1;
315      goto CLEANUP;
316    }
317    doInputTestPhonemes(vocab, fin, fout);
318  }
319  else /* interactive mode */
320  {
321    LPRINTF("'qqq' to quit\n");
322    while (ESR_TRUE)
323    {
324      LPRINTF("> ");
325      if(! pfgets(phrase, MAX_LINE_LENGTH, PSTDIN ))
326        break;
327      // remove trailing whitespace
328      for(p=&phrase[0]; *p!=0 && *p!='\n' && *p!='\r'; p++) {}
329      *p=0;
330      lstrtrim(phrase);
331      if(!LSTRCMP("qqq",phrase))
332        break;
333      else
334        doGetProns(vocab, phrase, MAX_PRONS_LENGTH, fout);
335     }
336  }
337
338CLEANUP:
339  if(vocab)
340    vocab->destroy(vocab);
341
342  if(bSession)
343    ShutdownSession();
344
345  if(fin)
346    pfclose(fin);
347
348  if(fout && fout != stdout)
349    fclose(fout);
350
351/*  PANSIFileSystemDestroy();
352  PFileSystemDestroy();*/
353  PMemShutdown();
354  return rc;
355}
356
357static ESR_ReturnCode InitSession ( LCHAR *parfilename )
358{
359  ESR_ReturnCode    init_status;
360
361  init_status = SR_SessionCreate ( parfilename );
362
363  return ( init_status );
364}
365
366static ESR_ReturnCode ShutdownSession ( void )
367{
368  ESR_ReturnCode    shutdown_status;
369
370  shutdown_status = SR_SessionDestroy ( );
371
372  return ( shutdown_status );
373}
374
375