Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
3.4 KiB

  1. /****************************************************************
  2. *
  3. * NAME: confidence.c
  4. *
  5. *
  6. * DESCRIPTION:
  7. *
  8. * Common code to run confidence. This is common for all languages.
  9. * Some languages may not support confidences then we just set the default values
  10. *
  11. *
  12. * HISTORY
  13. *
  14. * Introduced March 2002 (mrevow)
  15. *
  16. ***************************************************************/
  17. #include <common.h>
  18. #include <limits.h>
  19. #include <nfeature.h>
  20. #include <engine.h>
  21. #include <nnet.h>
  22. #include <charmap.h>
  23. #include <charcost.h>
  24. #include <runNet.h>
  25. #include <avalanche.h>
  26. #include <avalanchep.h>
  27. #include <confidence.h>
  28. #include <resource.h>
  29. static LOCAL_NET s_confidenceNet = {0};
  30. static int s_cConfidenceNet = 0;
  31. // Attempt to load for languages - It is not ann error if it fails
  32. // Simply means that the language does not support confidence
  33. BOOL LoadConfidenceNets (HINSTANCE hInst)
  34. {
  35. if ( FALSE == loadNet(hInst, RESID_AVAL_CONFIDENCE, &s_cConfidenceNet, &s_confidenceNet))
  36. {
  37. memset(&s_confidenceNet, 0, sizeof(s_confidenceNet));
  38. s_cConfidenceNet = 0;
  39. }
  40. return TRUE;
  41. }
  42. // Unload Confidence nets
  43. void UnLoadConfidenceNets()
  44. {
  45. }
  46. //create a sort indedx array for the best order of the
  47. // alt list outputs
  48. static BOOL GetIndexes(int *pOutput,int cAlt,int *pOutIndex)
  49. {
  50. int *pSortOutput = NULL;
  51. int c,temp1,temp2,j;
  52. if (!(pSortOutput=(int *)ExternAlloc(sizeof(int)*cAlt)))
  53. return 0;
  54. //Initialize pOutIndex and pSortOutput
  55. for (c=0;c<cAlt;++c)
  56. {
  57. pOutIndex[c]=c;
  58. pSortOutput[c]=pOutput[c];
  59. }
  60. for (c=0;c<=cAlt-1;++c)
  61. {
  62. for (j=0;j<cAlt-1-c;j++)
  63. { if ( pSortOutput[j] < pSortOutput[j+1])
  64. {
  65. temp1=pSortOutput[j];
  66. pSortOutput[j]=pSortOutput[j+1];
  67. pSortOutput[j+1]=temp1;
  68. temp2=pOutIndex[j];
  69. pOutIndex[j]=pOutIndex[j+1];
  70. pOutIndex[j+1]=temp2;
  71. }
  72. }
  73. }
  74. ExternFree(pSortOutput);
  75. return 1;
  76. }
  77. // run the confidence nets if available
  78. // Set default value if not available
  79. BOOL ConfidenceLevel(XRC *pxrc, void *pAlt, ALTINFO *pAltInfo, RREAL *pOutput)
  80. {
  81. int cAlt;
  82. int c, cOut, iWin;
  83. PALTERNATES *pAvalAlt=(PALTERNATES *)pAlt;
  84. int *pOutIndex = NULL;
  85. RREAL *pConfFeat = NULL, *pFeat, *pOut;
  86. int iRet = FALSE;
  87. // Some languages will not have a confidence net
  88. if (0 == s_cConfidenceNet)
  89. {
  90. return FALSE;
  91. }
  92. cAlt= min (pxrc->answer.cAlt, (unsigned int)pAltInfo->NumCand);
  93. //Check the range of the number of candiudates
  94. ASSERT(cAlt>=0);
  95. ASSERT(cAlt<=TOT_CAND);
  96. if (!(pOutIndex=(int *)ExternAlloc(sizeof(int)*cAlt)))
  97. {
  98. // Nothing allocated yet - just return
  99. return 0 ;
  100. }
  101. if (!(pConfFeat=(RREAL *)ExternAlloc(sizeof(RREAL) * s_cConfidenceNet)))
  102. {
  103. goto fail;
  104. }
  105. if (!GetIndexes(pOutput, cAlt, pOutIndex))
  106. {
  107. goto fail;
  108. }
  109. pFeat = pConfFeat;
  110. for (c = 0 ; c < cAlt ; c++)
  111. {
  112. *pFeat++ = pAltInfo->aCandInfo[pOutIndex[c]].Callig;
  113. *pFeat++ = pAltInfo->aCandInfo[pOutIndex[c]].NN;
  114. *pFeat++ = pAltInfo->aCandInfo[pOutIndex[c]].InfCharCost;
  115. *pFeat++ = c;
  116. }
  117. for ( ; c < TOT_CAND; c++)
  118. {
  119. *pFeat++ = INT_MIN;
  120. *pFeat++ = INT_MAX;
  121. *pFeat++ = INT_MAX;
  122. *pFeat++ = c;
  123. }
  124. ASSERT(pFeat - pConfFeat == s_confidenceNet.runNet.cUnitsPerLayer[0]);
  125. pOut = runLocalConnectNet(&s_confidenceNet, pConfFeat, &iWin, &cOut);
  126. if (*pOut > CONFIDENCE_NET_THRESHOLD)
  127. {
  128. pxrc->answer.iConfidence=RECOCONF_MEDIUMCONFIDENCE;
  129. }
  130. else
  131. {
  132. pxrc->answer.iConfidence=RECOCONF_LOWCONFIDENCE;
  133. }
  134. // Success
  135. iRet = TRUE;
  136. fail:
  137. ExternFree(pOutIndex);
  138. ExternFree(pConfFeat);
  139. return iRet;
  140. }