whisper-apr 0.3.1

WASM-first automatic speech recognition engine implementing OpenAI Whisper
Documentation
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Domain vocabulary adapter (WAPR-172)
//!
//! Adapts vocabulary recognition for specific domains like medical, legal, or technical.
//!
//! # Overview
//!
//! The domain adapter provides:
//! 1. Pre-defined domain vocabularies (medical, legal, technical)
//! 2. Custom domain word lists with boosting
//! 3. Dynamic vocabulary expansion during inference
//!
//! # Example
//!
//! ```rust,ignore
//! use whisper_apr::vocabulary::{DomainAdapter, DomainType};
//!
//! // Use pre-defined medical domain
//! let adapter = DomainAdapter::new(DomainType::Medical);
//!
//! // Or create custom domain
//! let mut custom = DomainAdapter::new(DomainType::Custom);
//! custom.add_term_with_tokens("proprietary_term", vec![100, 200], 2.0);
//!
//! // Apply to logits during decoding
//! adapter.apply_bias(&mut logits);
//! ```

use std::collections::HashMap;

#[cfg(test)]
mod tests;

/// Domain type for vocabulary adaptation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DomainType {
    /// General domain (no specialization)
    General,
    /// Medical/healthcare terminology
    Medical,
    /// Legal/law terminology
    Legal,
    /// Technical/engineering terminology
    Technical,
    /// Financial/business terminology
    Financial,
    /// Scientific/academic terminology
    Scientific,
    /// Custom user-defined domain
    Custom,
}

impl DomainType {
    /// Get display name
    #[must_use]
    pub fn name(&self) -> &'static str {
        match self {
            Self::General => "General",
            Self::Medical => "Medical",
            Self::Legal => "Legal",
            Self::Technical => "Technical",
            Self::Financial => "Financial",
            Self::Scientific => "Scientific",
            Self::Custom => "Custom",
        }
    }

    /// Check if domain has pre-defined terms
    #[must_use]
    pub fn has_predefined_terms(&self) -> bool {
        !matches!(self, Self::General | Self::Custom)
    }
}

/// Configuration for domain adaptation
#[derive(Debug, Clone)]
pub struct DomainConfig {
    /// Base boost for domain terms
    pub base_boost: f32,
    /// Boost multiplier for high-priority terms
    pub priority_multiplier: f32,
    /// Maximum boost value
    pub max_boost: f32,
    /// Whether to suppress out-of-domain terms
    pub suppress_out_of_domain: bool,
    /// Suppression factor (negative bias)
    pub suppression_factor: f32,
}

impl DomainConfig {
    /// Create default configuration
    #[must_use]
    pub fn new() -> Self {
        Self {
            base_boost: 1.0,
            priority_multiplier: 1.5,
            max_boost: 5.0,
            suppress_out_of_domain: false,
            suppression_factor: 0.5,
        }
    }

    /// Set base boost
    #[must_use]
    pub fn with_base_boost(mut self, boost: f32) -> Self {
        self.base_boost = boost;
        self
    }

    /// Set priority multiplier
    #[must_use]
    pub fn with_priority_multiplier(mut self, multiplier: f32) -> Self {
        self.priority_multiplier = multiplier;
        self
    }

    /// Set max boost
    #[must_use]
    pub fn with_max_boost(mut self, max: f32) -> Self {
        self.max_boost = max;
        self
    }

    /// Enable out-of-domain suppression
    #[must_use]
    pub fn with_suppression(mut self, factor: f32) -> Self {
        self.suppress_out_of_domain = true;
        self.suppression_factor = factor;
        self
    }
}

impl Default for DomainConfig {
    fn default() -> Self {
        Self::new()
    }
}

/// A domain term with its token representation and boost
#[derive(Debug, Clone)]
pub struct DomainTerm {
    /// Original text
    pub text: String,
    /// Token sequence
    pub tokens: Vec<u32>,
    /// Boost value
    pub boost: f32,
    /// Whether this is a high-priority term
    pub is_priority: bool,
    /// Category within the domain
    pub category: Option<String>,
}

