wfst2c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/sh
  2. ###########################################################################
  3. ## ##
  4. ## Language Technologies Institute ##
  5. ## Carnegie Mellon University ##
  6. ## Copyright (c) 2001-2008 ##
  7. ## All Rights Reserved. ##
  8. ## ##
  9. ## Permission is hereby granted, free of charge, to use and distribute ##
  10. ## this software and its documentation without restriction, including ##
  11. ## without limitation the rights to use, copy, modify, merge, publish, ##
  12. ## distribute, sublicense, and/or sell copies of this work, and to ##
  13. ## permit persons to whom this work is furnished to do so, subject to ##
  14. ## the following conditions: ##
  15. ## 1. The code must retain the above copyright notice, this list of ##
  16. ## conditions and the following disclaimer. ##
  17. ## 2. Any modifications must be clearly marked as such. ##
  18. ## 3. Original authors' names are not deleted. ##
  19. ## 4. The authors' names are not used to endorse or promote products ##
  20. ## derived from this software without specific prior written ##
  21. ## permission. ##
  22. ## ##
  23. ## CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK ##
  24. ## DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ##
  25. ## ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT ##
  26. ## SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE ##
  27. ## FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ##
  28. ## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN ##
  29. ## AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ##
  30. ## ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF ##
  31. ## THIS SOFTWARE. ##
  32. ## ##
  33. ###########################################################################
  34. ## ##
  35. ## Convert an EST WFST into C code (with awk and sed!) ##
  36. ## ##
  37. ###########################################################################
  38. sed '1,/EST_Header_End/d' $1 |
  39. awk 'BEGIN {
  40. mmm["a"] = 97;
  41. mmm["b"] = 98;
  42. mmm["c"] = 99;
  43. mmm["d"] = 100;
  44. mmm["e"] = 101;
  45. mmm["f"] = 102;
  46. mmm["g"] = 103;
  47. mmm["h"] = 104;
  48. mmm["i"] = 105;
  49. mmm["j"] = 106;
  50. mmm["k"] = 107;
  51. mmm["l"] = 108;
  52. mmm["m"] = 109;
  53. mmm["n"] = 110;
  54. mmm["o"] = 111;
  55. mmm["p"] = 112;
  56. mmm["q"] = 113;
  57. mmm["r"] = 114;
  58. mmm["s"] = 115;
  59. mmm["t"] = 116;
  60. mmm["u"] = 117;
  61. mmm["v"] = 118;
  62. mmm["w"] = 119;
  63. mmm["x"] = 120;
  64. mmm["y"] = 121;
  65. mmm["z"] = 122;
  66. mmm["#"] = 35;
  67. mmm["V"] = 86;
  68. mmm["N"] = 78;
  69. state = "0";
  70. name="'`basename $1 .wfst`'";
  71. }
  72. function symstate(sym,state)
  73. {
  74. m = sprintf("((fsm_%s_state_%d * 128) + %d)",name,state,mmm[sym]);
  75. return m;
  76. }
  77. {if (NF == 3)
  78. {
  79. state = substr($1,3);
  80. if (state != "0")
  81. {
  82. printf("#define fsm_%s_trans_%d 0\n",name,trans);
  83. trans++;
  84. }
  85. printf("#define fsm_%s_state_%d %d\n",name,state,trans);
  86. }
  87. else if (NF == 4)
  88. {
  89. printf("#define fsm_%s_trans_%d %s\n",name,trans,symstate($2,$3));
  90. trans++;
  91. }
  92. }
  93. END {printf("#define fsm_%s_trans_%d 0\n",name,trans);
  94. trans++;
  95. printf("static short fsm_%s_trans[%d] = {\n",name,trans);
  96. for (i=0; i<trans-1; i++)
  97. {
  98. printf(" fsm_%s_trans_%d,\n",name,i);
  99. }
  100. printf(" fsm_%s_trans_%d\n",name,i);
  101. printf("};\n");
  102. printf("static cst_fsm fsm_%s = {\n",name);
  103. printf(" 128, /* vocab size */\n");
  104. printf(" %d, /* num_transitions */\n",trans);
  105. printf(" fsm_%s_trans\n",name);
  106. printf("};\n");
  107. }'