| 1 | from collections import Counter |
| 2 | import unicodedata |
| 3 | from google.protobuf import text_format |
| 4 | from gflanguages import languages_public_pb2 |
| 5 | |
| 6 | ATTRIBUTES = "base auxiliary marks punctuation index".split(" ") |
| 7 | |
| 8 | |
| 9 | def main(args=None): |
| 10 | for path in args: |
| 11 | with open(path, encoding="utf-8") as fp: |
| 12 | language = text_format.Parse( |
| 13 | fp.read(), languages_public_pb2.LanguageProto() |
| 14 | ) |
| 15 | changed = False |
| 16 | exemplar_values = {} |
| 17 | bases = language.exemplar_chars.base.split(" ") |
| 18 | marks = language.exemplar_chars.marks.split(" ") |
| 19 | if not len(bases) or bases == [""]: |
| 20 | continue |
| 21 | new_marks = [] |
| 22 | new_bases = [] |
| 23 | for chars in marks: |
| 24 | if not chars: |
| 25 | continue |
| 26 | if chars[0] != "\u25CC": |
| 27 | chars = "\u25CC" + chars |
| 28 | if chars not in new_marks: |
| 29 | new_marks.append(chars) |
| 30 | |
| 31 | for chars in bases: |
| 32 | if not chars: |
| 33 | continue |
| 34 | if chars[0] == "\u25CC": |
| 35 | chars = chars[1:] |
| 36 | cat = unicodedata.category(chars[0]) |
| 37 | if cat in ["Mn", "Mc"]: |
| 38 | if chars[0] != "\u25CC": |
| 39 | chars = "\u25CC" + chars |
| 40 | if chars not in new_marks: |
| 41 | new_marks.append(chars) |
| 42 | else: |
| 43 | new_bases.append(chars) |
| 44 | |
| 45 | language.exemplar_chars.base = " ".join(new_bases) |
| 46 | language.exemplar_chars.marks = " ".join(new_marks) |
| 47 | |
| 48 | with open(path, "w", encoding="utf-8") as fp: |
| 49 | fp.write(text_format.MessageToString(language, as_utf8=True)) |
| 50 | fp.close() |
| 51 | |
| 52 | |
| 53 | if __name__ == "__main__": |
| 54 | import sys |
| 55 | |
| 56 | main(args=sys.argv[1:]) |