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.

emoji 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/python3
  2. import os
  3. import re
  4. import sys
  5. import math
  6. import codecs
  7. import xml.etree.ElementTree as etree
  8. class Emoji:
  9. def __init__(self, m):
  10. self.emoji = m.group(1)
  11. self.pronunciation = m.group(2)
  12. self.codepoints = m.group(3)
  13. self.comment = m.group(4)
  14. def __repr__(self):
  15. return "Emoji(emoji={0}, pronunciation={1}, codepoints={2}, comment={3})".format(
  16. repr(self.emoji),
  17. repr(self.pronunciation),
  18. repr(self.codepoints),
  19. repr(self.comment))
  20. def __str__(self):
  21. return "{0}{1}// [{2}]{3}".format(self.emoji, self.pronunciation, self.codepoints, self.comment)
  22. def read_annotations(filename):
  23. ldml = etree.parse(filename).getroot()
  24. for annotations in ldml.findall("annotations"):
  25. for annotation in annotations.findall("annotation"):
  26. if annotation.attrib.get("type", "") == "tts":
  27. yield annotation.attrib["cp"], annotation.text
  28. def read_emoji(filename, encoding="utf-8"):
  29. re_emoji = re.compile(r"^([^ \t]*)([^/]*)// \[([^\]]*)\](.*)$")
  30. with codecs.open(filename, "r", encoding) as f:
  31. for line in f:
  32. line = line.replace("\n", "")
  33. if line.strip() == "":
  34. yield line # blank line
  35. elif line.startswith("//"):
  36. yield line # line comment
  37. elif line.startswith("$"):
  38. yield line # flags only
  39. else:
  40. m = re_emoji.match(line)
  41. if m:
  42. yield Emoji(m)
  43. else:
  44. yield line
  45. def find_langname(lang):
  46. espeak_data_path = os.path.join(os.getcwd(), "espeak-ng-data")
  47. for root, dirnames, filenames in os.walk(espeak_data_path):
  48. if lang in filenames:
  49. filename = os.path.join(root, lang)
  50. with codecs.open(filename, "r", "utf-8") as f:
  51. for line in f:
  52. line = line.replace("\n", "")
  53. if line.startswith("name "):
  54. return line.replace("name ", "")
  55. emoji_dict = sys.argv[1]
  56. lang = sys.argv[2]
  57. cldr_path = sys.argv[3]
  58. filenames = [
  59. os.path.join(cldr_path, "common", "annotations", "{0}.xml".format(lang)),
  60. os.path.join(cldr_path, "common", "annotationsDerived", "{0}.xml".format(lang))
  61. ]
  62. annotations = {}
  63. for filename in filenames:
  64. for cp, name in read_annotations(filename):
  65. annotations[cp] = name
  66. for entry in read_emoji(emoji_dict):
  67. if isinstance(entry, Emoji):
  68. translation = annotations.get(entry.emoji.replace("\uFE0F", ""), None)
  69. if translation:
  70. length = len(entry.pronunciation.strip())
  71. tabs = entry.pronunciation.count('\t') - 1
  72. first_tab = 8 - (length % 8)
  73. tab_length = length + first_tab + ((tabs - 1) * 8)
  74. new_length = len(translation)
  75. new_tabs = math.ceil((tab_length - new_length)/8)
  76. entry.pronunciation = "\t{0}{1}".format(translation, "\t"*int(new_tabs))
  77. else:
  78. entry.comment += " (no translation)"
  79. elif entry == "// Emoji and Other Symbol pronunciations for English":
  80. langname = find_langname(lang)
  81. entry = "// Emoji and Other Symbol pronunciations for {0}".format(langname)
  82. elif entry == "// 2. common/annotations/en.xml (CLDR)":
  83. entry = "// 2. common/annotations/{0}.xml (CLDR)".format(lang)
  84. print(entry)