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.

iana.py 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. def fold_lines(path):
  20. next_line = None
  21. with open(path) as f:
  22. for line in f:
  23. line = line.replace('\n', '')
  24. if line.startswith(' '):
  25. next_line = '%s%s' % (next_line, line[1:])
  26. continue
  27. if next_line:
  28. yield next_line
  29. next_line = line
  30. def iana_subtag_entries(path):
  31. tag = {}
  32. for line in fold_lines(path):
  33. if line == '%%':
  34. if 'Type' in tag:
  35. yield tag
  36. tag = {}
  37. continue
  38. packed = line.split(': ')
  39. key = packed[0]
  40. value = ': '.join(packed[1:])
  41. if key == 'Description':
  42. # Only select the first Description. This handles subtag codes
  43. # that have multiple descriptions (e.g. 'es' maps to "Spanish"
  44. # and "Castilian").
  45. if not key in tag.keys():
  46. tag[key] = value
  47. else:
  48. tag[key] = value
  49. yield tag
  50. typemap = {
  51. 'extlang': 'ExtLang',
  52. 'grandfathered': 'Grandfathered',
  53. 'language': 'Language',
  54. 'redundant': 'Redundant',
  55. 'region': 'Region',
  56. 'script': 'Script',
  57. 'variant': 'Variant',
  58. }
  59. scopemap = {
  60. 'collection': 'Collection',
  61. 'macrolanguage': 'MacroLanguage',
  62. 'special': 'Special',
  63. 'private-use': 'PrivateUse',
  64. }
  65. def read_iana_subtags(path):
  66. tags = {}
  67. for tag in iana_subtag_entries(path):
  68. if 'Subtag' in tag.keys():
  69. ref = tag['Subtag']
  70. del tag['Subtag']
  71. else:
  72. ref = tag['Tag']
  73. del tag['Tag']
  74. if 'Scope' in tag.keys():
  75. if tag['Type'] != 'language':
  76. raise Exception('"Scope" property unexpected for Type="%s"' % tag['Type'])
  77. tag['Type'] = scopemap[ tag['Scope'] ]
  78. del tag['Scope']
  79. else:
  80. tag['Type'] = typemap[ tag['Type'] ]
  81. if '..' not in ref: # exclude private use definitions
  82. tags[ref] = tag
  83. return tags