• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*---------------------------------------------------------------------------*
2  *  cnfd_scr.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 /* Mark - Jan 2002 */
21 
22 #include "math.h"
23 #include "stdlib.h"
24 #include "stdio.h"
25 #include "simapi.h"
26 #include "pendian.h"
27 #include "portable.h"
28 #include "srec_results.h"
29 #include "ESR_Session.h"
30 
31 #if USE_CONFIDENCE_SCORER
32 
33 #ifdef SREC_ENGINE_VERBOSE_LOGGING
34 static const char* const conf_feature_names[12] =
35   { "gdiff", "sd", "sd13", "spf", "abs", "gdiffpf", "gv" };
36 #endif
37 
38 #define CONF_FEATURE_GDIFF           0
39 #define CONF_FEATURE_SCORE_DIFF      1
40 #define CONF_FEATURE_SCORE_DIFF13    2
41 #define CONF_FEATURE_SCORE_PER_FRAME 3
42 #define CONF_FEATURE_ABSOLUTE_SCORE  4
43 #define CONF_FEATURE_GDIFF_PER_FRAME 5
44 #define NUM_CONF_FEATURES            6
45 #if NUM_CONF_FEATURES != NCONFPARS
46 #error allocate difference in simapi.h
47 #endif
48 
CA_AllocateConfidenceScorer(void)49 CA_ConfidenceScorer* CA_AllocateConfidenceScorer(void)
50 {
51   CA_ConfidenceScorer *hConfidenceScorer;
52   hConfidenceScorer = NULL;
53 
54   TRY_CA_EXCEPT
55 
56   hConfidenceScorer = (CA_ConfidenceScorer *) CALLOC_CLR(1,
57                       sizeof(CA_ConfidenceScorer), "ca.hConfidenceScorer");
58 
59   return (hConfidenceScorer);
60 
61   BEG_CATCH_CA_EXCEPT
62   END_CATCH_CA_EXCEPT(hConfidenceScorer)
63 
64 }
65 
66 
CA_FreeConfidenceScorer(CA_ConfidenceScorer * hConfidenceScorer)67 void CA_FreeConfidenceScorer(CA_ConfidenceScorer* hConfidenceScorer)
68 {
69   TRY_CA_EXCEPT
70 
71   ASSERT(hConfidenceScorer);
72 
73   CA_UnloadConfidenceScorer(hConfidenceScorer);
74 
75   FREE((char *) hConfidenceScorer);
76   return;
77 
78   BEG_CATCH_CA_EXCEPT
79   END_CATCH_CA_EXCEPT(hConfidenceScorer);
80 }
81 
82 
CA_LoadConfidenceScorer(CA_ConfidenceScorer * hConfidenceScorer)83 int CA_LoadConfidenceScorer(CA_ConfidenceScorer* hConfidenceScorer)
84 {
85   static char const * const names[NUM_CONF_FEATURES] = {
86       "gdiff",
87       "sd",
88       "sd13",
89       "spf",
90       "abs",
91       "gdiffpf",
92   };
93   int i, j;
94 
95   for (j = 0; j < 2; j++) {
96     for (i = 0; i < NUM_CONF_FEATURES; i++) {
97       char name[256];
98       char value[256];
99       Confidence_model_parameters* params;
100       size_t len;
101 
102       if (j == 0) {
103         params = &hConfidenceScorer->one_nbest;
104         sprintf(name, "SREC.Confidence.sigmoid_param.%s.one_nbest", names[i]);
105       }
106       else {
107         params = &hConfidenceScorer->many_nbest;
108         sprintf(name, "SREC.Confidence.sigmoid_param.%s.many_nbest", names[i]);
109       }
110       len = P_PATH_MAX;
111       if (ESR_SUCCESS != ESR_SessionGetLCHAR(name, (LCHAR*) value, &len)) {
112         return ESR_FALSE;
113       }
114       if (3 != sscanf(value, "%lg %lg %lg",
115           &params->scale[i], &params->offset[i], &params->weight[i])) {
116         return ESR_FALSE;
117       }
118     }
119   }
120   return ESR_TRUE;
121 }
122 
123 
CA_UnloadConfidenceScorer(CA_ConfidenceScorer * hConfidenceScorer)124 void CA_UnloadConfidenceScorer(CA_ConfidenceScorer* hConfidenceScorer)
125 {
126   ASSERT(hConfidenceScorer);
127 }
128 
129 
130 
131 static int CA_ConfScorerGetFeatures(CA_Recog* recog, CA_NBestList *nbestlist, float* features, int *num_features,
132 									int choice_number, int num_choices_left);
133 
CA_ComputeConfidenceValues(CA_ConfidenceScorer * hConfidenceScorer,CA_Recog * recog,CA_NBestList * nbestlist)134 int CA_ComputeConfidenceValues(CA_ConfidenceScorer* hConfidenceScorer, CA_Recog* recog,
135                                                             CA_NBestList *nbestlist)
136 {
137   float features[12];
138   double value=1.0, final_value, confidence_value;
139   double confidence_feature, confidence_feature_weighted;
140   int i, num_features,current_choice;
141   int rc, error_check;
142   int num_choices,num_choices_left;
143 
144   confidence_value = 1.0;
145   num_choices_left = num_choices = srec_nbest_get_num_choices(nbestlist);
146 
147   for(current_choice=0;current_choice<num_choices;current_choice++)
148     {
149       confidence_value = 1.0;
150       rc = CA_ConfScorerGetFeatures(recog, nbestlist, features, &num_features, current_choice, num_choices_left);
151       if (rc)
152         {
153           PLogError("confscor failed\n");
154           error_check = srec_nbest_put_confidence_value(nbestlist, current_choice, 0);
155           if(error_check)
156             return 1;
157           num_choices_left--;
158           continue;
159         }
160 
161       if (num_choices_left == 1)
162         {
163           for (i=0;i<NUM_CONF_FEATURES;i++) {
164             if(i==CONF_FEATURE_SCORE_DIFF || i==CONF_FEATURE_SCORE_DIFF13) {
165               confidence_feature_weighted = 1.0;
166             }
167             else {
168               confidence_feature = 1.0/(1.0 + exp((hConfidenceScorer->one_nbest.scale[i] *
169                                                    features[i]) + hConfidenceScorer->one_nbest.offset[i]));
170               confidence_feature_weighted = pow(confidence_feature,hConfidenceScorer->one_nbest.weight[i]);
171             }
172             confidence_value = confidence_value * confidence_feature_weighted;
173           }
174         }
175       else
176         {
177           for (i=0;i<NUM_CONF_FEATURES;i++) {
178             confidence_feature = 1.0/(1.0 + exp((hConfidenceScorer->many_nbest.scale[i] *
179                                                  features[i]) + hConfidenceScorer->many_nbest.offset[i]));
180             confidence_value = confidence_value * pow(confidence_feature, hConfidenceScorer->many_nbest.weight[i]);
181           }
182         }
183 
184       value *= confidence_value;
185       final_value = 1000.0 * value;
186       error_check = srec_nbest_put_confidence_value(nbestlist, current_choice, (int)final_value);
187       if(error_check)
188         return 1;
189       num_choices_left--;
190 	}
191 	num_choices_left = srec_nbest_fix_homonym_confidence_values( nbestlist);
192 #ifdef SREC_ENGINE_VERBOSE_LOGGING
193   PLogMessage("confidence %d features ", (int)final_value);
194   for (i = 0; i < num_features; i++)
195     PLogMessage(" %s %f", conf_feature_names[i], features[i]);
196 #endif
197 
198   return 0;
199 }
200 
CA_ConfScorerGetFeatures(CA_Recog * recog,CA_NBestList * nbestlist,float * features,int * num_features,int choice_number,int num_choices_left)201 int CA_ConfScorerGetFeatures(CA_Recog* recog, CA_NBestList *nbestlist, float* features, int *num_features,
202 							 int choice_number, int num_choices_left)
203 {
204   //static char* rejfeat_type = (char*) - 1;
205   int rc;
206   asr_int32_t num_speech_frames = 400;
207 #define MAX_ENTRY_LENGTH 512
208   char label0[MAX_ENTRY_LENGTH];
209   char label1[MAX_ENTRY_LENGTH];
210   asr_int32_t cost0, cost1, cost2;
211   asr_int32_t speech_cost0;
212   asr_int32_t gsm_cost = 0, am_index = 0, num_words = 0;
213 
214   if (!nbestlist)
215     return 1;
216 
217   ASSERT(features);
218   ASSERT(num_features);
219   ASSERT(nbestlist);
220   ASSERT(recog);
221 
222   /* @F=(,"gdiff","sdiff12","sdiff13","spf1","speechcost0","gdiffpf");
223             0       1          2        3         4           5      */
224  if (num_choices_left > 0)
225   {
226     rc = srec_nbest_get_result(nbestlist, choice_number, label0, MAX_ENTRY_LENGTH, &cost0, choice_number);
227     if (rc) return rc;
228     rc = srec_nbest_get_choice_info(nbestlist, choice_number, &num_speech_frames, "num_speech_frames");
229     if (rc) return rc;
230     rc = srec_nbest_get_choice_info(nbestlist, choice_number, &speech_cost0, "speech_frames_cost");
231     if (rc) return rc;
232     ASSERT(!rc);
233     features[CONF_FEATURE_ABSOLUTE_SCORE] = ((float)(speech_cost0));
234     features[CONF_FEATURE_SCORE_PER_FRAME] = ((float)(speech_cost0)) / (float)num_speech_frames;
235     if (num_choices_left> 1)
236     {
237       rc = srec_nbest_get_result(nbestlist, choice_number+1, label1, MAX_ENTRY_LENGTH, &cost1, choice_number);
238       if (rc) return rc;
239       features[CONF_FEATURE_SCORE_DIFF] = ((float)cost1 - (float)cost0);
240       if (num_choices_left > 2)
241       {
242         rc = srec_nbest_get_result(nbestlist, choice_number+2, label1, MAX_ENTRY_LENGTH, &cost2, choice_number);
243         if (rc) return rc;
244         features[CONF_FEATURE_SCORE_DIFF13] = ((float)cost2 - (float)cost0);
245       }
246       else
247       {
248         cost2 = (-1);
249         features[CONF_FEATURE_SCORE_DIFF13] = ((float)cost1 - (float)cost0);
250       }
251     }
252     else
253     {
254       features[CONF_FEATURE_SCORE_DIFF] = 400;
255       features[CONF_FEATURE_SCORE_DIFF13] = 400;
256       cost1 = cost2 = (-1);
257     }
258 
259 
260     srec_nbest_get_choice_info(nbestlist, choice_number, &gsm_cost, "gsm_cost");
261     srec_nbest_get_choice_info(nbestlist, choice_number, &am_index, "acoustic_model_index");
262     srec_nbest_get_choice_info(nbestlist, choice_number, &num_words, "num_words");
263 
264     features[CONF_FEATURE_GDIFF] = (float)(speech_cost0 - gsm_cost);
265     /* should never happen */
266     if (num_speech_frames == 0) num_speech_frames = 1;
267     features[CONF_FEATURE_GDIFF_PER_FRAME] = ((float)(speech_cost0 - gsm_cost)) / (float)num_speech_frames;
268     // features[CONF_FEATURE_GENDER_VALUE] = (float)am_index;
269     *num_features = NUM_CONF_FEATURES;
270     rc = 0;
271   }
272   else
273   {
274     *num_features = 0;
275     rc = 1;
276   }
277 
278 #define DUMP_FEATURES_FOR_RETRAIN 0
279 #if DUMP_FEATURES_FOR_RETRAIN
280   srec_nbest_get_choice_info(nbestlist, 0, &gsm_cost, "gsm_cost");
281   srec_nbest_get_choice_info(nbestlist, 0, &am_index, "acoustic_model_index");
282   srec_nbest_get_choice_info(nbestlist, 0, &num_words, "num_words");
283 
284   printf("REJFEATS:");
285   printf(" cost0(%d)", cost0);
286   printf(" cost1(%d)", cost1);
287   printf(" cost2(%d)", cost2);
288   printf(" speechcost0(%d)", speech_cost0);
289   printf(" nframes0(%d)", num_speech_frames);
290   printf(" nwords0(%d)", num_words);
291   printf(" gsmcost(%d)", gsm_cost);
292   printf(" amindex(%d)", am_index);
293   /* printf(" glmcost(%d)", glm_cost);*/
294   printf("\n");
295 #endif
296   return rc; /* no error reported */
297 }
298 
299 
300 #endif /* #if USE_CONFIDENCE_SCORER */
301 
302