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.4KB

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