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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. emoji_dict = sys.argv[1]
  46. lang = sys.argv[2]
  47. cldr_path = sys.argv[3]
  48. filenames = [
  49. os.path.join(cldr_path, "common", "annotations", "{0}.xml".format(lang)),
  50. os.path.join(cldr_path, "common", "annotationsDerived", "{0}.xml".format(lang))
  51. ]
  52. annotations = {}
  53. for filename in filenames:
  54. for cp, name in read_annotations(filename):
  55. annotations[cp] = name
  56. for entry in read_emoji(emoji_dict):
  57. if isinstance(entry, Emoji):
  58. translation = annotations.get(entry.emoji, None)
  59. if translation:
  60. length = len(entry.pronunciation.strip())
  61. tabs = entry.pronunciation.count('\t') - 1
  62. first_tab = 8 - (length % 8)
  63. tab_length = length + first_tab + ((tabs - 1) * 8)
  64. new_length = len(translation)
  65. new_tabs = math.ceil((tab_length - new_length)/8)
  66. entry.pronunciation = "\t{0}{1}".format(translation, "\t"*int(new_tabs))
  67. else:
  68. entry.comment += " (no translation)"
  69. print(entry)