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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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[0]:
  80. return CodeRange(x[0]), x[1:]
  81. if ' ' in x:
  82. return [CodePoint(c) for c in x[0].split()], x[1:]
  83. if x[0] == '':
  84. return CodePoint('0000'), x[1:]
  85. return CodePoint(x[0]), x[1:]
  86. def string(x):
  87. if x[0] == '':
  88. return None, x[1:]
  89. return x[0], x[1:]
  90. def integer(x):
  91. return int(x[0]), x[1:]
  92. def boolean(x):
  93. if x[0] == 'Y':
  94. return True, x[1:]
  95. return False, x[1:]
  96. def script(x):
  97. return script_map[x[0]], x[1:]
  98. def strlist(x):
  99. return x, []
  100. data_items = {
  101. 'Blocks': [
  102. ('Range', codepoint),
  103. ('Name', string)
  104. ],
  105. 'DerivedAge': [
  106. ('Range', codepoint),
  107. ('Age', string),
  108. ],
  109. 'PropList': [
  110. ('Range', codepoint),
  111. ('Property', string),
  112. ],
  113. 'PropertyValueAliases': [
  114. ('Property', string),
  115. ('Key', string),
  116. ('Value', string),
  117. ('Aliases', strlist),
  118. ],
  119. 'Scripts': [
  120. ('Range', codepoint),
  121. ('Script', script),
  122. ],
  123. 'UnicodeData': [
  124. ('CodePoint', codepoint),
  125. ('Name', string),
  126. ('GeneralCategory', string),
  127. ('CanonicalCombiningClass', integer),
  128. ('BidiClass', string),
  129. ('DecompositionType', string),
  130. ('DecompositionMapping', string),
  131. ('NumericType', string),
  132. ('NumericValue', string),
  133. ('BidiMirrored', boolean),
  134. ('UnicodeName', string),
  135. ('ISOComment', string),
  136. ('UpperCase', codepoint),
  137. ('LowerCase', codepoint),
  138. ('TitleCase', codepoint),
  139. ],
  140. # Supplemental Data:
  141. 'Klingon': [
  142. ('CodePoint', codepoint),
  143. ('Script', string),
  144. ('GeneralCategory', string),
  145. ('Name', string),
  146. ('Transliteration', string),
  147. ],
  148. }
  149. def parse_ucd_data(ucd_rootdir, dataset):
  150. keys = data_items[dataset]
  151. first = None
  152. with open(os.path.join(ucd_rootdir, '%s.txt' % dataset)) as f:
  153. for line in f:
  154. line = line.replace('\n', '').split('#')[0]
  155. linedata = [' '.join(x.split()) for x in line.split(';')]
  156. if len(linedata) > 1:
  157. if linedata[1].endswith(', First>'):
  158. first = linedata
  159. continue
  160. if linedata[1].endswith(', Last>'):
  161. linedata[0] = '%s..%s' % (first[0], linedata[0])
  162. linedata[1] = linedata[1].replace(', Last>', '').replace('<', '')
  163. first = None
  164. data = {}
  165. for key, typemap in keys:
  166. data[key], linedata = typemap(linedata)
  167. yield data
  168. if __name__ == '__main__':
  169. try:
  170. items = sys.argv[3].split(',')
  171. except:
  172. items = None
  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