|
| 1 | +from mwl_phonemizer.base import MirandesePhonemizer |
| 2 | + |
| 3 | + |
| 4 | +class LookupTableMWL(MirandesePhonemizer): |
| 5 | + TILDE = "̃" # ◌̃ |
| 6 | + |
| 7 | + LETTERS = { |
| 8 | + "a": ["a", "ɐ"], |
| 9 | + "b": ["b", "β"], |
| 10 | + "c": ["k", "s"], |
| 11 | + "ç": ["s", "z"], |
| 12 | + "d": ["d", "ð"], |
| 13 | + "e": ["ɨ"], |
| 14 | + "é": ["ɛ"], |
| 15 | + "f": ["f"], |
| 16 | + "g": ["ɣ"], |
| 17 | + "h": [""], # silent |
| 18 | + "i": ["i", "j"], |
| 19 | + "j": ["ʒ"], |
| 20 | + "l": ["l", "ɫ"], |
| 21 | + "m": ["m", TILDE, ], |
| 22 | + "n": ["n", "ŋ", TILDE], |
| 23 | + "o": ["u", "o", "ʊ"], |
| 24 | + "ó": ["ɔ"], |
| 25 | + "p": ["p"], |
| 26 | + "q": ["k"], |
| 27 | + "r": ["ɾ"], |
| 28 | + "s": ["s̺", "z̺"], |
| 29 | + "t": ["t"], |
| 30 | + "u": ["u", "w", "ũ"], |
| 31 | + "x": ["ʃ"], |
| 32 | + "y": ["j"], |
| 33 | + "z": ["z"], |
| 34 | + |
| 35 | + "A": ["ɐ̃ŋ"], |
| 36 | + "E": ["ẽŋ", "ɨ̃"], |
| 37 | + "I": ["ĩŋ"], |
| 38 | + "O": ["õŋ"], |
| 39 | + "R": ["r"], |
| 40 | + "S": ["s̺"], |
| 41 | + "U": ["ũŋ", "ʊ̃ŋ"], |
| 42 | + "Q": ["k"], |
| 43 | + "G": ["g"], |
| 44 | + "Ç": ["sɛ", "sɨ"], |
| 45 | + "C": ["s̻i"], |
| 46 | + "W": ["wo"], |
| 47 | + "Z": ["sk"], |
| 48 | + # "I": ["ɨ̃j̃"], # SENDINESE |
| 49 | + } |
| 50 | + |
| 51 | + @staticmethod |
| 52 | + def normalize(sentence: str): |
| 53 | + # normalize short/long pauses to " " and "." |
| 54 | + sentence = (sentence.lower() |
| 55 | + .replace("\t", " ") |
| 56 | + .replace("-", " ") |
| 57 | + .replace(",", " ") |
| 58 | + .replace(";", " ") |
| 59 | + .replace(".", ".") |
| 60 | + .replace("!", ".") |
| 61 | + .replace("?", ".")) |
| 62 | + |
| 63 | + # temp representation of digraphs as individual letters |
| 64 | + DIMAP = { |
| 65 | + "an": "A", |
| 66 | + "en": "E", |
| 67 | + "in": "I", |
| 68 | + "on": "O", |
| 69 | + "un": "U", |
| 70 | + "rr": "R", |
| 71 | + "ss": "S", |
| 72 | + "lh": "ʎ", |
| 73 | + "nh": "ɲ", |
| 74 | + "qu": "Q", |
| 75 | + "gu": "G", |
| 76 | + "gue": "G", |
| 77 | + "Ge": "G", |
| 78 | + "ce": "Ç", |
| 79 | + "ci": "C", |
| 80 | + "uo": "W", |
| 81 | + "çc": "Z", |
| 82 | + "ge": "ʒɨ", |
| 83 | + } |
| 84 | + |
| 85 | + # normalize digraphs |
| 86 | + for di, n in DIMAP.items(): |
| 87 | + sentence = sentence.replace(di, n) |
| 88 | + return sentence |
| 89 | + |
| 90 | + # ------------------------- |
| 91 | + # Phonemizer interface |
| 92 | + # ------------------------- |
| 93 | + def phonemize(self, word: str, lookup_word: bool = True) -> str: |
| 94 | + """Phonemize a single Mirandese word via espeak + correction rules.""" |
| 95 | + if lookup_word and word.lower() in self.GOLD: |
| 96 | + return self.GOLD[word.lower()] |
| 97 | + word = self.normalize(word) |
| 98 | + phonemes = "" |
| 99 | + for idx, char in enumerate(word): |
| 100 | + if char in self.LETTERS: |
| 101 | + pho = self.LETTERS[char][0] |
| 102 | + phonemes += pho |
| 103 | + else: |
| 104 | + phonemes += char |
| 105 | + return phonemes |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + |
| 110 | + pho = LookupTableMWL() |
| 111 | + |
| 112 | + stats = pho.evaluate_on_gold(limit=None, detailed=False, show_changes=False) |
| 113 | + |
| 114 | + # --- Compute PER (Phoneme Error Rate) --- # TODO - move this to evaluate_on_gold |
| 115 | + total_ref_len_stress = sum(len(v) for v in pho.GOLD.values()) |
| 116 | + total_ref_len_no_stress = sum(len(pho.strip_stress(v)) for v in pho.GOLD.values()) |
| 117 | + |
| 118 | + per = stats['avg_edit_distance'] * stats['counts'] / total_ref_len_stress |
| 119 | + |
| 120 | + per_no_stress = stats['avg_edit_distance_no_stress'] * stats['counts'] / total_ref_len_no_stress |
| 121 | + |
| 122 | + # --- Print Summary Metrics --- |
| 123 | + print("\n" + "=" * 50) |
| 124 | + print(" Mirandese Phonemizer Rule Evaluation") |
| 125 | + print("=" * 50) |
| 126 | + print(f"Total Words Evaluated: {stats['counts']}\n") |
| 127 | + |
| 128 | + print("## Phoneme Error Rate (PER, Full IPA Match, includes stress)") |
| 129 | + print(f"PER: {per:.2%}") |
| 130 | + |
| 131 | + print("\n## Phoneme Error Rate (PER, Stress-Agnostic)") |
| 132 | + print(f"PER: {per_no_stress:.2%}") |
| 133 | + |
| 134 | + # --- Print only 'wrong' words (ED > 0) --- |
| 135 | + print("\n--- Incorrectly Phonemized Words (Full IPA Match ED > 0) ---") |
| 136 | + wrong_words = stats.get("details", []) |
| 137 | + |
| 138 | + if wrong_words: |
| 139 | + print(f"Total Incorrect: {len(wrong_words)} words\n") |
| 140 | + |
| 141 | + # Print a header for the detailed list |
| 142 | + print(f"{'Word':<20} | {'Gold':<15} | {'Phonemized':<15} | {'ED After':<8}") |
| 143 | + print("-" * 75) |
| 144 | + |
| 145 | + # Print the detailed list |
| 146 | + for d in wrong_words: |
| 147 | + print( |
| 148 | + f"{d['word']:<20} | {d['gold']:<15} | {d['phonemes']:<15} | {d['ed']:<8}") |
| 149 | + else: |
| 150 | + print("All words achieved an exact match (100% Accuracy)!") |
| 151 | + |
| 152 | + sample_texts = [ |
| 153 | + "Muitas lhénguas ténen proua de ls sous pergaminos antigos, de la lhiteratura screbida hai cientos d'anhos i de scritores hai muito afamados, hoije bandeiras dessas lhénguas. Mas outras hai que nun puoden tener proua de nada desso, cumo ye l causo de la lhéngua mirandesa.", |
| 154 | + "Todos ls seres houmanos nácen lhibres i eiguales an honra i an dreitos. Dotados de rezon i de cuncéncia, dében de se dar bien uns culs outros i cumo armano", |
| 155 | + "Hai más fuogo alhá, i ye deimingo!" |
| 156 | + ] |
| 157 | + for t in sample_texts: |
| 158 | + print(pho.phonemize_sentence(t)) |
0 commit comments