amkyawdev commited on
Commit
a14c2d1
Β·
verified Β·
1 Parent(s): 7dcec51

Add tests

Browse files
Files changed (2) hide show
  1. test_audio_processor.py +69 -0
  2. test_verifier.py +91 -0
test_audio_processor.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Test audio processor module."""
2
+
3
+ import sys
4
+ from pathlib import Path
5
+
6
+ sys.path.insert(0, str(Path(__file__).parent.parent))
7
+
8
+ import numpy as np
9
+ from src.data_processing.audio_processor import AudioProcessor
10
+
11
+
12
+ def test_audio_processor_init():
13
+ """Test AudioProcessor initialization."""
14
+ processor = AudioProcessor(sample_rate=16000)
15
+ assert processor.sample_rate == 16000
16
+ print("βœ“ AudioProcessor init test passed")
17
+
18
+
19
+ def test_normalize_audio():
20
+ """Test audio normalization."""
21
+ processor = AudioProcessor()
22
+
23
+ audio = np.array([0.5, -0.5, 1.0, -1.0])
24
+ normalized = processor.normalize_audio(audio)
25
+
26
+ assert np.abs(normalized).max() <= 1.0
27
+ print("βœ“ Normalize audio test passed")
28
+
29
+
30
+ def test_remove_silence():
31
+ """Test silence removal."""
32
+ processor = AudioProcessor()
33
+
34
+ # Create audio with silence
35
+ audio = np.concatenate([
36
+ np.zeros(1000), # silence
37
+ np.random.randn(5000), # speech
38
+ np.zeros(500), # silence
39
+ ])
40
+
41
+ cleaned = processor.remove_silence(audio, threshold_db=40)
42
+
43
+ assert len(cleaned) < len(audio)
44
+ print("βœ“ Remove silence test passed")
45
+
46
+
47
+ def test_prosody_extraction():
48
+ """Test prosody feature extraction."""
49
+ processor = AudioProcessor()
50
+
51
+ # Generate synthetic audio
52
+ duration = 1.0
53
+ sample_rate = 16000
54
+ t = np.linspace(0, duration, int(sample_rate * duration))
55
+ audio = np.sin(2 * np.pi * 200 * t) * 0.5 # 200Hz tone
56
+
57
+ prosody = processor.extract_prosody_features(audio)
58
+
59
+ assert "mean_pitch" in prosody
60
+ assert "mean_energy" in prosody
61
+ print("βœ“ Prosody extraction test passed")
62
+
63
+
64
+ if __name__ == "__main__":
65
+ test_audio_processor_init()
66
+ test_normalize_audio()
67
+ test_remove_silence()
68
+ test_prosody_extraction()
69
+ print("\nβœ… All audio processor tests passed!")
test_verifier.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Test verification module."""
2
+
3
+ import sys
4
+ from pathlib import Path
5
+
6
+ sys.path.insert(0, str(Path(__file__).parent.parent))
7
+
8
+ from src.annotation.automatic_verifier import (
9
+ AutomaticVerifier,
10
+ TextLengthRule,
11
+ SentimentConsistencyRule,
12
+ )
13
+
14
+
15
+ def test_verifier_init():
16
+ """Test verifier initialization."""
17
+ verifier = AutomaticVerifier()
18
+ assert len(verifier.rules) > 0
19
+ print("βœ“ Verifier init test passed")
20
+
21
+
22
+ def test_text_length_rule():
23
+ """Test text length verification rule."""
24
+ rule = TextLengthRule(min_length=5, max_length=100)
25
+
26
+ # Test too short
27
+ passed, msg = rule.verify({"text": "Hi"})
28
+ assert not passed
29
+
30
+ # Test valid
31
+ passed, msg = rule.verify({"text": "Valid length text"})
32
+ assert passed
33
+
34
+ print("βœ“ Text length rule test passed")
35
+
36
+
37
+ def test_sentiment_consistency():
38
+ """Test sentiment consistency rule."""
39
+ rule = SentimentConsistencyRule()
40
+
41
+ # Positive text with positive label
42
+ passed, msg = rule.verify({
43
+ "text": "ကျေးဇူးပါ",
44
+ "sentiment": "positive",
45
+ })
46
+ assert passed
47
+
48
+ print("βœ“ Sentiment consistency rule test passed")
49
+
50
+
51
+ def test_dataset_verification():
52
+ """Test full dataset verification."""
53
+ verifier = AutomaticVerifier()
54
+
55
+ samples = [
56
+ {"id": "utt_001", "text": "ကျေးဇူးပါ", "sentiment": "positive"},
57
+ {"id": "utt_002", "text": "မကျေနပ်", "sentiment": "negative"},
58
+ ]
59
+
60
+ results = verifier.verify_dataset(samples)
61
+
62
+ assert results["total_samples"] == 2
63
+ assert "statistics" in results
64
+
65
+ print("βœ“ Dataset verification test passed")
66
+
67
+
68
+ def test_sample_filtering():
69
+ """Test sample filtering based on verification."""
70
+ verifier = AutomaticVerifier()
71
+
72
+ samples = [
73
+ {"id": "utt_001", "text": "ကျေးဇူးပါ", "sentiment": "positive"},
74
+ {"id": "utt_002", "text": "", "sentiment": "negative"}, # Invalid
75
+ ]
76
+
77
+ kept, removed = verifier.filter_samples(samples)
78
+
79
+ assert len(kept) == 1
80
+ assert len(removed) == 1
81
+
82
+ print("βœ“ Sample filtering test passed")
83
+
84
+
85
+ if __name__ == "__main__":
86
+ test_verifier_init()
87
+ test_text_length_rule()
88
+ test_sentiment_consistency()
89
+ test_dataset_verification()
90
+ test_sample_filtering()
91
+ print("\nβœ… All verifier tests passed!")