utt_test_main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*************************************************************************/
  2. /* */
  3. /* Language Technologies Institute */
  4. /* Carnegie Mellon University */
  5. /* Copyright (c) 1999 */
  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. /* Author: Alan W Black (awb@cs.cmu.edu) */
  34. /* Date: April 2000 */
  35. /*************************************************************************/
  36. /* */
  37. /* Read some data, build an utt, look up words and extract some feats */
  38. /* Actually doing osmething here */
  39. /* */
  40. /* */
  41. /*************************************************************************/
  42. #include <stdio.h>
  43. #include "cst_hrg.h"
  44. #include "cst_tokenstream.h"
  45. #include "cst_lexicon.h"
  46. extern cst_lexicon cmu_lex;
  47. void cmu_lex_init();
  48. static int bbb_relation_load(cst_relation *r,const char *filename);
  49. static int WordSylSeg(cst_utterance *u);
  50. int main(int argc, char **argv)
  51. {
  52. cst_utterance *u;
  53. cst_relation *r;
  54. cst_item *item=0;
  55. int i;
  56. cmu_lex_init();
  57. u = new_utterance();
  58. r = utt_relation_create(u,"Word");
  59. bbb_relation_load(r,"ttt.txt");
  60. WordSylSeg(u);
  61. for (i=0,item=item_next(relation_head(utt_relation(u,"Segment")));
  62. item; item=item_next(item),i++)
  63. {
  64. printf("Segment %s %s %s %s\n",
  65. ffeature_string(item,"name"),
  66. ffeature_string(item,"n.name"),
  67. ffeature_string(item,"p.name"),
  68. ffeature_string(item,"R:SylStructure.parent.name")
  69. /* ffeature_string(item,"R:SylStructure.parent.R:Word.n.name"), */
  70. /* item_feat_float(item,"duration")); */
  71. );
  72. }
  73. delete_utterance(u);
  74. return 0;
  75. }
  76. static int WordSylSeg(cst_utterance *u)
  77. {
  78. cst_item *word;
  79. cst_relation *sylstructure,*seg,*syl;
  80. cst_val *phones;
  81. const cst_val *p;
  82. cst_item *ssword,*segitem;
  83. syl = utt_relation_create(u,"Syllable");
  84. sylstructure = utt_relation_create(u,"SylStructure");
  85. seg = utt_relation_create(u,"Segment");
  86. for (word=relation_head(utt_relation(u,"Word"));
  87. word; word=item_next(word))
  88. {
  89. printf("word: %s\n",item_feat_string(word,"name"));
  90. ssword = relation_append(sylstructure,word);
  91. phones = lex_lookup((cst_lexicon *)&cmu_lex,item_feat_string(word,"name"),0);
  92. for (p=phones; p; p=val_cdr(p))
  93. {
  94. segitem = relation_append(seg,NULL);
  95. item_set(segitem,"name",val_car(p));
  96. printf("seg: %s\n",item_feat_string(segitem,"name"));
  97. item_add_daughter(ssword,segitem);
  98. }
  99. delete_val_list(phones);
  100. }
  101. return TRUE;
  102. }
  103. static int bbb_relation_load(cst_relation *r,const char *filename)
  104. {
  105. const char *token;
  106. cst_item *item;
  107. cst_tokenstream *fd;
  108. fd = ts_open(filename);
  109. if (fd == 0)
  110. return 0;
  111. while (!ts_eof(fd))
  112. {
  113. token = ts_get(fd);
  114. if (cst_streq(token,""))
  115. continue;
  116. item = relation_append(r,NULL);
  117. item_set_string(item,"name",token);
  118. item_set_string(item,"whitespace",fd->whitespace);
  119. item_set_string(item,"prepunctuation",fd->prepunctuation);
  120. item_set_string(item,"punc",fd->postpunctuation);
  121. item_set_int(item,"file_pos",fd->file_pos);
  122. item_set_int(item,"line_number",fd->line_number);
  123. }
  124. ts_close(fd);
  125. return 1;
  126. }