|
| 1 | +from glob import iglob |
| 2 | +from operator import itemgetter |
| 3 | +from itertools import accumulate |
| 4 | +from collections import defaultdict |
| 5 | + |
| 6 | +non_han = {*',: '} |
| 7 | + |
| 8 | +def is_ascii_lowercase_letter(char): |
| 9 | + return 'a' <= char <= 'z' |
| 10 | + |
| 11 | +def is_ascii_letter(char): |
| 12 | + return 'A' <= char <= 'Z' or 'a' <= char <= 'z' |
| 13 | + |
| 14 | +def column_start(column): |
| 15 | + return len(column) + 1 |
| 16 | + |
| 17 | +listed = set() |
| 18 | +for filename in ['char.csv', 'scripts/ignore.csv']: |
| 19 | + with open(filename, encoding='utf-8') as f: |
| 20 | + assert next(f).startswith('char,jyutping') |
| 21 | + listed |= {tuple(line.rstrip('\n').split(',')[:1]) for line in f} |
| 22 | + |
| 23 | +unlisted = defaultdict(list) |
| 24 | +for filename in iglob("*.csv"): |
| 25 | + if filename == 'char.csv': |
| 26 | + continue |
| 27 | + with open(filename, encoding='utf-8') as f: |
| 28 | + column_names = next(f).rstrip('\n').split(',') |
| 29 | + if "char" not in column_names or "jyutping" not in column_names: |
| 30 | + continue |
| 31 | + chars_column_index = column_names.index("char") |
| 32 | + romans_column_index = column_names.index("jyutping") |
| 33 | + |
| 34 | + for line_num, line in enumerate(f, 2): |
| 35 | + def yield_chars(chars, start): |
| 36 | + s = '' |
| 37 | + is_punctuation = True |
| 38 | + for i, char in enumerate(chars, start): |
| 39 | + if (char in non_han) != is_punctuation: |
| 40 | + if s: |
| 41 | + if not is_punctuation: |
| 42 | + yield (i, s) |
| 43 | + s = '' |
| 44 | + is_punctuation = not is_punctuation |
| 45 | + if not is_punctuation and s and not (is_ascii_lowercase_letter(char) and is_ascii_letter(s[-1])): |
| 46 | + yield (i, s) |
| 47 | + s = '' |
| 48 | + s += char |
| 49 | + if s: |
| 50 | + i += 1 |
| 51 | + if not is_punctuation: |
| 52 | + yield (i, s) |
| 53 | + |
| 54 | + def yield_romans(romans, start): |
| 55 | + s = '' |
| 56 | + is_space = True |
| 57 | + for i, roman in enumerate(romans, start): |
| 58 | + if (roman == ' ') != is_space: |
| 59 | + if s: |
| 60 | + if not is_space: |
| 61 | + yield (i, s) |
| 62 | + s = '' |
| 63 | + is_space = not is_space |
| 64 | + s += roman |
| 65 | + if s: |
| 66 | + i += 1 |
| 67 | + if not is_space: |
| 68 | + yield (i, s) |
| 69 | + |
| 70 | + columns = line.rstrip('\n').split(',') |
| 71 | + columns_start = list(accumulate(map(column_start, columns), initial=0)) |
| 72 | + |
| 73 | + if chars_column_index < len(columns) > romans_column_index: |
| 74 | + chars_orig = columns[chars_column_index] |
| 75 | + romans_orig = columns[romans_column_index] |
| 76 | + chars = yield_chars(chars_orig, columns_start[chars_column_index]) |
| 77 | + romans = yield_romans(romans_orig, columns_start[romans_column_index]) |
| 78 | + for char_i, char in chars: |
| 79 | + try: |
| 80 | + roman_i, roman = next(romans) |
| 81 | + except StopIteration: |
| 82 | + break |
| 83 | + if (char, roman) not in listed: |
| 84 | + unlisted[(char, roman)].append(f'{filename}:{line_num}') |
| 85 | + |
| 86 | +with open('scripts/unlisted.csv', 'w', encoding='utf-8') as f: |
| 87 | + print('char', 'jyutping', 'location', sep=',', file=f) |
| 88 | + for (char, roman), locations in sorted(unlisted.items(), key=itemgetter(0)): |
| 89 | + print(char, roman, ';'.join(locations), sep=',', file=f) |
0 commit comments