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.

372 lines
9.6 KiB

  1. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  2. //
  3. // Copyright (c) 2001 Microsoft Corporation. All rights reserved.
  4. //
  5. // Module:
  6. // volcano/dll/vtunefl.c
  7. //
  8. // Description:
  9. // Volcano tuning parameters, training and runtime code
  10. //
  11. // Author:
  12. // hrowley
  13. //
  14. //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  15. #include <stdio.h>
  16. #include <float.h>
  17. #include <stdlib.h>
  18. #include "common.h"
  19. #include "vtune.h"
  20. // An array of names of the weights, for the tuning program
  21. wchar_t *g_wszVTuneWeightNames[VTUNE_NUM_WEIGHTS] =
  22. {
  23. L"Circle",
  24. L"IJ",
  25. L"Unigram",
  26. L"CharShapeBoxUni",
  27. L"CharCrane",
  28. L"CharOtter",
  29. L"CharZilla",
  30. L"CharHound",
  31. L"StrShapeBoxUni",
  32. L"StrCrane",
  33. L"StrOtter",
  34. L"StrZilla",
  35. L"StrHound",
  36. L"StrShapeBoxBi",
  37. L"StrSUnigram",
  38. L"StrBigram",
  39. L"StrCBigram",
  40. L"FreeProb",
  41. L"FreeSUnigram",
  42. L"FreeBigram",
  43. L"FreeCBigram",
  44. L"FreeShapeUni",
  45. L"FreeShapeBi",
  46. L"FreeSegUni",
  47. L"FreeSegBi"
  48. };
  49. ///////////////////////////////////////
  50. //
  51. // VTuneInit
  52. //
  53. // Initialize the parameter database to a reasonable set of defaults
  54. // based on the previous volcano parameters.
  55. //
  56. // Parameters:
  57. // pTune: [out] Pointer to a parameter database to be initialized
  58. //
  59. // Return values:
  60. // None.
  61. //
  62. //////////////////////////////////////
  63. void VTuneInit(VOLCANO_PARAMS *pTune)
  64. {
  65. int i;
  66. // Initialize the header
  67. pTune->dwFileType = VTUNE_FILE_TYPE;
  68. pTune->iFileVer = VTUNE_CUR_FILE_VERSION;
  69. pTune->iMinCodeVer = VTUNE_MIN_FILE_VERSION;
  70. // Untunable parameters, not enough data
  71. pTune->weights.afl[VTUNE_ADHOC_CIRCLE] = (float) 18;
  72. pTune->weights.afl[VTUNE_ADHOC_IJ] = (float) 5;
  73. // One fixed weight to get everything to scale well
  74. pTune->weights.afl[VTUNE_UNIGRAM] = (float) 1;
  75. // Initialize the weights for all the core recognizers.
  76. for (i = 0; i < VOLCANO_CONFIG_NUM_CORE_RECOGNIZERS; i++)
  77. {
  78. pTune->weights.afl[VTUNE_CHAR_CORE + i] = (float) 1;
  79. pTune->weights.afl[VTUNE_STRING_CORE + i] = (float) 1;
  80. }
  81. // Character mode parameters
  82. pTune->weights.afl[VTUNE_CHAR_CRANE] = (float) 1;
  83. pTune->weights.afl[VTUNE_CHAR_CORE + VOLCANO_CONFIG_ZILLA] = (float) 9.469135;
  84. pTune->weights.afl[VTUNE_CHAR_SHAPE_BOX_UNIGRAM] = (float) 0.4;
  85. // String mode parameters
  86. pTune->weights.afl[VTUNE_STRING_CRANE] = (float) 1;
  87. pTune->weights.afl[VTUNE_STRING_CORE + VOLCANO_CONFIG_ZILLA] = (float) 9.469135;
  88. pTune->weights.afl[VTUNE_STRING_SHAPE_BOX_UNIGRAM] = (float) 0.4;
  89. pTune->weights.afl[VTUNE_STRING_SHAPE_BOX_BIGRAM] = (float) 0.0747;
  90. pTune->weights.afl[VTUNE_STRING_SMOOTHING_UNIGRAM] = (float) 0.455;
  91. pTune->weights.afl[VTUNE_STRING_BIGRAM] = (float) 0.0576;
  92. pTune->weights.afl[VTUNE_STRING_CLASS_BIGRAM] = (float) 0.0;
  93. // Free mode parameters
  94. pTune->weights.afl[VTUNE_FREE_SMOOTHING_UNIGRAM] = (float) 0.455;
  95. pTune->weights.afl[VTUNE_FREE_BIGRAM] = (float) 0.0576;
  96. pTune->weights.afl[VTUNE_FREE_CLASS_BIGRAM] = (float) 0.0;
  97. pTune->weights.afl[VTUNE_FREE_SEG_UNIGRAM] = (float) 0.227;
  98. pTune->weights.afl[VTUNE_FREE_SEG_BIGRAM] = (float) 0.227;
  99. pTune->weights.afl[VTUNE_FREE_SHAPE_UNIGRAM] = (float) 0.4;
  100. pTune->weights.afl[VTUNE_FREE_SHAPE_BIGRAM] = (float) 0.0747;
  101. pTune->weights.afl[VTUNE_FREE_PROB] = (float) 1.0;
  102. // The following five parameters are not tuned using the
  103. // linear tuning algorithm so they are not placed in the array
  104. // of weights.
  105. // Zilla geometric weighting
  106. pTune->flZillaGeo = (float) 1.333333;
  107. // IFELang3 weightings
  108. pTune->flStringHwxWeight = (float) (7000.0);
  109. pTune->flStringHwxThreshold = -FLT_MAX;
  110. pTune->flFreeHwxWeight = (float) (1250.0);
  111. pTune->flFreeHwxThreshold = -FLT_MAX;
  112. }
  113. ///////////////////////////////////////
  114. //
  115. // VTuneWriteFile
  116. //
  117. // Given a set of parameters and a file name, write out the database.
  118. //
  119. // Parameters:
  120. // pTune: [in] The parameters to write out
  121. // wszFileName: [in] The file name to write to
  122. //
  123. // Return values:
  124. // TRUE on success, FALSE on failure.
  125. //
  126. //////////////////////////////////////
  127. BOOL VTuneWriteFile(VOLCANO_PARAMS *pTune, wchar_t *wszFileName)
  128. {
  129. FILE *f = _wfopen(wszFileName, L"wb");
  130. if (f == NULL)
  131. {
  132. return FALSE;
  133. }
  134. if (fwrite(pTune, sizeof(VOLCANO_PARAMS), 1, f) < 1)
  135. {
  136. fclose(f);
  137. return FALSE;
  138. }
  139. if (fclose(f) < 0)
  140. {
  141. return FALSE;
  142. }
  143. return TRUE;
  144. }
  145. ///////////////////////////////////////
  146. //
  147. // VTuneLoadFileFromName
  148. //
  149. // Given a pointer to a parameter structure and a file name, read
  150. // the parameter database in.
  151. //
  152. // Parameters:
  153. // pTune: [out] Pointer to parameter structure to fill in
  154. // wszFileName: [in] File to read from
  155. //
  156. // Return values:
  157. // TRUE on success, FALSE on failure.
  158. //
  159. //////////////////////////////////////
  160. BOOL VTuneLoadFileFromName(VOLCANO_PARAMS *pTune, wchar_t *wszFileName)
  161. {
  162. FILE *f = _wfopen(wszFileName, L"rb");
  163. if (f == NULL) return FALSE;
  164. if (fread(pTune, sizeof(VOLCANO_PARAMS), 1, f) < 1)
  165. {
  166. fclose(f);
  167. return FALSE;
  168. }
  169. fclose(f);
  170. // Check the file version
  171. if (!VTuneCheckFileVersion(pTune))
  172. {
  173. return FALSE;
  174. }
  175. return TRUE;
  176. }
  177. ///////////////////////////////////////
  178. //
  179. // VTuneLoadFile
  180. //
  181. // Given a parameter information structure and a path,
  182. // map the database from a file called vtune.bin in that
  183. // directory and fill in the info structure.
  184. //
  185. // Parameters:
  186. // pInfo: [out] Pointer to the parameter information structure to fill in.
  187. // wszPath: [in] Name of directory to map vtune.bin from
  188. //
  189. // Return values:
  190. // TRUE on success, FALSE on failure.
  191. //
  192. //////////////////////////////////////
  193. BOOL VTuneLoadFile(VOLCANO_PARAMS_INFO *pInfo, wchar_t *wszPath)
  194. {
  195. wchar_t wszFile[_MAX_PATH];
  196. // Generate path to file.
  197. FormatPath(wszFile, wszPath, (wchar_t *)0, (wchar_t *)0, (wchar_t *)0, L"vtune.bin");
  198. pInfo->pTune = (VOLCANO_PARAMS *) DoOpenFile(&pInfo->info, wszFile);
  199. if (pInfo->pTune == NULL)
  200. {
  201. ASSERT(("Failed to map file for VTune database.\n", FALSE));
  202. return FALSE;
  203. }
  204. // Check the file version
  205. return VTuneCheckFileVersion(pInfo->pTune);
  206. }
  207. ///////////////////////////////////////
  208. //
  209. // VTuneUnloadFile
  210. //
  211. // Given a parameter information structure, close the mapping
  212. // from the file that holds the parameters.
  213. //
  214. // Parameters:
  215. // pInfo: [in] Pointer to the parameter information structure to fill in.
  216. //
  217. // Return values:
  218. // TRUE on success, FALSE on failure.
  219. //
  220. //////////////////////////////////////
  221. BOOL VTuneUnloadFile(VOLCANO_PARAMS_INFO *pInfo)
  222. {
  223. return DoCloseFile(&pInfo->info);
  224. }
  225. ///////////////////////////////////////
  226. //
  227. // VTuneCompressTuningRecord
  228. //
  229. // Given an array of scores from the various recognizer components,
  230. // write it out to a file in a compact form. The compaction process
  231. // makes two assumptions, first that there are less than 32 components,
  232. // and second that many of the component scores will be zero. The compact
  233. // form consists of a DWORD which is a bitmask of which components
  234. // have non-zero stores, followed by FLOATs giving those scores.
  235. //
  236. // Parameters:
  237. // f: [in] File to write tuning information to
  238. // pTune: [in] Array of component scores to write out
  239. //
  240. // Return values:
  241. // TRUE on success, FALSE on write failure.
  242. //
  243. //////////////////////////////////////
  244. BOOL VTuneCompressTuningRecord(FILE *f, VOLCANO_WEIGHTS *pTune)
  245. {
  246. #if VTUNE_NUM_WEIGHTS > 32
  247. #error VTUNE_NUM_WEIGHTS must be not be more than 32
  248. #endif
  249. int i;
  250. // Build the bitmask of non-zero scores
  251. DWORD fFlags = 0;
  252. for (i = 0; i < VTUNE_NUM_WEIGHTS; i++)
  253. {
  254. if (pTune->afl[i] != 0)
  255. {
  256. fFlags |= (1 << i);
  257. }
  258. }
  259. // Write out the bitmask
  260. if (fwrite(&fFlags, sizeof(DWORD), 1, f) < 1)
  261. {
  262. return FALSE;
  263. }
  264. // Then write out the non-zero scores
  265. for (i = 0; i < VTUNE_NUM_WEIGHTS; i++)
  266. {
  267. if (pTune->afl[i] != 0)
  268. {
  269. if (fwrite(pTune->afl + i, sizeof(FLOAT), 1, f) < 1)
  270. {
  271. return FALSE;
  272. }
  273. }
  274. }
  275. return TRUE;
  276. }
  277. ///////////////////////////////////////
  278. //
  279. // VTuneDecompressTuningRecord
  280. //
  281. // Given a compacted array of scores as produced by the VTuneCompressTuningRecord,
  282. // unpack it into a flat array for use by the tuning programs.
  283. //
  284. // Parameters:
  285. // pTune: [out] Pointer to score array to fill in
  286. // pBuffer: [in] Pointer to the compacted record
  287. //
  288. // Return values:
  289. // Pointer to the next tuning record.
  290. //
  291. //////////////////////////////////////
  292. DWORD *VTuneDecompressTuningRecord(VOLCANO_WEIGHTS *pTune, DWORD *pBuffer)
  293. {
  294. int i;
  295. // Get the bitmask of non-zero scores
  296. DWORD fFlags = *(pBuffer++);
  297. // Initialize all the scores to zero
  298. VTuneZeroWeights(pTune);
  299. // Then run through all the non-zero scores and fill them in
  300. for (i = 0; i < VTUNE_NUM_WEIGHTS; i++)
  301. {
  302. if (fFlags & (1 << i))
  303. {
  304. pTune->afl[i] = *((FLOAT *)pBuffer);
  305. pBuffer++;
  306. }
  307. }
  308. return pBuffer;
  309. }
  310. ///////////////////////////////////////
  311. //
  312. // VTuneAddScores
  313. //
  314. // Add an array of scores to another array of scores
  315. //
  316. // Parameters:
  317. // pDest: [in/out] Array of scores where the result is stored.
  318. // pSrc: [in] Array of scores to add to pDest.
  319. //
  320. // Return values:
  321. // None.
  322. //
  323. //////////////////////////////////////
  324. void VTuneAddScores(VOLCANO_WEIGHTS *pDest, VOLCANO_WEIGHTS *pSrc)
  325. {
  326. int i;
  327. for (i = 0; i < VTUNE_NUM_WEIGHTS; i++)
  328. {
  329. pDest->afl[i] += pSrc->afl[i];
  330. }
  331. }