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.

categories.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #!/usr/bin/python
  2. # Copyright (C) 2012 Reece H. Dunn
  3. #
  4. # This file is part of ucd-tools.
  5. #
  6. # ucd-tools is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # ucd-tools is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
  18. import os
  19. import sys
  20. import ucd
  21. ucd_rootdir = sys.argv[1]
  22. ucd_version = ucd_rootdir.split('-')[-1]
  23. unicode_chars = {}
  24. for data in ucd.parse_ucd_data(ucd_rootdir, 'UnicodeData'):
  25. for codepoint in data['CodePoint']:
  26. unicode_chars[codepoint] = data['GeneralCategory']
  27. # This map is a combination of the information in the UnicodeData and Blocks
  28. # data files. It is intended to reduce the number of character tables that
  29. # need to be generated.
  30. category_sets = [
  31. (ucd.CodeRange('000000..00D7FF'), None, 'Multiple Blocks'),
  32. (ucd.CodeRange('00D800..00DFFF'), 'Cs', 'Surrogates'),
  33. (ucd.CodeRange('00E000..00F8FF'), 'Co', 'Private Use Area'),
  34. (ucd.CodeRange('00F900..02FAFF'), None, 'Multiple Blocks'),
  35. (ucd.CodeRange('02FB00..0DFFFF'), 'Cn', 'Unassigned'),
  36. (ucd.CodeRange('0E0000..0E01FF'), None, 'Multiple Blocks'),
  37. (ucd.CodeRange('0E0200..0EFFFF'), 'Cn', 'Unassigned'),
  38. (ucd.CodeRange('0F0000..0FFFFD'), 'Co', 'Plane 15 Private Use'),
  39. (ucd.CodeRange('0FFFFE..0FFFFF'), 'Cn', 'Plane 15 Private Use'),
  40. (ucd.CodeRange('100000..10FFFD'), 'Co', 'Plane 16 Private Use'),
  41. (ucd.CodeRange('10FFFE..10FFFF'), 'Cn', 'Plane 16 Private Use'),
  42. ]
  43. # These categories have many pages consisting of just this category:
  44. # Cn -- Unassigned
  45. # Lo -- CJK Ideographs
  46. special_categories = ['Cn', 'Lo']
  47. category_tables = {}
  48. for codepoints, category, comment in category_sets:
  49. if not category:
  50. table = {}
  51. table_entry = None
  52. table_codepoint = None
  53. table_category = None
  54. for i, codepoint in enumerate(codepoints):
  55. try:
  56. category = unicode_chars[codepoint]
  57. except KeyError:
  58. category = 'Cn' # Unassigned
  59. if (i % 256) == 0:
  60. if table_entry:
  61. if table_category in special_categories:
  62. table[table_codepoint] = table_category
  63. else:
  64. table[table_codepoint] = table_entry
  65. table_entry = []
  66. table_codepoint = codepoint
  67. table_category = category
  68. if category != table_category:
  69. table_category = None
  70. table_entry.append(category)
  71. if table_entry:
  72. if table_category in special_categories:
  73. table[table_codepoint] = table_category
  74. else:
  75. table[table_codepoint] = table_entry
  76. category_tables['%s_%s' % (codepoints.first, codepoints.last)] = table
  77. if __name__ == '__main__':
  78. sys.stdout.write("""/* Unicode General Categories
  79. *
  80. * Copyright (C) 2012 Reece H. Dunn
  81. *
  82. * This file is part of ucd-tools.
  83. *
  84. * ucd-tools is free software: you can redistribute it and/or modify
  85. * it under the terms of the GNU General Public License as published by
  86. * the Free Software Foundation, either version 3 of the License, or
  87. * (at your option) any later version.
  88. *
  89. * ucd-tools is distributed in the hope that it will be useful,
  90. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  91. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  92. * GNU General Public License for more details.
  93. *
  94. * You should have received a copy of the GNU General Public License
  95. * along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
  96. */
  97. // NOTE: This file is automatically generated from the UnicodeData.txt file in
  98. // the Unicode Character database by the ucd-tools/tools/categories.py script.
  99. #include "ucd/ucd.h"
  100. #include <stddef.h>
  101. using namespace ucd;
  102. // Unicode Character Data %s
  103. """ % ucd_version)
  104. for category in special_categories:
  105. sys.stdout.write('\n')
  106. sys.stdout.write('static const ucd::category categories_%s[256] =\n' % category)
  107. sys.stdout.write('{')
  108. for i in range(0, 256):
  109. if (i % 16) == 0:
  110. sys.stdout.write('\n\t/* %02X */' % i)
  111. sys.stdout.write(' %s,' % category)
  112. sys.stdout.write('\n};\n')
  113. for codepoints, category, comment in category_sets:
  114. if not category:
  115. tables = category_tables['%s_%s' % (codepoints.first, codepoints.last)]
  116. for codepoint in sorted(tables.keys()):
  117. table = tables[codepoint]
  118. if table in special_categories:
  119. continue
  120. sys.stdout.write('\n')
  121. sys.stdout.write('static const ucd::category categories_%s[256] =\n' % codepoint)
  122. sys.stdout.write('{')
  123. for i, category in enumerate(table):
  124. if (i % 16) == 0:
  125. sys.stdout.write('\n\t/* %02X */' % i)
  126. sys.stdout.write(' %s,' % category)
  127. sys.stdout.write('\n};\n')
  128. for codepoints, category, comment in category_sets:
  129. if not category:
  130. table_index = '%s_%s' % (codepoints.first, codepoints.last)
  131. sys.stdout.write('\n')
  132. sys.stdout.write('static const ucd::category *categories_%s[] =\n' % table_index)
  133. sys.stdout.write('{\n')
  134. for codepoint, table in sorted(category_tables[table_index].items()):
  135. if isinstance(table, str):
  136. sys.stdout.write('\tcategories_%s, // %s\n' % (table, codepoint))
  137. else:
  138. sys.stdout.write('\tcategories_%s,\n' % codepoint)
  139. sys.stdout.write('};\n')
  140. sys.stdout.write('\n')
  141. sys.stdout.write('ucd::category ucd::lookup_category(codepoint_t c)\n')
  142. sys.stdout.write('{\n')
  143. for codepoints, category, comment in category_sets:
  144. if category:
  145. sys.stdout.write('\tif (c <= 0x%s) return %s; // %s : %s\n' % (codepoints.last, category, codepoints, comment))
  146. else:
  147. sys.stdout.write('\tif (c <= 0x%s) // %s\n' % (codepoints.last, codepoints))
  148. sys.stdout.write('\t{\n')
  149. sys.stdout.write('\t\tconst ucd::category *table = categories_%s_%s[(c - 0x%s) / 256];\n' % (codepoints.first, codepoints.last, codepoints.first))
  150. sys.stdout.write('\t\treturn table[c % 256];\n')
  151. sys.stdout.write('\t}\n')
  152. sys.stdout.write('\treturn Ii; // Invalid Unicode Codepoint\n')
  153. sys.stdout.write('}\n')
  154. sys.stdout.write("""
  155. ucd::category_group ucd::lookup_category_group(codepoint_t c)
  156. {
  157. switch (lookup_category(c))
  158. {
  159. case Cc: case Cf: case Cn: case Co: case Cs:
  160. return C;
  161. case Ll: case Lm: case Lo: case Lt: case Lu:
  162. return L;
  163. case Mc: case Me: case Mn:
  164. return M;
  165. case Nd: case Nl: case No:
  166. return N;
  167. case Pc: case Pd: case Pe: case Pf: case Pi: case Po: case Ps:
  168. return P;
  169. case Sc: case Sk: case Sm: case So:
  170. return S;
  171. case Zl: case Zp: case Zs:
  172. return Z;
  173. case Ii:
  174. return I;
  175. }
  176. }
  177. """)