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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. void
  167. test_mac_newline_tokens()
  168. {
  169. printf("testing mac newline tokens\n");
  170. espeak_ng_TOKENIZER *tokenizer = create_tokenizer();
  171. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  172. assert(text_decoder_decode_string(decoder, "\r\r", -1, ESPEAKNG_ENCODING_US_ASCII) == ENS_OK);
  173. assert(tokenizer_reset(tokenizer, decoder) == 1);
  174. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  175. assert(tokenizer_get_token_text(tokenizer) != NULL);
  176. assert(strcmp(tokenizer_get_token_text(tokenizer), "\r") == 0);
  177. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  178. assert(tokenizer_get_token_text(tokenizer) != NULL);
  179. assert(strcmp(tokenizer_get_token_text(tokenizer), "\r") == 0);
  180. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_END_OF_BUFFER);
  181. assert(tokenizer_get_token_text(tokenizer) != NULL);
  182. assert(*tokenizer_get_token_text(tokenizer) == '\0');
  183. destroy_text_decoder(decoder);
  184. destroy_tokenizer(tokenizer);
  185. }
  186. void
  187. test_windows_newline_tokens()
  188. {
  189. printf("testing windows newline tokens\n");
  190. espeak_ng_TOKENIZER *tokenizer = create_tokenizer();
  191. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  192. assert(text_decoder_decode_string(decoder, "\r\n\r\n", -1, ESPEAKNG_ENCODING_US_ASCII) == ENS_OK);
  193. assert(tokenizer_reset(tokenizer, decoder) == 1);
  194. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  195. assert(tokenizer_get_token_text(tokenizer) != NULL);
  196. assert(strcmp(tokenizer_get_token_text(tokenizer), "\r\n") == 0);
  197. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  198. assert(tokenizer_get_token_text(tokenizer) != NULL);
  199. assert(strcmp(tokenizer_get_token_text(tokenizer), "\r\n") == 0);
  200. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_END_OF_BUFFER);
  201. assert(tokenizer_get_token_text(tokenizer) != NULL);
  202. assert(*tokenizer_get_token_text(tokenizer) == '\0');
  203. destroy_text_decoder(decoder);
  204. destroy_tokenizer(tokenizer);
  205. }
  206. void
  207. test_unicode_newline_tokens()
  208. {
  209. printf("testing unicode newline tokens\n");
  210. espeak_ng_TOKENIZER *tokenizer = create_tokenizer();
  211. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  212. assert(text_decoder_decode_string(decoder, "\x0C\x0C\xC2\x85\xC2\x85\xE2\x80\xA8\xE2\x80\xA8", -1, ESPEAKNG_ENCODING_UTF_8) == ENS_OK);
  213. assert(tokenizer_reset(tokenizer, decoder) == 1);
  214. // U+000C : FORM FEED (FF) -- Used as a page (not paragraph) break.
  215. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  216. assert(tokenizer_get_token_text(tokenizer) != NULL);
  217. assert(strcmp(tokenizer_get_token_text(tokenizer), "\x0C") == 0);
  218. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  219. assert(tokenizer_get_token_text(tokenizer) != NULL);
  220. assert(strcmp(tokenizer_get_token_text(tokenizer), "\x0C") == 0);
  221. // U+0085 : NEXT LINE (NEL) -- Used in EBCDIC systems as a combined CR+LF character.
  222. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  223. assert(tokenizer_get_token_text(tokenizer) != NULL);
  224. assert(strcmp(tokenizer_get_token_text(tokenizer), "\xC2\x85") == 0);
  225. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  226. assert(tokenizer_get_token_text(tokenizer) != NULL);
  227. assert(strcmp(tokenizer_get_token_text(tokenizer), "\xC2\x85") == 0);
  228. // General Category: Zl -- LINE SEPARATOR
  229. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  230. assert(tokenizer_get_token_text(tokenizer) != NULL);
  231. assert(strcmp(tokenizer_get_token_text(tokenizer), "\xE2\x80\xA8") == 0);
  232. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  233. assert(tokenizer_get_token_text(tokenizer) != NULL);
  234. assert(strcmp(tokenizer_get_token_text(tokenizer), "\xE2\x80\xA8") == 0);
  235. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_END_OF_BUFFER);
  236. assert(tokenizer_get_token_text(tokenizer) != NULL);
  237. assert(*tokenizer_get_token_text(tokenizer) == '\0');
  238. destroy_text_decoder(decoder);
  239. destroy_tokenizer(tokenizer);
  240. }
  241. void
  242. test_paragraph_tokens()
  243. {
  244. printf("testing paragraph tokens\n");
  245. espeak_ng_TOKENIZER *tokenizer = create_tokenizer();
  246. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  247. assert(text_decoder_decode_string(decoder, "\xE2\x80\xA9\xE2\x80\xA9", -1, ESPEAKNG_ENCODING_UTF_8) == ENS_OK);
  248. assert(tokenizer_reset(tokenizer, decoder) == 1);
  249. // General Category: Zp -- PARAGRAPH SEPARATOR
  250. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_PARAGRAPH);
  251. assert(tokenizer_get_token_text(tokenizer) != NULL);
  252. assert(strcmp(tokenizer_get_token_text(tokenizer), "\xE2\x80\xA9") == 0);
  253. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_PARAGRAPH);
  254. assert(tokenizer_get_token_text(tokenizer) != NULL);
  255. assert(strcmp(tokenizer_get_token_text(tokenizer), "\xE2\x80\xA9") == 0);
  256. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_END_OF_BUFFER);
  257. assert(tokenizer_get_token_text(tokenizer) != NULL);
  258. assert(*tokenizer_get_token_text(tokenizer) == '\0');
  259. destroy_text_decoder(decoder);
  260. destroy_tokenizer(tokenizer);
  261. }
  262. void
  263. test_whitespace_tokens()
  264. {
  265. printf("testing whitespace tokens\n");
  266. espeak_ng_TOKENIZER *tokenizer = create_tokenizer();
  267. espeak_ng_TEXT_DECODER *decoder = create_text_decoder();
  268. assert(text_decoder_decode_string(decoder, "\t\t\n\x0B\x0B\n \xE3\x80\x80 \n\xC2\xA0\xC2\xA0", -1, ESPEAKNG_ENCODING_UTF_8) == ENS_OK);
  269. assert(tokenizer_reset(tokenizer, decoder) == 1);
  270. // General Category: Cc, Property: White_Space
  271. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_WHITESPACE);
  272. assert(tokenizer_get_token_text(tokenizer) != NULL);
  273. assert(strcmp(tokenizer_get_token_text(tokenizer), "\t\t") == 0);
  274. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  275. assert(tokenizer_get_token_text(tokenizer) != NULL);
  276. assert(strcmp(tokenizer_get_token_text(tokenizer), "\n") == 0);
  277. // General Category: Cc, Property: White_Space, VERTICAL TAB (VT) -- Not treated as newline tokens.
  278. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_WHITESPACE);
  279. assert(tokenizer_get_token_text(tokenizer) != NULL);
  280. assert(strcmp(tokenizer_get_token_text(tokenizer), "\x0B\x0B") == 0);
  281. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  282. assert(tokenizer_get_token_text(tokenizer) != NULL);
  283. assert(strcmp(tokenizer_get_token_text(tokenizer), "\n") == 0);
  284. // General Category: Zs, Property: White_Space
  285. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_WHITESPACE);
  286. assert(tokenizer_get_token_text(tokenizer) != NULL);
  287. assert(strcmp(tokenizer_get_token_text(tokenizer), " \xE3\x80\x80 ") == 0);
  288. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_NEWLINE);
  289. assert(tokenizer_get_token_text(tokenizer) != NULL);
  290. assert(strcmp(tokenizer_get_token_text(tokenizer), "\n") == 0);
  291. // General Category: Zs, Property: White_Space, Decomposition: <noBreak>
  292. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_WHITESPACE);
  293. assert(tokenizer_get_token_text(tokenizer) != NULL);
  294. assert(strcmp(tokenizer_get_token_text(tokenizer), "\xC2\xA0\xC2\xA0") == 0);
  295. assert(tokenizer_read_next_token(tokenizer) == ESPEAKNG_TOKEN_END_OF_BUFFER);
  296. assert(tokenizer_get_token_text(tokenizer) != NULL);
  297. assert(*tokenizer_get_token_text(tokenizer) == '\0');
  298. destroy_text_decoder(decoder);
  299. destroy_tokenizer(tokenizer);
  300. }
  301. int
  302. main(int argc, char **argv)
  303. {
  304. test_latin_common();
  305. test_greek();
  306. test_armenian();
  307. test_arabic();
  308. test_devanagari();
  309. test_tibetan();
  310. test_sinhala();
  311. test_georgian();
  312. test_ethiopic();
  313. test_ideographic();
  314. test_fullwidth();
  315. test_unbound_tokenizer();
  316. test_linux_newline_tokens();
  317. test_mac_newline_tokens();
  318. test_windows_newline_tokens();
  319. test_unicode_newline_tokens();
  320. test_paragraph_tokens();
  321. test_whitespace_tokens();
  322. printf("done\n");
  323. return EXIT_SUCCESS;
  324. }