eSpeak NG is an open source speech synthesizer that supports more than hundred languages and accents.
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.

tokenizer.c 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (C) 2017 Reece H. Dunn
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write see:
  16. * <http://www.gnu.org/licenses/>.
  17. */
  18. #include "config.h"
  19. #include <assert.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <espeak-ng/espeak_ng.h>
  25. #include "encoding.h"
  26. #include "tokenizer.h"
  27. #include "speech.h"
  28. #include "phoneme.h"
  29. #include "synthesize.h"
  30. #include "translate.h"
  31. void
  32. test_latin_common()
  33. {
  34. printf("testing Latin/Common (Latn/Zyyy) script classification\n");
  35. assert(clause_type_from_codepoint('a') == CLAUSE_NONE);
  36. assert(clause_type_from_codepoint('.') == CLAUSE_PERIOD);
  37. assert(clause_type_from_codepoint('?') == CLAUSE_QUESTION);
  38. assert(clause_type_from_codepoint('!') == CLAUSE_EXCLAMATION);
  39. assert(clause_type_from_codepoint(',') == CLAUSE_COMMA);
  40. assert(clause_type_from_codepoint(':') == CLAUSE_COLON);
  41. assert(clause_type_from_codepoint(';') == CLAUSE_SEMICOLON);
  42. assert(clause_type_from_codepoint(0x00A1) == (CLAUSE_SEMICOLON | CLAUSE_OPTIONAL_SPACE_AFTER));
  43. assert(clause_type_from_codepoint(0x00Bf) == (CLAUSE_SEMICOLON | CLAUSE_OPTIONAL_SPACE_AFTER));
  44. assert(clause_type_from_codepoint(0x2013) == CLAUSE_SEMICOLON);
  45. assert(clause_type_from_codepoint(0x2014) == CLAUSE_SEMICOLON);
  46. assert(clause_type_from_codepoint(0x2026) == (CLAUSE_SEMICOLON | CLAUSE_SPEAK_PUNCTUATION_NAME | CLAUSE_OPTIONAL_SPACE_AFTER));
  47. }
  48. void
  49. test_greek()
  50. {
  51. printf("testing Greek (Grek) script classification\n");
  52. assert(clause_type_from_codepoint(0x037E) == CLAUSE_QUESTION);
  53. assert(clause_type_from_codepoint(0x0387) == CLAUSE_SEMICOLON);
  54. }
  55. void
  56. test_armenian()
  57. {
  58. printf("testing Armenian (Armn) script classification\n");
  59. assert(clause_type_from_codepoint(0x055B) == (CLAUSE_EXCLAMATION | CLAUSE_PUNCTUATION_IN_WORD));
  60. assert(clause_type_from_codepoint(0x055C) == (CLAUSE_EXCLAMATION | CLAUSE_PUNCTUATION_IN_WORD));
  61. assert(clause_type_from_codepoint(0x055D) == CLAUSE_COMMA);
  62. assert(clause_type_from_codepoint(0x055E) == (CLAUSE_QUESTION | CLAUSE_PUNCTUATION_IN_WORD));
  63. assert(clause_type_from_codepoint(0x0589) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  64. }
  65. void
  66. test_arabic()
  67. {
  68. printf("testing Arabic (Arab) script classification\n");
  69. assert(clause_type_from_codepoint(0x060C) == CLAUSE_COMMA);
  70. assert(clause_type_from_codepoint(0x061B) == CLAUSE_SEMICOLON);
  71. assert(clause_type_from_codepoint(0x061F) == CLAUSE_QUESTION);
  72. assert(clause_type_from_codepoint(0x06D4) == CLAUSE_PERIOD);
  73. }
  74. void
  75. test_devanagari()
  76. {
  77. printf("testing Devanagari (Deva) script classification\n");
  78. assert(clause_type_from_codepoint(0x0964) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  79. }
  80. void
  81. test_tibetan()
  82. {
  83. printf("testing Tibetan (Tibt) script classification\n");
  84. assert(clause_type_from_codepoint(0x0F0D) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  85. assert(clause_type_from_codepoint(0x0F0E) == CLAUSE_PARAGRAPH);
  86. }
  87. void
  88. test_sinhala()
  89. {
  90. printf("testing Sinhala (Sinh) script classification\n");
  91. assert(clause_type_from_codepoint(0x0DF4) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  92. }
  93. void
  94. test_georgian()
  95. {
  96. printf("testing Georgian (Geor) script classification\n");
  97. assert(clause_type_from_codepoint(0x10FB) == CLAUSE_PARAGRAPH);
  98. }
  99. void
  100. test_ethiopic()
  101. {
  102. printf("testing Ethiopic (Ethi) script classification\n");
  103. assert(clause_type_from_codepoint(0x1362) == CLAUSE_PERIOD);
  104. assert(clause_type_from_codepoint(0x1363) == CLAUSE_COMMA);
  105. assert(clause_type_from_codepoint(0x1364) == CLAUSE_SEMICOLON);
  106. assert(clause_type_from_codepoint(0x1365) == CLAUSE_COLON);
  107. assert(clause_type_from_codepoint(0x1366) == CLAUSE_COLON);
  108. assert(clause_type_from_codepoint(0x1367) == CLAUSE_QUESTION);
  109. assert(clause_type_from_codepoint(0x1368) == CLAUSE_PARAGRAPH);
  110. }
  111. void
  112. test_ideographic()
  113. {
  114. printf("testing Ideographic (Hani) script classification\n");
  115. assert(clause_type_from_codepoint(0x3001) == (CLAUSE_COMMA | CLAUSE_OPTIONAL_SPACE_AFTER));
  116. assert(clause_type_from_codepoint(0x3002) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  117. }
  118. void
  119. test_fullwidth()
  120. {
  121. printf("testing Full Width/Common (Zyyy) script classification\n");
  122. assert(clause_type_from_codepoint(0xFF01) == (CLAUSE_EXCLAMATION | CLAUSE_OPTIONAL_SPACE_AFTER));
  123. assert(clause_type_from_codepoint(0xFF0C) == (CLAUSE_COMMA | CLAUSE_OPTIONAL_SPACE_AFTER));
  124. assert(clause_type_from_codepoint(0xFF0E) == (CLAUSE_PERIOD | CLAUSE_OPTIONAL_SPACE_AFTER));
  125. assert(clause_type_from_codepoint(0xFF1A) == (CLAUSE_COLON | CLAUSE_OPTIONAL_SPACE_AFTER));
  126. assert(clause_type_from_codepoint(0xFF1B) == (CLAUSE_SEMICOLON | CLAUSE_OPTIONAL_SPACE_AFTER));
  127. assert(clause_type_from_codepoint(0xFF1F) == (CLAUSE_QUESTION | CLAUSE_OPTIONAL_SPACE_AFTER));
  128. }
  129. void
  130. test_unbound_tokenizer()
  131. {
  132. printf("testing unbound tokenizer\n");
  133. espeak_ng_TOKENIZER *tokenizer = create_tokenizer();
  134. assert(tokenizer != NULL);
  135. assert(tokenizer_get_token_text(tokenizer) != NULL);
  136. assert(*tokenizer_get_token_text(tokenizer) == '\0');
  137. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_END_OF_BUFFER);
  138. assert(tokenizer_get_token_text(tokenizer) != NULL);
  139. assert(*tokenizer_get_token_text(tokenizer) == '\0');
  140. assert(tokenizer_reset(tokenizer, NULL) == 1);
  141. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_END_OF_BUFFER);
  142. assert(tokenizer_get_token_text(tokenizer) != NULL);
  143. assert(*tokenizer_get_token_text(tokenizer) == '\0');
  144. destroy_tokenizer(tokenizer);
  145. }
  146. void
  147. test_linux_newline_tokens()
  148. {
  149. printf("testing linux newline tokens\n");
  150. espeak_ng_TOKENIZER *tokenizer = create_tokenizer();
  151. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  152. assert(text_decoder_decode_string(decoder, "\n\n", -1, ESPEAKNG_ENCODING_US_ASCII) == ENS_OK);
  153. assert(tokenizer_reset(tokenizer, decoder) == 1);
  154. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  155. assert(tokenizer_get_token_text(tokenizer) != NULL);
  156. assert(strcmp(tokenizer_get_token_text(tokenizer), "\n") == 0);
  157. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  158. assert(tokenizer_get_token_text(tokenizer) != NULL);
  159. assert(strcmp(tokenizer_get_token_text(tokenizer), "\n") == 0);
  160. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_END_OF_BUFFER);
  161. assert(tokenizer_get_token_text(tokenizer) != NULL);
  162. assert(*tokenizer_get_token_text(tokenizer) == '\0');
  163. destroy_text_decoder(decoder);
  164. destroy_tokenizer(tokenizer);
  165. }
  166. int
  167. main(int argc, char **argv)
  168. {
  169. test_latin_common();
  170. test_greek();
  171. test_armenian();
  172. test_arabic();
  173. test_devanagari();
  174. test_tibetan();
  175. test_sinhala();
  176. test_georgian();
  177. test_ethiopic();
  178. test_ideographic();
  179. test_fullwidth();
  180. test_unbound_tokenizer();
  181. test_linux_newline_tokens();
  182. printf("done\n");
  183. return EXIT_SUCCESS;
  184. }