File size: 11,725 Bytes
cfb5e7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""Multi-modal data fusion for Myanmar Ghost project.

Fuses audio (prosody) and text to understand sentiment/intensity
in expressions like "ကျေးဇူးပါ" (thank you) which can mean:
- Genuine gratitude (low pitch, slow)
- Sarcasm (high pitch, fast)
- Complaint (negative prosody)
"""

from dataclasses import dataclass
from enum import Enum
from typing import Any, Dict, List, Optional, Tuple

import numpy as np
import torch
import torch.nn as nn
from torch import Tensor


class SentimentClass(str, Enum):
    """Sentiment classes for thanking expressions."""
    GENUINE = "genuine"          # α€›α€­α€―α€Έα€žα€¬α€Έα€α€Όα€„α€Ία€Έ
    SARCASTIC = "sarcastic"      # α€žα€›α€±α€¬α€Ία€α€Όα€„α€Ία€Έ
    COMPLAINING = "complaining"  # မကျေနပ်ခြင်း
    NEUTRAL = "neutral"


@dataclass
class ProsodyFeatures:
    """Prosodic features extracted from audio."""
    mean_pitch: float
    pitch_std: float
    pitch_range: Tuple[float, float]
    mean_energy: float
    energy_std: float
    speaking_rate: float  # syllables per second
    pause_duration: float  # total pause time in seconds
    
    def to_tensor(self) -> Tensor:
        """Convert to PyTorch tensor."""
        return torch.tensor([
            self.mean_pitch,
            self.pitch_std,
            self.pitch_range[0],
            self.pitch_range[1],
            self.mean_energy,
            self.energy_std,
            self.speaking_rate,
            self.pause_duration,
        ], dtype=torch.float32)
    
    def to_dict(self) -> Dict[str, float]:
        """Convert to dictionary."""
        return {
            "mean_pitch": self.mean_pitch,
            "pitch_std": self.pitch_std,
            "pitch_min": self.pitch_range[0],
            "pitch_max": self.pitch_range[1],
            "mean_energy": self.mean_energy,
            "energy_std": self.energy_std,
            "speaking_rate": self.speaking_rate,
            "pause_duration": self.pause_duration,
        }


@dataclass
class TextFeatures:
    """Text-based features for sentiment analysis."""
    text_length: int
    word_count: int
    contains_intensifier: bool  # e.g., "ထရမ်း", "များစွာ"
    politeness_level: int  # 1-5 scale
    formality: float  # 0-1 scale
    
    def to_tensor(self) -> Tensor:
        """Convert to PyTorch tensor."""
        return torch.tensor([
            float(self.text_length),
            float(self.word_count),
            float(self.contains_intensifier),
            float(self.politeness_level),
            self.formality,
        ], dtype=torch.float32)


@dataclass
class FusedFeatures:
    """Combined multi-modal features."""
    prosody: ProsodyFeatures
    text: TextFeatures
    sentiment_hint: Optional[SentimentClass] = None
    
    def concat_tensors(self) -> Tensor:
        """Concatenate all features into single tensor."""
        return torch.cat([
            self.prosody.to_tensor(),
            self.text.to_tensor(),
        ])


class ProsodyExtractor:
    """Extract prosodic features from audio."""
    
    # Prosody patterns for different sentiments
    GENUINE_PATTERN = {
        "pitch_range": (50, 200),  # Hz
        "speaking_rate": (2, 4),   # syllables/sec
        "energy_std": (0.1, 0.3),
    }
    
    SARCASTIC_PATTERN = {
        "pitch_range": (200, 400),
        "speaking_rate": (4, 8),
        "energy_std": (0.3, 0.6),
    }
    
    COMPLAINING_PATTERN = {
        "pitch_range": (100, 250),
        "speaking_rate": (3, 6),
        "energy_std": (0.2, 0.5),
    }
    
    def extract_from_audio(
        self,
        audio: np.ndarray,
        sample_rate: int = 16000,
    ) -> ProsodyFeatures:
        """Extract prosodic features from audio signal."""
        import librosa
        
        # Pitch tracking
        pitches, magnitudes = librosa.piptrack(
            y=audio,
            sr=sample_rate,
            n_fft=512,
            hop_length=160,
        )
        
        pitch_values = []
        for i in range(pitches.shape[1]):
            index = magnitudes[:, i].argmax()
            pitch = pitches[index, i]
            if pitch > 0:
                pitch_values.append(pitch)
        
        # Energy
        rms = librosa.feature.rms(y=audio, hop_length=160)[0]
        
        # Speaking rate (syllable detection)
        onsets = librosa.onset.onset_detect(
            y=audio,
            sr=sample_rate,
            hop_length=160,
        )
        
        duration = len(audio) / sample_rate
        speaking_rate = len(onsets) / duration if duration > 0 else 0
        
        # Pause detection
        energy_threshold = np.percentile(rms, 25)
        pauses = rms < energy_threshold
        pause_duration = np.sum(pauses) * 160 / sample_rate
        
        return ProsodyFeatures(
            mean_pitch=np.mean(pitch_values) if pitch_values else 0,
            pitch_std=np.std(pitch_values) if pitch_values else 0,
            pitch_range=(
                np.min(pitch_values) if pitch_values else 0,
                np.max(pitch_values) if pitch_values else 0,
            ),
            mean_energy=np.mean(rms),
            energy_std=np.std(rms),
            speaking_rate=speaking_rate,
            pause_duration=pause_duration,
        )
    
    def infer_sentiment(self, prosody: ProsodyFeatures) -> SentimentClass:
        """Infer sentiment from prosodic features."""
        patterns = [
            (SentimentClass.GENUINE, self.GENUINE_PATTERN),
            (SentimentClass.SARCASTIC, self.SARCASTIC_PATTERN),
            (SentimentClass.COMPLAINING, self.COMPLAINING_PATTERN),
        ]
        
        scores = {}
        for sentiment, pattern in patterns:
            score = 0
            features = prosody.to_dict()
            
            for key, (low, high) in pattern.items():
                if key in features:
                    value = features[key]
                    if low <= value <= high:
                        score += 1
            
            scores[sentiment] = score
        
        return max(scores, key=scores.get)


class TextFeatureExtractor:
    """Extract text-based features."""
    
    INTENSIFIERS = {"ထရမ်း", "များစွာ", "ပါး", "α€žα€­α€•α€Ί", "α€‘α€œα€½α€”α€Ί"}
    POLITE_WORDS = {"ကျေးဇူး", "οΏ½εΏƒη—…", "ဂုဏ်", "ထား", "ကြိုးစား", "ပင်ပန်း"}
    
    def extract_from_text(self, text: str) -> TextFeatures:
        """Extract features from text."""
        words = text.split()
        
        has_intensifier = any(
            word in self.INTENSIFIERS for word in words
        )
        
        politeness = self._estimate_politeness(text)
        formality = self._estimate_formality(text)
        
        return TextFeatures(
            text_length=len(text),
            word_count=len(words),
            contains_intensifier=has_intensifier,
            politeness_level=politeness,
            formality=formality,
        )
    
    def _estimate_politeness(self, text: str) -> int:
        """Estimate politeness level (1-5)."""
        score = 3  # default neutral
        polite_count = sum(1 for w in self.POLITE_WORDS if w in text)
        if "ပါ" in text or "ပါး" in text:
            score += 1
        if "ကျေးဇူး" in text:
            score += 1
        if polite_count > 2:
            score += 1
        return min(5, max(1, score))
    
    def _estimate_formality(self, text: str) -> float:
        """Estimate formality (0-1)."""
        formal_markers = {"α€™α€Ύ", "α€žα€Šα€Ί", "α€€α€­α€―", "ဖြင့်", "ထား"}
        informal_markers = {"နော်", "α€Ÿα€―α€α€Ί", "α€™α€Ÿα€―α€α€Ί", "α€œα€¬α€Έ"}
        
        formal_count = sum(1 for m in formal_markers if m in text)
        informal_count = sum(1 for m in informal_markers if m in text)
        
        if formal_count + informal_count == 0:
            return 0.5
        
        return formal_count / (formal_count + informal_count + 1)


class MultiModalFusion(nn.Module):
    """Fuse audio and text modalities."""
    
    def __init__(
        self,
        prosody_dim: int = 8,
        text_dim: int = 5,
        hidden_dim: int = 64,
        num_classes: int = 4,
    ):
        super().__init__()
        
        self.prosody_encoder = nn.Sequential(
            nn.Linear(prosody_dim, hidden_dim),
            nn.ReLU(),
            nn.Dropout(0.2),
        )
        
        self.text_encoder = nn.Sequential(
            nn.Linear(text_dim, hidden_dim),
            nn.ReLU(),
            nn.Dropout(0.2),
        )
        
        self.fusion = nn.Sequential(
            nn.Linear(hidden_dim * 2, hidden_dim),
            nn.ReLU(),
            nn.Dropout(0.3),
            nn.Linear(hidden_dim, num_classes),
        )
    
    def forward(self, prosody: Tensor, text: Tensor) -> Tensor:
        """Forward pass."""
        p_encoded = self.prosody_encoder(prosody)
        t_encoded = self.text_encoder(text)
        
        fused = torch.cat([p_encoded, t_encoded], dim=-1)
        logits = self.fusion(fused)
        
        return logits
    
    def predict(self, prosody: Tensor, text: Tensor) -> Tuple[Tensor, Tensor]:
        """Predict sentiment with probabilities."""
        logits = self.forward(prosody, text)
        probs = torch.softmax(logits, dim=-1)
        return logits, probs


class SentimentClassifier:
    """High-level classifier for multi-modal sentiment."""
    
    def __init__(self, model: MultiModalFusion):
        self.model = model
        self.prosody_extractor = ProsodyExtractor()
        self.text_extractor = TextFeatureExtractor()
    
    def classify(
        self,
        audio: np.ndarray,
        text: str,
        return_probs: bool = True,
    ) -> Dict[str, Any]:
        """Classify sentiment from audio and text."""
        prosody_features = self.prosody_extractor.extract_from_audio(audio)
        prosody_hint = self.prosody_extractor.infer_sentiment(prosody_features)
        
        text_features = self.text_extractor.extract_from_text(text)
        
        fused = FusedFeatures(
            prosody=prosody_features,
            text=text_features,
            sentiment_hint=prosody_hint,
        )
        
        prosody_tensor = fused.prosody.to_tensor().unsqueeze(0)
        text_tensor = fused.text.to_tensor().unsqueeze(0)
        
        with torch.no_grad():
            logits, probs = self.model.predict(prosody_tensor, text_tensor)
        
        result = {
            "predicted_class": SentimentClass(probs.argmax().item()).value,
            "prosody_hint": prosody_hint.value,
            "text_features": text_features.to_dict(),
            "prosody_features": prosody_features.to_dict(),
        }
        
        if return_probs:
            result["probabilities"] = {
                c.value: probs[0, i].item()
                for i, c in enumerate(SentimentClass)
            }
        
        return result


def create_fusion_model(
    prosody_dim: int = 8,
    text_dim: int = 5,
    hidden_dim: int = 64,
    num_classes: int = 4,
) -> MultiModalFusion:
    """Factory function to create fusion model."""
    return MultiModalFusion(
        prosody_dim=prosody_dim,
        text_dim=text_dim,
        hidden_dim=hidden_dim,
        num_classes=num_classes,
    )


if __name__ == "__main__":
    # Example usage
    model = create_fusion_model()
    prosody = torch.randn(1, 8)
    text = torch.randn(1, 5)
    
    logits, probs = model.predict(prosody, text)
    print(f"Predicted class: {SentimentClass(probs.argmax().item()).value}")
    print(f"Probabilities: {probs}")