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