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

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