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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
|
| 3 |
+
# ==========================================
|
| 4 |
+
# 1. CONSTANTS (Must Match V7 Training Data)
|
| 5 |
+
# ==========================================
|
| 6 |
+
|
| 7 |
+
# The exact 64-phoneme interleaved list from AMLM V7
|
| 8 |
+
PHONEME_LIST = [
|
| 9 |
+
# --- STOPS ---
|
| 10 |
+
'p', 'pʰ', 'b', 'bʰ',
|
| 11 |
+
't', 'tʰ', 'd', 'dʰ',
|
| 12 |
+
't̪', 't̪ʰ', 'd̪', 'd̪ʱ',
|
| 13 |
+
'ʈ', 'ɖ',
|
| 14 |
+
'k', 'kʰ', 'g', 'ɡʰ',
|
| 15 |
+
'q', 'ʔ',
|
| 16 |
+
# --- FRICATIVES ---
|
| 17 |
+
'f', 'v',
|
| 18 |
+
's', 'z',
|
| 19 |
+
'ʃ', 'ʒ', 'ʂ', 'ʐ',
|
| 20 |
+
'x', 'ɣ', 'h',
|
| 21 |
+
'θ', 'ð',
|
| 22 |
+
# --- AFFRICATES ---
|
| 23 |
+
'tʃ', 'dʒ',
|
| 24 |
+
# --- NASALS ---
|
| 25 |
+
'm', 'mʰ', 'n', 'nʰ', 'ŋ', 'ɳ',
|
| 26 |
+
# --- LIQUIDS ---
|
| 27 |
+
'l', 'lʰ', 'r', 'rʰ', 'ɽ', 'ɽʰ',
|
| 28 |
+
# --- GLIDES ---
|
| 29 |
+
'w', 'j',
|
| 30 |
+
# --- VOWELS ---
|
| 31 |
+
'a', 'aː', 'i', 'iː', 'u', 'uː', 'e', 'eː', 'o', 'oː', 'ə', 'æ', 'ɔ', 'ɪ', 'ʊ'
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
# Map symbol to index (0-63)
|
| 35 |
+
P2I = {p: i for i, p in enumerate(PHONEME_LIST)}
|
| 36 |
+
|
| 37 |
+
# Error Column Indices (0-8)
|
| 38 |
+
ERROR_MAP = {
|
| 39 |
+
"Sub": 0, "Substitution": 0,
|
| 40 |
+
"Front": 1, "Fronting": 1, "Velar Fronting": 1, "Palatal Fronting": 1,
|
| 41 |
+
"Stop": 2, "Stopping": 2,
|
| 42 |
+
"Glide": 3, "Gliding": 3,
|
| 43 |
+
"Clust": 4, "Cluster Reduction": 4,
|
| 44 |
+
"Epen": 5, "Epenthesis": 5,
|
| 45 |
+
"Block": 6, "Blocking": 6, "BlockBetweenWords": 6, "BlockWithinWord": 6,
|
| 46 |
+
"Prolong": 7, "Prolongation": 7, "ProlongationEvent": 7,
|
| 47 |
+
"Repeat": 8, "Repetition": 8, "RepetitionEvent": 8, "WordRepetition": 8, "SyllableRepetition": 8, "SoundRepetition": 8
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
POS_NAMES = ["Init", "Med", "Fin"]
|
| 51 |
+
|
| 52 |
+
class TensorBridge:
|
| 53 |
+
def __init__(self):
|
| 54 |
+
self.num_channels = 3
|
| 55 |
+
self.num_phonemes = 64
|
| 56 |
+
self.num_errors = 9
|
| 57 |
+
# Cleaning map for backend IPA artifacts
|
| 58 |
+
self.ipa_cleaning_map = {
|
| 59 |
+
"g": "g", "ɡ": "g", # Unify various 'g' unicode chars
|
| 60 |
+
"r": "r", "ɾ": "r", # Tap vs Trill unification
|
| 61 |
+
"t_dental": "t̪", "d_dental": "d̪",
|
| 62 |
+
"t_retro": "ʈ", "d_retro": "ɖ"
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
def initialize_tensor(self):
|
| 66 |
+
"""Returns a zeroed 3D tensor (List format for JSON compatibility)."""
|
| 67 |
+
# Shape: (3, 64, 9)
|
| 68 |
+
return np.zeros((3, 64, 9), dtype=np.float32).tolist()
|
| 69 |
+
|
| 70 |
+
def _unify_phoneme(self, symbol):
|
| 71 |
+
"""Maps backend IPA (potentially messy) to V7 Tensor tokens."""
|
| 72 |
+
if not symbol: return None
|
| 73 |
+
|
| 74 |
+
# 1. Direct Match
|
| 75 |
+
if symbol in P2I: return symbol
|
| 76 |
+
|
| 77 |
+
# 2. Cleaning Map
|
| 78 |
+
if symbol in self.ipa_cleaning_map:
|
| 79 |
+
mapped = self.ipa_cleaning_map[symbol]
|
| 80 |
+
if mapped in P2I: return mapped
|
| 81 |
+
|
| 82 |
+
# 3. Fallback: Diacritic stripping (Try base character)
|
| 83 |
+
base = symbol[0]
|
| 84 |
+
if base in P2I: return base
|
| 85 |
+
|
| 86 |
+
return None
|
| 87 |
+
|
| 88 |
+
def _determine_position(self, event_dict):
|
| 89 |
+
"""
|
| 90 |
+
Determines position index (0=Init, 1=Med, 2=Fin).
|
| 91 |
+
Input: dict with keys 'position' (str) OR 'word' + 'index'.
|
| 92 |
+
"""
|
| 93 |
+
# Explicit String from Backend
|
| 94 |
+
pos_str = event_dict.get('position', '').lower()
|
| 95 |
+
if 'init' in pos_str or 'start' in pos_str: return 0
|
| 96 |
+
if 'med' in pos_str or 'mid' in pos_str: return 1
|
| 97 |
+
if 'fin' in pos_str or 'end' in pos_str: return 2
|
| 98 |
+
|
| 99 |
+
# Implicit Calculation from Word Index
|
| 100 |
+
word = event_dict.get('word_ipa', '') or event_dict.get('word', '')
|
| 101 |
+
idx = event_dict.get('char_index', -1)
|
| 102 |
+
|
| 103 |
+
if word and idx != -1:
|
| 104 |
+
if idx == 0: return 0
|
| 105 |
+
if idx == len(word) - 1: return 2
|
| 106 |
+
return 1 # Medial default
|
| 107 |
+
|
| 108 |
+
return 0 # Default to Initial if unknown (safest assumption)
|
| 109 |
+
|
| 110 |
+
def parse_backend_output(self, analysis_data):
|
| 111 |
+
"""
|
| 112 |
+
Converts Main Backend output (Test or Practice) into standardized Diagnostic Events.
|
| 113 |
+
|
| 114 |
+
Output format: List of { 'phoneme_idx': int, 'error_idx': int, 'pos_idx': int }
|
| 115 |
+
"""
|
| 116 |
+
events = []
|
| 117 |
+
|
| 118 |
+
# Determine if this is a full AnalysisResponse or simple list
|
| 119 |
+
raw_list = analysis_data if isinstance(analysis_data, list) else analysis_data.get('errors', [])
|
| 120 |
+
|
| 121 |
+
for item in raw_list:
|
| 122 |
+
# 1. Identify Error Type
|
| 123 |
+
proc_name = item.get('process') or item.get('type') or item.get('error_name')
|
| 124 |
+
if not proc_name: continue
|
| 125 |
+
|
| 126 |
+
# Fuzzy match error name
|
| 127 |
+
err_idx = -1
|
| 128 |
+
for key, val in ERROR_MAP.items():
|
| 129 |
+
if key.lower() in proc_name.lower():
|
| 130 |
+
err_idx = val
|
| 131 |
+
break
|
| 132 |
+
if err_idx == -1: continue # Unknown error type
|
| 133 |
+
|
| 134 |
+
# 2. Identify Target Phoneme
|
| 135 |
+
# For SODA: 'expected' is the target. For Fluency: 'phone' or 'sound'.
|
| 136 |
+
target_char = item.get('expected') or item.get('phone') or item.get('target')
|
| 137 |
+
|
| 138 |
+
# Clean and Map
|
| 139 |
+
clean_char = self._unify_phoneme(target_char)
|
| 140 |
+
if not clean_char: continue
|
| 141 |
+
|
| 142 |
+
p_idx = P2I[clean_char]
|
| 143 |
+
|
| 144 |
+
# 3. Identify Position
|
| 145 |
+
pos_idx = self._determine_position(item)
|
| 146 |
+
|
| 147 |
+
events.append({
|
| 148 |
+
'phoneme_idx': p_idx,
|
| 149 |
+
'error_idx': err_idx,
|
| 150 |
+
'pos_idx': pos_idx,
|
| 151 |
+
'raw': item
|
| 152 |
+
})
|
| 153 |
+
|
| 154 |
+
return events
|
| 155 |
+
|
| 156 |
+
def update_tensor(self, current_tensor_blob, new_results, learning_rate=0.2):
|
| 157 |
+
"""
|
| 158 |
+
Merges new errors into the patient tensor.
|
| 159 |
+
|
| 160 |
+
Args:
|
| 161 |
+
current_tensor_blob: 3D List or None (from Firebase).
|
| 162 |
+
new_results: List of errors (from Test or Practice).
|
| 163 |
+
learning_rate: How much to increase severity per error (0.0 - 1.0).
|
| 164 |
+
|
| 165 |
+
Returns:
|
| 166 |
+
Updated 3D List (JSON serializable).
|
| 167 |
+
"""
|
| 168 |
+
# 1. Load or Initialize
|
| 169 |
+
if current_tensor_blob is None:
|
| 170 |
+
tensor = np.zeros((3, 64, 9), dtype=np.float32)
|
| 171 |
+
else:
|
| 172 |
+
tensor = np.array(current_tensor_blob, dtype=np.float32)
|
| 173 |
+
# Safety check shape
|
| 174 |
+
if tensor.shape != (3, 64, 9):
|
| 175 |
+
tensor = np.zeros((3, 64, 9), dtype=np.float32) # Reset if corrupt
|
| 176 |
+
|
| 177 |
+
# 2. Parse New Errors
|
| 178 |
+
events = self.parse_backend_output(new_results)
|
| 179 |
+
|
| 180 |
+
# 3. Apply Updates
|
| 181 |
+
for ev in events:
|
| 182 |
+
c, h, w = ev['pos_idx'], ev['phoneme_idx'], ev['error_idx']
|
| 183 |
+
|
| 184 |
+
# Logic: Increment severity.
|
| 185 |
+
# If it was 0, it becomes 0.2. If it was 0.8, it becomes 1.0.
|
| 186 |
+
current_val = tensor[c, h, w]
|
| 187 |
+
new_val = min(1.0, current_val + learning_rate)
|
| 188 |
+
tensor[c, h, w] = new_val
|
| 189 |
+
|
| 190 |
+
# 4. Spillover Logic (Optional but smart)
|
| 191 |
+
# If Blocking happened on 'p' (Stop), slightly increase 'b' (Voiced Stop) too?
|
| 192 |
+
# For now, we rely on AMLM to fill these gaps, so we keep Bridge simple.
|
| 193 |
+
|
| 194 |
+
return tensor.tolist()
|