impl DomainTerm {
    /// Create a new domain term
    #[must_use]
    pub fn new(text: String, tokens: Vec<u32>, boost: f32) -> Self {
        Self {
            text,
            tokens,
            boost,
            is_priority: false,
            category: None,
        }
    }

    /// Mark as priority term
    #[must_use]
    pub fn with_priority(mut self) -> Self {
        self.is_priority = true;
        self
    }

    /// Set category
    #[must_use]
    pub fn with_category(mut self, category: &str) -> Self {
        self.category = Some(category.to_string());
        self
    }

    /// Get first token (for quick lookup)
    #[must_use]
    pub fn first_token(&self) -> Option<u32> {
        self.tokens.first().copied()
    }
}

/// Domain vocabulary adapter
#[derive(Debug, Clone)]
pub struct DomainAdapter {
    /// Domain type
    domain_type: DomainType,
    /// Configuration
    config: DomainConfig,
    /// Domain terms indexed by first token
    pub(crate) terms: Vec<DomainTerm>,
    /// Token to term indices map
    first_token_map: HashMap<u32, Vec<usize>>,
    /// All tokens that belong to domain terms (for suppression)
    domain_tokens: HashMap<u32, f32>,
}

impl DomainAdapter {
    /// Create a new domain adapter
    #[must_use]
    pub fn new(domain_type: DomainType) -> Self {
        let mut adapter = Self {
            domain_type,
            config: DomainConfig::default(),
            terms: Vec::new(),
            first_token_map: HashMap::new(),
            domain_tokens: HashMap::new(),
        };

        // Load pre-defined terms if available
        if domain_type.has_predefined_terms() {
            adapter.load_predefined_terms();
        }

        adapter
    }

    /// Create with custom config
    #[must_use]
    pub fn with_config(domain_type: DomainType, config: DomainConfig) -> Self {
        let mut adapter = Self {
            domain_type,
            config,
            terms: Vec::new(),
            first_token_map: HashMap::new(),
            domain_tokens: HashMap::new(),
        };

        if domain_type.has_predefined_terms() {
            adapter.load_predefined_terms();
        }

        adapter
    }

    /// Get domain type
    #[must_use]
    pub fn domain_type(&self) -> DomainType {
        self.domain_type
    }

    /// Get configuration
    #[must_use]
    pub fn config(&self) -> &DomainConfig {
        &self.config
    }

    /// Get number of terms
    #[must_use]
    pub fn len(&self) -> usize {
        self.terms.len()
    }

