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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 iana
  21. script_map = {
  22. # UCD script names not derivable from IANA script tags:
  23. 'Canadian_Aboriginal': 'Cans',
  24. 'Common': 'Zyyy',
  25. 'Egyptian_Hieroglyphs': 'Egyp',
  26. 'Inherited': 'Zyyy',
  27. 'Meetei_Mayek': 'Mtei',
  28. 'Nko': 'Nkoo',
  29. 'Phags_Pa': 'Phag',
  30. # Codes in http://www.unicode.org/iso15924/iso15924-codes.html not in IANA:
  31. 'Cuneiform': 'Xsux',
  32. 'Duployan': 'Dupl',
  33. }
  34. for ref, tag in iana.read_iana_subtags('data/language-subtag-registry').items():
  35. if tag['Type'] == 'Script':
  36. # Convert the IANA scipt tag descriptions to the UCD script names:
  37. desc = tag['Description']
  38. if ' (' in desc:
  39. desc = desc.split(' (')[0]
  40. desc = desc.replace(' ', '_')
  41. script_map[desc] = ref
  42. # Fix up incorrectly mapped script names:
  43. script_map['Cyrillic'] = 'Cyrl'
  44. class CodePoint:
  45. def __init__(self, x):
  46. if isinstance(x, str):
  47. self.codepoint = int(x, 16)
  48. else:
  49. self.codepoint = x
  50. def __repr__(self):
  51. return '%06X' % self.codepoint
  52. def __str__(self):
  53. return '%06X' % self.codepoint
  54. def __iter__(self):
  55. yield self
  56. def __hash__(self):
  57. return self.codepoint
  58. def __eq__(self, other):
  59. return self.codepoint == other.codepoint
  60. def __ne__(self, other):
  61. return self.codepoint != other.codepoint
  62. def __lt__(self, other):
  63. return self.codepoint < other.codepoint
  64. class CodeRange:
  65. def __init__(self, x):
  66. f, l = x.split('..')
  67. self.first = CodePoint(f)
  68. self.last = CodePoint(l)
  69. def __repr__(self):
  70. return '%s..%s' % (self.first, self.last)
  71. def __str__(self):
  72. return '%s..%s' % (self.first, self.last)
  73. def __iter__(self):
  74. for c in range(self.first.codepoint, self.last.codepoint + 1):
  75. yield CodePoint(c)
  76. def size(self):
  77. return self.last.codepoint - self.first.codepoint + 1
  78. def codepoint(x):
  79. if '..' in x:
  80. return CodeRange(x)
  81. if ' ' in x:
  82. return [CodePoint(c) for c in x.split()]
  83. if x == '':
  84. return CodePoint('0000')
  85. return CodePoint(x)
  86. def string(x):
  87. if x == '':
  88. return None
  89. return x
  90. def boolean(x):
  91. if x == 'Y':
  92. return True
  93. return False
  94. def script(x):
  95. return script_map[x]
  96. data_items = {
  97. 'Blocks': [
  98. ('Range', codepoint),
  99. ('Name', str)
  100. ],
  101. 'DerivedAge': [
  102. ('Range', codepoint),
  103. ('Age', str),
  104. ],
  105. 'PropList': [
  106. ('Range', codepoint),
  107. ('Property', str),
  108. ],
  109. 'Scripts': [
  110. ('Range', codepoint),
  111. ('Script', script),
  112. ],
  113. 'UnicodeData': [
  114. ('CodePoint', codepoint),
  115. ('Name', string),
  116. ('GeneralCategory', string),
  117. ('CanonicalCombiningClass', int),
  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. # Supplemental Data:
  131. 'Klingon': [
  132. ('CodePoint', codepoint),
  133. ('Script', str),
  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) == len(keys):
  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 keydata, value in zip(keys, linedata):
  156. key, typemap = keydata
  157. if key:
  158. data[key] = typemap(value)
  159. yield data
  160. if __name__ == '__main__':
  161. try:
  162. items = sys.argv[3].split(',')
  163. except:
  164. items = None
  165. for entry in parse_ucd_data(sys.argv[1], sys.argv[2]):
  166. if items:
  167. print ','.join([str(entry[item]) for item in items])
  168. else:
  169. print entry