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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. class CodePoint:
  21. def __init__(self, x):
  22. if isinstance(x, str):
  23. self.codepoint = int(x, 16)
  24. else:
  25. self.codepoint = x
  26. def __repr__(self):
  27. return '%06X' % self.codepoint
  28. def __str__(self):
  29. return '%06X' % self.codepoint
  30. def __hash__(self):
  31. return self.codepoint
  32. def __eq__(self, other):
  33. return self.codepoint == other.codepoint
  34. def __ne__(self, other):
  35. return self.codepoint != other.codepoint
  36. def __lt__(self, other):
  37. return self.codepoint < other.codepoint
  38. class CodeRange:
  39. def __init__(self, x):
  40. f, l = x.split('..')
  41. self.first = CodePoint(f)
  42. self.last = CodePoint(l)
  43. def __repr__(self):
  44. return '%s..%s' % (self.first, self.last)
  45. def __str__(self):
  46. return '%s..%s' % (self.first, self.last)
  47. def __iter__(self):
  48. for c in range(self.first.codepoint, self.last.codepoint + 1):
  49. yield CodePoint(c)
  50. def size(self):
  51. return self.last.codepoint - self.first.codepoint + 1
  52. def codepoint(x):
  53. if '..' in x:
  54. return CodeRange(x)
  55. if ' ' in x:
  56. return [CodePoint(c) for c in x.split()]
  57. if x == '':
  58. return None
  59. return CodePoint(x)
  60. def string(x):
  61. if x == '':
  62. return None
  63. return x
  64. def boolean(x):
  65. if x == 'Y':
  66. return True
  67. return False
  68. data_items = {
  69. 'Blocks': [
  70. ('Range', codepoint),
  71. ('Name', str)
  72. ],
  73. 'DerivedAge': [
  74. ('Range', codepoint),
  75. ('Age', str),
  76. ],
  77. 'PropList': [
  78. ('Range', codepoint),
  79. ('Property', str),
  80. ],
  81. 'Scripts': [
  82. ('Range', codepoint),
  83. ('Script', str),
  84. ],
  85. 'UnicodeData': [
  86. ('CodePoint', codepoint),
  87. ('Name', string),
  88. ('GeneralCategory', string),
  89. ('CanonicalCombiningClass', int),
  90. ('BidiClass', string),
  91. ('DecompositionType', string),
  92. ('DecompositionMapping', string),
  93. ('NumericType', string),
  94. ('NumericValue', string),
  95. ('BidiMirrored', boolean),
  96. ('UnicodeName', string),
  97. ('ISOComment', string),
  98. ('UpperCase', codepoint),
  99. ('LowerCase', codepoint),
  100. ('TitleCase', codepoint),
  101. ],
  102. }
  103. def parse_ucd_data(ucd_rootdir, dataset):
  104. keys = data_items[dataset]
  105. first = None
  106. with open(os.path.join(ucd_rootdir, '%s.txt' % dataset)) as f:
  107. for line in f:
  108. line = line.replace('\n', '').split('#')[0]
  109. linedata = [' '.join(x.split()) for x in line.split(';')]
  110. if len(linedata) == len(keys):
  111. if linedata[1].endswith(', First>'):
  112. first = linedata
  113. continue
  114. if linedata[1].endswith(', Last>'):
  115. linedata[0] = '%s..%s' % (first[0], linedata[0])
  116. linedata[1] = linedata[1].replace(', Last>', '').replace('<', '')
  117. first = None
  118. data = {}
  119. for keydata, value in zip(keys, linedata):
  120. key, typemap = keydata
  121. if key:
  122. data[key] = typemap(value)
  123. yield data
  124. if __name__ == '__main__':
  125. for entry in parse_ucd_data(sys.argv[1], sys.argv[2]):
  126. print entry