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.5KB

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