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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. annotations = {}
  46. for cp, name in read_annotations(sys.argv[2]):
  47. annotations[cp] = name
  48. for entry in read_emoji(sys.argv[1]):
  49. if isinstance(entry, Emoji):
  50. translation = annotations.get(entry.emoji, None)
  51. if translation:
  52. length = len(entry.pronunciation.strip())
  53. tabs = entry.pronunciation.count('\t') - 1
  54. first_tab = 8 - (length % 8)
  55. tab_length = length + first_tab + ((tabs - 1) * 8)
  56. new_length = len(translation)
  57. new_tabs = math.ceil((tab_length - new_length)/8)
  58. entry.pronunciation = "\t{0}{1}".format(translation, "\t"*int(new_tabs))
  59. else:
  60. entry.comment += " (no translation)"
  61. print(entry)