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.

ucd.py 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/python
  2. # Copyright (C) 2012-2017 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. script_map = {}
  21. class CodePoint:
  22. def __init__(self, x):
  23. if isinstance(x, str):
  24. self.codepoint = int(x, 16)
  25. else:
  26. self.codepoint = x
  27. def __repr__(self):
  28. return '%06X' % self.codepoint
  29. def __str__(self):
  30. return '%06X' % self.codepoint
  31. def __iter__(self):
  32. yield self
  33. def __hash__(self):
  34. return self.codepoint
  35. def __eq__(self, other):
  36. return self.codepoint == other.codepoint
  37. def __ne__(self, other):
  38. return self.codepoint != other.codepoint
  39. def __lt__(self, other):
  40. return self.codepoint < other.codepoint
  41. def char(self):
  42. return unichr(self.codepoint)
  43. class CodeRange:
  44. def __init__(self, x):
  45. f, l = x.split('..')
  46. self.first = CodePoint(f)
  47. self.last = CodePoint(l)
  48. def __repr__(self):
  49. return '%s..%s' % (self.first, self.last)
  50. def __str__(self):
  51. return '%s..%s' % (self.first, self.last)
  52. def __iter__(self):
  53. for c in range(self.first.codepoint, self.last.codepoint + 1):
  54. yield CodePoint(c)
  55. def size(self):
  56. return self.last.codepoint - self.first.codepoint + 1
  57. def char(self):
  58. return unichr(self.first.codepoint)
  59. def codepoint(x):
  60. if '..' in x[0]:
  61. return CodeRange(x[0]), x[1:]
  62. if ' ' in x:
  63. return [CodePoint(c) for c in x[0].split()], x[1:]
  64. if x[0] == '':
  65. return CodePoint('0000'), x[1:]
  66. return CodePoint(x[0]), x[1:]
  67. def string(x):
  68. if x[0] == '':
  69. return None, x[1:]
  70. return x[0], x[1:]
  71. def integer(x):
  72. return int(x[0]), x[1:]
  73. def boolean(x):
  74. if x[0] == 'Y':
  75. return True, x[1:]
  76. return False, x[1:]
  77. def script(x):
  78. return script_map[x[0]], x[1:]
  79. def strlist(x):
  80. return x, []
  81. data_items = {
  82. # Unicode Character Data:
  83. 'emoji-data': [
  84. ('Range', codepoint),
  85. ('Property', string)
  86. ],
  87. 'Blocks': [
  88. ('Range', codepoint),
  89. ('Name', string)
  90. ],
  91. 'DerivedAge': [
  92. ('Range', codepoint),
  93. ('Age', string),
  94. ],
  95. 'DerivedCoreProperties': [
  96. ('Range', codepoint),
  97. ('Property', string),
  98. ],
  99. 'PropList': [
  100. ('Range', codepoint),
  101. ('Property', string),
  102. ],
  103. 'PropertyValueAliases': [
  104. ('Property', string),
  105. ('Key', string),
  106. ('Value', string),
  107. ('Aliases', strlist),
  108. ],
  109. 'Scripts': [
  110. ('Range', codepoint),
  111. ('Script', script),
  112. ],
  113. 'UnicodeData': [
  114. ('CodePoint', codepoint),
  115. ('Name', string),
  116. ('GeneralCategory', string),
  117. ('CanonicalCombiningClass', integer),
  118. ('BidiClass', string),
  119. ('DecompositionType', string),
  120. ('DecompositionMapping', string),
  121. ('NumericType', string),
  122. ('NumericValue', string),
  123. ('BidiMirrored', boolean),
  124. ('UnicodeName', string),
  125. ('ISOComment', string),
  126. ('UpperCase', codepoint),
  127. ('LowerCase', codepoint),
  128. ('TitleCase', codepoint),
  129. ],
  130. # ConScript Unicode Registry Data:
  131. 'Klingon': [
  132. ('CodePoint', codepoint),
  133. ('Script', string),
  134. ('GeneralCategory', string),
  135. ('Name', string),
  136. ('Transliteration', string),
  137. ],
  138. }
  139. def parse_ucd_data(ucd_rootdir, dataset):
  140. keys = data_items[dataset]
  141. first = None
  142. with open(os.path.join(ucd_rootdir, '%s.txt' % dataset)) as f:
  143. for line in f:
  144. line = line.replace('\n', '').split('#')[0]
  145. linedata = [' '.join(x.split()) for x in line.split(';')]
  146. if len(linedata) > 1:
  147. if linedata[1].endswith(', First>'):
  148. first = linedata
  149. continue
  150. if linedata[1].endswith(', Last>'):
  151. linedata[0] = '%s..%s' % (first[0], linedata[0])
  152. linedata[1] = linedata[1].replace(', Last>', '').replace('<', '')
  153. first = None
  154. data = {}
  155. for key, typemap in keys:
  156. data[key], linedata = typemap(linedata)
  157. yield data
  158. def parse_property_mapping(ucd_rootdir, propname, reverse=False):
  159. ret = {}
  160. for data in parse_ucd_data(ucd_rootdir, 'PropertyValueAliases'):
  161. if data['Property'] == propname:
  162. if reverse:
  163. ret[data['Value']] = data['Key']
  164. else:
  165. ret[data['Key']] = data['Value']
  166. return ret
  167. if __name__ == '__main__':
  168. try:
  169. items = sys.argv[3].split(',')
  170. except:
  171. items = None
  172. script_map = parse_property_mapping(sys.argv[1], 'sc', reverse=True)
  173. for entry in parse_ucd_data(sys.argv[1], sys.argv[2]):
  174. if items:
  175. print(','.join([str(entry[item]) for item in items]))
  176. else:
  177. print(entry)
  178. else:
  179. script_map = parse_property_mapping('data/ucd', 'sc', reverse=True)