cmu_indic_lang.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*************************************************************************/
  2. /* */
  3. /* Language Technologies Institute */
  4. /* Carnegie Mellon University */
  5. /* Copyright (c) 2013 */
  6. /* All Rights Reserved. */
  7. /* */
  8. /* Permission is hereby granted, free of charge, to use and distribute */
  9. /* this software and its documentation without restriction, including */
  10. /* without limitation the rights to use, copy, modify, merge, publish, */
  11. /* distribute, sublicense, and/or sell copies of this work, and to */
  12. /* permit persons to whom this work is furnished to do so, subject to */
  13. /* the following conditions: */
  14. /* 1. The code must retain the above copyright notice, this list of */
  15. /* conditions and the following disclaimer. */
  16. /* 2. Any modifications must be clearly marked as such. */
  17. /* 3. Original authors' names are not deleted. */
  18. /* 4. The authors' names are not used to endorse or promote products */
  19. /* derived from this software without specific prior written */
  20. /* permission. */
  21. /* */
  22. /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
  23. /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
  24. /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
  25. /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
  26. /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
  27. /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
  28. /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
  29. /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
  30. /* THIS SOFTWARE. */
  31. /* */
  32. /*************************************************************************/
  33. /* */
  34. /* indic language support */
  35. /* */
  36. /*************************************************************************/
  37. #include "flite.h"
  38. #include "cst_val.h"
  39. #include "cst_voice.h"
  40. #include "cst_lexicon.h"
  41. #include "cst_ffeatures.h"
  42. #include "cmu_indic_lang.h"
  43. /* ./bin/compile_regexes cst_rx_not_indic "^[0-9a-zA-Z/:_'-]+$" */
  44. static const unsigned char cst_rx_not_indic_rxprog[] = {
  45. 156, 6, 0, 83, 1, 0, 3, 11, 0, 74, 4, 0, 0, 48, 49, 50,
  46. 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102, 103, 104, 105,
  47. 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
  48. 122, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  49. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 47, 58, 95, 39, 45,
  50. 0, 2, 0, 3, 0, 0, 0,
  51. };
  52. static const cst_regex cst_rx_not_indic_rx = {
  53. 0, 1, NULL, 0, 87,
  54. (char *)cst_rx_not_indic_rxprog
  55. };
  56. const cst_regex * const cst_rx_not_indic = &cst_rx_not_indic_rx;
  57. cst_val *us_tokentowords(cst_item *token);
  58. /* Note that's an ascii | not the devangari one */
  59. const cst_string * const indic_postpunctuationsymbols = "\"'`.,:;!?(){}[]|";
  60. static cst_val *cmu_indic_tokentowords_one(cst_item *token, const char *name);
  61. cst_val *cmu_indic_tokentowords(cst_item *token) {
  62. return cmu_indic_tokentowords_one(token, item_feat_string(token, "name"));
  63. }
  64. static cst_val *cmu_indic_tokentowords_one(cst_item *token, const char *name)
  65. {
  66. /* Return list of words that expand token/name */
  67. cst_val *r;
  68. /* printf("token_name %s name %s\n",item_name(token),name); */
  69. if (item_feat_present(token,"phones"))
  70. return cons_val(string_val(name),NULL);
  71. #if 0
  72. if (item_feat_present(token,"nsw"))
  73. nsw = item_feat_string(token,"nsw");
  74. utt = item_utt(token);
  75. lex = val_lexicon(feat_val(utt->features,"lexicon"));
  76. #endif
  77. if (cst_regex_match(cst_rx_not_indic,name))
  78. /* Do English analysis on non-unicode tokens */
  79. r = us_tokentowords(token);
  80. else if (cst_strlen(name) > 0)
  81. r = cons_val(string_val(name),0);
  82. else
  83. r = NULL;
  84. return r;
  85. }
  86. int indic_utt_break(cst_tokenstream *ts,
  87. const char *token,
  88. cst_relation *tokens)
  89. {
  90. const char *postpunct = item_feat_string(relation_tail(tokens), "punc");
  91. const char *ltoken = item_name(relation_tail(tokens));
  92. if (cst_strchr(ts->whitespace,'\n') != cst_strrchr(ts->whitespace,'\n'))
  93. /* contains two new lines */
  94. return TRUE;
  95. else if ((cst_strlen(ltoken) > 3) &&
  96. (cst_streq(&ltoken[cst_strlen(ltoken)-3],"।"))) /* devanagari '|' */
  97. return TRUE;
  98. else if (strchr(postpunct,':') ||
  99. strchr(postpunct,'?') ||
  100. strchr(postpunct,'|') || /* if ascii '|' gets used as dvngr '|' */
  101. strchr(postpunct,'!'))
  102. return TRUE;
  103. else if (strchr(postpunct,'.'))
  104. return TRUE;
  105. else
  106. return FALSE;
  107. }
  108. void cmu_indic_lang_init(cst_voice *v)
  109. {
  110. /* Set indic language stuff */
  111. feat_set_string(v->features,"language","cmu_indic_lang");
  112. /* utterance break function */
  113. feat_set(v->features,"utt_break",breakfunc_val(&indic_utt_break));
  114. /* Phoneset -- need to get this from voice */
  115. feat_set(v->features,"phoneset",phoneset_val(&cmu_indic_phoneset));
  116. feat_set_string(v->features,"silence",cmu_indic_phoneset.silence);
  117. /* Get information from voice and add to lexicon */
  118. /* Text analyser -- whitespace defaults */
  119. feat_set_string(v->features,"text_whitespace",
  120. cst_ts_default_whitespacesymbols);
  121. feat_set_string(v->features,"text_prepunctuation",
  122. cst_ts_default_prepunctuationsymbols);
  123. /* We can't put multi-byte characters in these classes so we can't */
  124. /* add devanagari end of sentence '|' here, but would like to -- */
  125. /* But we do add ascii '|' to it as it sometimes gets used the same way */
  126. feat_set_string(v->features,"text_postpunctuation",
  127. indic_postpunctuationsymbols);
  128. feat_set_string(v->features,"text_singlecharsymbols",
  129. cst_ts_default_singlecharsymbols);
  130. /* Tokenization tokenization function */
  131. feat_set(v->features,"tokentowords_func",itemfunc_val(&cmu_indic_tokentowords));
  132. /* Pos tagger (gpos)/induced pos */
  133. /* Phrasing */
  134. feat_set(v->features,"phrasing_cart",cart_val(&cmu_indic_phrasing_cart));
  135. /* Intonation, Duration and F0 -- part of cg */
  136. feat_set_string(v->features,"no_intonation_accent_model","1");
  137. /* Default ffunctions (required) */
  138. basic_ff_register(v->ffunctions);
  139. return;
  140. }