    /// Check if empty
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.terms.is_empty()
    }

    /// Get all terms
    #[must_use]
    pub fn terms(&self) -> &[DomainTerm] {
        &self.terms
    }

    /// Add a term with tokens
    pub fn add_term_with_tokens(&mut self, text: &str, tokens: Vec<u32>, boost: f32) {
        if tokens.is_empty() {
            return;
        }

        let clamped_boost = boost.clamp(-self.config.max_boost, self.config.max_boost);
        let first_token = tokens[0];
        let term_idx = self.terms.len();

        // Add all tokens to domain token set
        for &token in &tokens {
            self.domain_tokens
                .entry(token)
                .and_modify(|b| *b = b.max(clamped_boost))
                .or_insert(clamped_boost);
        }

        self.terms
            .push(DomainTerm::new(text.to_string(), tokens, clamped_boost));

        self.first_token_map
            .entry(first_token)
            .or_default()
            .push(term_idx);
    }

    /// Add a term with default boost
    pub fn add_term_with_tokens_default(&mut self, text: &str, tokens: Vec<u32>) {
        self.add_term_with_tokens(text, tokens, self.config.base_boost);
    }

    /// Add a priority term
    pub fn add_priority_term(&mut self, text: &str, tokens: Vec<u32>) {
        let boost = self.config.base_boost * self.config.priority_multiplier;
        if !tokens.is_empty() {
            let term_idx = self.terms.len();
            let first_token = tokens[0];

            for &token in &tokens {
                self.domain_tokens
                    .entry(token)
                    .and_modify(|b| *b = b.max(boost))
                    .or_insert(boost);
            }

            let mut term = DomainTerm::new(text.to_string(), tokens, boost);
            term.is_priority = true;
            self.terms.push(term);

            self.first_token_map
                .entry(first_token)
                .or_default()
                .push(term_idx);
        }
    }

    /// Apply bias to logits
    ///
    /// Boosts domain terms and optionally suppresses out-of-domain tokens.
    pub fn apply_bias(&self, logits: &mut [f32]) {
        if self.is_empty() {
            return;
        }

        // Apply boost to domain tokens
        for (&token, &boost) in &self.domain_tokens {
            if (token as usize) < logits.len() {
                logits[token as usize] += boost;
            }
        }
    }

    /// Check if a token is in the domain vocabulary
    #[must_use]
    pub fn is_domain_token(&self, token: u32) -> bool {
        self.domain_tokens.contains_key(&token)
    }

    /// Get boost for a specific token
    #[must_use]
    pub fn get_token_boost(&self, token: u32) -> Option<f32> {
        self.domain_tokens.get(&token).copied()
    }

    /// Clear all terms
    pub fn clear(&mut self) {
        self.terms.clear();
        self.first_token_map.clear();
        self.domain_tokens.clear();
    }

    /// Load pre-defined terms for the domain
    fn load_predefined_terms(&mut self) {
        // Note: In production, these would be loaded from a vocabulary file
        // Here we provide placeholder structure for the API
        match self.domain_type {
            DomainType::Medical => self.load_medical_terms(),
            DomainType::Legal => self.load_legal_terms(),
            DomainType::Technical => self.load_technical_terms(),
            DomainType::Financial => self.load_financial_terms(),
            DomainType::Scientific => self.load_scientific_terms(),
            DomainType::General | DomainType::Custom => {}
        }
    }

    #[allow(clippy::needless_pass_by_ref_mut)]
    fn load_medical_terms(&mut self) {
        let _ = self; // Placeholder: In production, will load from vocabulary file
    }

    #[allow(clippy::needless_pass_by_ref_mut)]
    fn load_legal_terms(&mut self) {
        let _ = self; // Placeholder
    }

    #[allow(clippy::needless_pass_by_ref_mut)]
    fn load_technical_terms(&mut self) {
        let _ = self; // Placeholder
    }

    #[allow(clippy::needless_pass_by_ref_mut)]
    fn load_financial_terms(&mut self) {
        let _ = self; // Placeholder
    }

    #[allow(clippy::needless_pass_by_ref_mut)]
    fn load_scientific_terms(&mut self) {
        let _ = self; // Placeholder
    }

    /// Get terms by category
    #[must_use]
    pub fn terms_by_category(&self, category: &str) -> Vec<&DomainTerm> {
        self.terms
            .iter()
            .filter(|t| t.category.as_deref() == Some(category))
            .collect()
    }

    /// Get all categories
    #[must_use]
    pub fn categories(&self) -> Vec<String> {
        let mut categories: Vec<_> = self
            .terms
            .iter()
            .filter_map(|t| t.category.clone())
            .collect();
        categories.sort();
        categories.dedup();
        categories
    }

    /// Get priority terms only
    #[must_use]
    pub fn priority_terms(&self) -> Vec<&DomainTerm> {
        self.terms.iter().filter(|t| t.is_priority).collect()
    }
}

/// Medical domain adapter factory
impl DomainAdapter {
    /// Create medical domain adapter
    #[must_use]
    pub fn medical() -> Self {
        Self::new(DomainType::Medical)
    }

    /// Create legal domain adapter
    #[must_use]
    pub fn legal() -> Self {
        Self::new(DomainType::Legal)
    }

    /// Create technical domain adapter
    #[must_use]
    pub fn technical() -> Self {
        Self::new(DomainType::Technical)
    }

    /// Create financial domain adapter
    #[must_use]
    pub fn financial() -> Self {
        Self::new(DomainType::Financial)
    }

    /// Create scientific domain adapter
    #[must_use]
    pub fn scientific() -> Self {
        Self::new(DomainType::Scientific)
    }

    /// Create custom domain adapter
    #[must_use]
    pub fn custom() -> Self {
        Self::new(DomainType::Custom)
    }
}