| 1 |
from gflanguages import languages_public_pb2 |
| 2 |
import enum |
| 3 |
import re |
| 4 |
|
| 5 |
|
| 6 |
class Udhr: |
| 7 |
def __init__( |
| 8 |
self, key, iso639_3, iso15924, bcp47, direction, ohchr, stage, loc, name |
| 9 |
): |
| 10 |
self.key = key |
| 11 |
self.iso639_3 = iso639_3 |
| 12 |
self.iso15924 = iso15924 |
| 13 |
self.bcp47 = bcp47 |
| 14 |
self.direction = direction |
| 15 |
self.ohchr = ohchr |
| 16 |
self.stage = stage |
| 17 |
self.loc = loc |
| 18 |
self.name = name |
| 19 |
|
| 20 |
self.title = None |
| 21 |
self.preamble = None |
| 22 |
self.articles = [] |
| 23 |
|
| 24 |
def Parse(self, translation_data): |
| 25 |
if translation_data is None or self.stage < 2: |
| 26 |
return |
| 27 |
|
| 28 |
if translation_data.find("./{*}title") is not None: |
| 29 |
self.title = translation_data.find("./{*}title").text |
| 30 |
|
| 31 |
preamble_data = translation_data.find("./{*}preamble") |
| 32 |
if preamble_data is not None: |
| 33 |
if preamble_data.find("./{*}title") is not None: |
| 34 |
self.preamble = { |
| 35 |
"title": preamble_data.find("./{*}title").text, |
| 36 |
"content": [ |
| 37 |
para.text for para in preamble_data.findall("./{*}para") |
| 38 |
], |
| 39 |
} |
| 40 |
|
| 41 |
articles_data = translation_data.findall("./{*}article") |
| 42 |
for article_data in articles_data: |
| 43 |
title_data = article_data.find("./{*}title") |
| 44 |
article = { |
| 45 |
"id": int(article_data.get("number")), |
| 46 |
"title": None if title_data is None else title_data.text, |
| 47 |
"content": [para.text for para in article_data.findall("./{*}para")], |
| 48 |
} |
| 49 |
self.articles.append(article) |
| 50 |
|
| 51 |
def LoadArticleOne(self, article_one): |
| 52 |
self.articles.append({"id": 0, "title": None, "content": [article_one]}) |
| 53 |
|
| 54 |
def GetSampleTexts(self): |
| 55 |
extractor = SampleTextExtractor(self) |
| 56 |
return extractor.GetSampleTexts() |
| 57 |
|
| 58 |
|
| 59 |
class SampleTextExtractor: |
| 60 |
class TextType(enum.Enum): |
| 61 |
GLYPHS = 1 |
| 62 |
WORD = 2 |
| 63 |
PHRASE = 3 |
| 64 |
SENTENCE = 4 |
| 65 |
PARAGRAPH = 5 |
| 66 |
PASSAGE = 6 |
| 67 |
|
| 68 |
def __init__(self, udhr): |
| 69 |
self._udhr = udhr |
| 70 |
self._glyphs = iter(self._GetGlyphs()) |
| 71 |
self._words = iter(self._GetWords()) |
| 72 |
self._paragraphs = iter(self._GetParagraphs()) |
| 73 |
self._phrase_history = set() |
| 74 |
|
| 75 |
self._non_word_regex = re.compile(r"[^\w]+") |
| 76 |
self._space_regex = re.compile(r"\s+") |
| 77 |
self._non_space_regex = re.compile(r"[^\s]+") |
| 78 |
self._non_word_space_regex = re.compile(r"[^\w\s]+") |
| 79 |
self._any_regex = re.compile(r".") |
| 80 |
|
| 81 |
def _DisplayLength(self, s): |
| 82 |
"""Returns length of given string. Omits combining characters. |
| 83 |
|
| 84 |
Some entire scripts will not be counted; in those cases, the raw length of |
| 85 |
the string is returned. |
| 86 |
""" |
| 87 |
word_space_length = len(self._non_word_space_regex.sub("", s)) |
| 88 |
space_length = len(self._non_space_regex.sub("", s)) |
| 89 |
if word_space_length == space_length: |
| 90 |
return len(s) |
| 91 |
return word_space_length |
| 92 |
|
| 93 |
def _GetGlyphs(self): |
| 94 |
seen = set() |
| 95 |
for article in self._udhr.articles: |
| 96 |
for para in article["content"]: |
| 97 |
for ch in self._non_word_regex.sub("", para) or self._space_regex.sub( |
| 98 |
"", para |
| 99 |
): |
| 100 |
ch = ch.lower() |
| 101 |
if ch not in seen: |
| 102 |
seen.add(ch) |
| 103 |
yield ch |
| 104 |
|
| 105 |
def _GetWords(self): |
| 106 |
if self._space_regex.search(self._udhr.articles[0]["content"][0]) is not None: |
| 107 |
splitter = self._space_regex |
| 108 |
else: |
| 109 |
splitter = self._non_word_regex |
| 110 |
|
| 111 |
seen = set() |
| 112 |
for article in self._udhr.articles: |
| 113 |
for para in article["content"]: |
| 114 |
for s in splitter.split(para): |
| 115 |
if s not in seen: |
| 116 |
seen.add(s) |
| 117 |
yield s |
| 118 |
|
| 119 |
def _GetParagraphs(self): |
| 120 |
if self._udhr.preamble is not None: |
| 121 |
for para in self._udhr.preamble["content"]: |
| 122 |
yield para |
| 123 |
for article in self._udhr.articles: |
| 124 |
for para in article["content"]: |
| 125 |
yield para |
| 126 |
|
| 127 |
def _ExtractGlyphs(self, min_chars, max_chars): |
| 128 |
s = "" |
| 129 |
for ch in self._glyphs: |
| 130 |
s += ch.upper() |
| 131 |
if len(s) >= min_chars: |
| 132 |
break |
| 133 |
if ch != ch.upper(): |
| 134 |
s += ch |
| 135 |
if len(s) >= min_chars: |
| 136 |
break |
| 137 |
return s |
| 138 |
|
| 139 |
def _ExtractWord(self, min_chars, max_chars): |
| 140 |
for iterator in [self._words, self._GetWords()]: |
| 141 |
for w in iterator: |
| 142 |
if w is None: |
| 143 |
continue |
| 144 |
if min_chars <= self._DisplayLength(w) <= max_chars: |
| 145 |
return w |
| 146 |
|
| 147 |
return self._ExtractPhrase(min_chars, max_chars) |
| 148 |
|
| 149 |
def _ExtractPhrase(self, min_chars, max_chars): |
| 150 |
for iterator in [self._paragraphs, self._GetParagraphs()]: |
| 151 |
for para in iterator: |
| 152 |
if para is None: |
| 153 |
continue |
| 154 |
for regex in [self._any_regex, self._space_regex, self._non_word_regex]: |
| 155 |
breaks = [-1] |
| 156 |
for match in regex.finditer(para, min_chars): |
| 157 |
breaks.append(match.start()) |
| 158 |
phrase = para[breaks[0] + 1 : breaks[len(breaks) - 1]] |
| 159 |
p_size = self._DisplayLength(phrase) |
| 160 |
while p_size > max_chars and len(breaks) > 1: |
| 161 |
breaks.pop() |
| 162 |
phrase = para[breaks[0] + 1 : breaks[len(breaks) - 1]] |
| 163 |
p_size = self._DisplayLength(phrase) |
| 164 |
if min_chars <= p_size and phrase not in self._phrase_history: |
| 165 |
self._phrase_history.add(phrase) |
| 166 |
return phrase |
| 167 |
return self._ExtractParagraph(min_chars, max_chars) |
| 168 |
|
| 169 |
def _ExtractSentence(self, min_chars, max_chars): |
| 170 |
|
| 171 |
|
| 172 |
return self._ExtractPhrase(min_chars, max_chars) |
| 173 |
|
| 174 |
def _ExtractParagraph(self, min_chars, max_chars): |
| 175 |
for iterator in [self._paragraphs, self._GetParagraphs()]: |
| 176 |
for para in iterator: |
| 177 |
if para is None: |
| 178 |
continue |
| 179 |
if min_chars <= self._DisplayLength(para) <= max_chars: |
| 180 |
return para |
| 181 |
|
| 182 |
return self._ExtractPassage(min_chars, max_chars) |
| 183 |
|
| 184 |
def _ExtractPassage(self, min_chars, max_chars): |
| 185 |
p = [] |
| 186 |
p_size = 0 |
| 187 |
while p_size < min_chars: |
| 188 |
for iterator in [self._paragraphs, self._GetParagraphs()]: |
| 189 |
for para in iterator: |
| 190 |
if para is None: |
| 191 |
continue |
| 192 |
p.append(para) |
| 193 |
p_size = self._DisplayLength(" ".join(p)) |
| 194 |
if max_chars < p_size: |
| 195 |
p.pop() |
| 196 |
elif min_chars <= p_size: |
| 197 |
return "\n".join(p) |
| 198 |
assert len(p) > 0, "Unable to extract passage: " + self._udhr.key |
| 199 |
if len(p) == 0: |
| 200 |
p.append([p for p in self._GetParagraphs()][0]) |
| 201 |
return "\n".join(p) |
| 202 |
|
| 203 |
def _Get(self, text_type, **kwargs): |
| 204 |
if "char_count" in kwargs: |
| 205 |
min_chars = kwargs["char_count"] |
| 206 |
max_chars = kwargs["char_count"] |
| 207 |
else: |
| 208 |
min_chars = kwargs["min_chars"] |
| 209 |
max_chars = kwargs["max_chars"] |
| 210 |
if text_type == self.TextType.GLYPHS: |
| 211 |
return self._ExtractGlyphs(min_chars, max_chars) |
| 212 |
if text_type == self.TextType.WORD: |
| 213 |
return self._ExtractWord(min_chars, max_chars) |
| 214 |
if text_type == self.TextType.PHRASE: |
| 215 |
return self._ExtractPhrase(min_chars, max_chars) |
| 216 |
if text_type == self.TextType.SENTENCE: |
| 217 |
return self._ExtractSentence(min_chars, max_chars) |
| 218 |
if text_type == self.TextType.PARAGRAPH: |
| 219 |
return self._ExtractParagraph(min_chars, max_chars) |
| 220 |
if text_type == self.TextType.PASSAGE: |
| 221 |
return self._ExtractPassage(min_chars, max_chars) |
| 222 |
raise Exception("Unsupported text type: " + text_type) |
| 223 |
|
| 224 |
def GetSampleTexts(self): |
| 225 |
sample_text = languages_public_pb2.SampleTextProto() |
| 226 |
sample_text.masthead_full = self._Get(self.TextType.GLYPHS, char_count=4) |
| 227 |
sample_text.masthead_partial = self._Get(self.TextType.GLYPHS, char_count=2) |
| 228 |
sample_text.styles = self._Get(self.TextType.PHRASE, min_chars=40, max_chars=60) |
| 229 |
sample_text.tester = self._Get(self.TextType.PHRASE, min_chars=60, max_chars=90) |
| 230 |
sample_text.poster_sm = self._Get( |
| 231 |
self.TextType.PHRASE, min_chars=10, max_chars=17 |
| 232 |
) |
| 233 |
sample_text.poster_md = self._Get( |
| 234 |
self.TextType.PHRASE, min_chars=6, max_chars=12 |
| 235 |
) |
| 236 |
sample_text.poster_lg = self._Get(self.TextType.WORD, min_chars=3, max_chars=8) |
| 237 |
sample_text.specimen_48 = self._Get( |
| 238 |
self.TextType.SENTENCE, min_chars=50, max_chars=80 |
| 239 |
) |
| 240 |
sample_text.specimen_36 = self._Get( |
| 241 |
self.TextType.PARAGRAPH, min_chars=100, max_chars=120 |
| 242 |
) |
| 243 |
sample_text.specimen_32 = self._Get( |
| 244 |
self.TextType.PARAGRAPH, min_chars=140, max_chars=180 |
| 245 |
) |
| 246 |
sample_text.specimen_21 = self._Get( |
| 247 |
self.TextType.PASSAGE, min_chars=300, max_chars=500 |
| 248 |
) |
| 249 |
sample_text.specimen_16 = self._Get( |
| 250 |
self.TextType.PASSAGE, min_chars=550, max_chars=750 |
| 251 |
) |
| 252 |
return sample_text |