oxirs-embed 0.2.4

Knowledge graph embeddings with TransE, ComplEx, and custom models
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
//! Text preprocessing for transformer models

use super::types::{DomainPreprocessingRules, TransformerConfig, TransformerType};
use anyhow::Result;
use regex::Regex;
use std::collections::HashMap;

/// Text preprocessor for transformer models
#[derive(Debug, Clone)]
pub struct TransformerPreprocessor {
    config: TransformerConfig,
    domain_rules: Option<DomainPreprocessingRules>,
}

impl TransformerPreprocessor {
    pub fn new(config: TransformerConfig) -> Self {
        let domain_rules = match config.transformer_type {
            TransformerType::SciBERT => Some(DomainPreprocessingRules::scientific()),
            TransformerType::BioBERT => Some(DomainPreprocessingRules::biomedical()),
            TransformerType::LegalBERT => Some(DomainPreprocessingRules::legal()),
            TransformerType::NewsBERT => Some(DomainPreprocessingRules::news()),
            TransformerType::SocialMediaBERT => Some(DomainPreprocessingRules::social_media()),
            _ => None,
        };

        Self {
            config,
            domain_rules,
        }
    }

    /// Main preprocessing function that routes to appropriate domain-specific methods
    pub fn preprocess_text(&self, text: &str) -> String {
        let mut processed = text.to_string();

        // Common preprocessing steps
        processed = self.clean_uri(&processed);
        processed = self.normalize_whitespace(&processed);

        // Apply domain-specific preprocessing
        processed = match self.config.transformer_type {
            TransformerType::SciBERT => self.preprocess_scientific_text(&processed),
            TransformerType::BioBERT => self.preprocess_biomedical_text(&processed),
            TransformerType::CodeBERT => self.preprocess_code_text(&processed),
            TransformerType::LegalBERT => self.preprocess_legal_text(&processed),
            TransformerType::NewsBERT => self.preprocess_news_text(&processed),
            TransformerType::SocialMediaBERT => self.preprocess_social_media_text(&processed),
            _ => processed,
        };

        // Apply general domain rules if available
        if let Some(ref rules) = self.domain_rules {
            processed = self.apply_domain_rules(&processed, rules);
        }

        processed
    }

    /// Clean URI components for better semantic representation
    fn clean_uri(&self, text: &str) -> String {
        let mut result = text.to_string();

        // Remove protocol prefixes
        result = result.replace("http://", "");
        result = result.replace("https://", "");
        result = result.replace("ftp://", "");

        // Replace common URI separators with spaces
        result = result.replace('/', " ");
        result = result.replace('#', " ");
        result = result.replace('?', " ");
        result = result.replace('&', " and ");
        result = result.replace('=', " equals ");

        // Handle underscores in URIs (common in ontologies)
        result = result.replace('_', " ");

        result
    }

    /// Normalize whitespace
    fn normalize_whitespace(&self, text: &str) -> String {
        // Replace multiple whitespace with single space
        let re = Regex::new(r"\s+").expect("regex pattern should be valid");
        re.replace_all(text, " ").trim().to_string()
    }

    /// Apply domain-specific preprocessing rules
    fn apply_domain_rules(&self, text: &str, rules: &DomainPreprocessingRules) -> String {
        let mut result = text.to_string();

        // Apply abbreviation expansions
        for (abbrev, expansion) in &rules.abbreviation_expansions {
            result = result.replace(abbrev, expansion);
        }

        // Apply pattern replacements
        for (pattern, replacement) in &rules.domain_specific_patterns {
            if let Ok(re) = Regex::new(pattern) {
                result = re.replace_all(&result, replacement).to_string();
            }
        }

        result
    }

    /// Preprocessing for scientific text (SciBERT)
    pub fn preprocess_scientific_text(&self, text: &str) -> String {
        let mut result = text.to_string();

        // Scientific abbreviations
        let scientific_abbrevs = HashMap::from([
            ("DNA", "deoxyribonucleic acid"),
            ("RNA", "ribonucleic acid"),
            ("ATP", "adenosine triphosphate"),
            ("GDP", "guanosine diphosphate"),
            ("GTP", "guanosine triphosphate"),
            ("Co2", "carbon dioxide"),
            ("H2O", "water"),
            ("NaCl", "sodium chloride"),
        ]);

        for (abbrev, expansion) in scientific_abbrevs {
            result = result.replace(abbrev, expansion);
        }

        // Handle scientific notation and units
        result = result.replace("°C", " degrees celsius");
        result = result.replace("mg/ml", " milligrams per milliliter");
        result = result.replace("μg/ml", " micrograms per milliliter");
        result = result.replace("mM", " millimolar");
        result = result.replace("μM", " micromolar");

        // Handle chemical formulas and reactions
        result = result.replace("->", " produces ");
        result = result.replace("<->", " is in equilibrium with ");

        result
    }

    /// Preprocessing for biomedical text (BioBERT)
    pub fn preprocess_biomedical_text(&self, text: &str) -> String {
        let mut result = text.to_string();

        // Biomedical gene and protein abbreviations
        let biomedical_abbrevs = HashMap::from([
            ("p53", "tumor protein p53"),
            ("BRCA1", "breast cancer gene 1"),
            ("BRCA2", "breast cancer gene 2"),
            ("TNF-α", "tumor necrosis factor alpha"),
            ("IL-1", "interleukin 1"),
            ("IL-6", "interleukin 6"),
            ("mRNA", "messenger ribonucleic acid"),
            ("tRNA", "transfer ribonucleic acid"),
            ("rRNA", "ribosomal ribonucleic acid"),
            ("CNS", "central nervous system"),
            ("PNS", "peripheral nervous system"),
        ]);

        for (abbrev, expansion) in biomedical_abbrevs {
            result = result.replace(abbrev, expansion);
        }

        // Handle medical terminology
        result = result.replace("bp", " base pairs");
        result = result.replace("kDa", " kilodaltons");
        result = result.replace("mg/kg", " milligrams per kilogram");

        result
    }

    /// Preprocessing for code text (CodeBERT)
    pub fn preprocess_code_text(&self, text: &str) -> String {
        let mut result = text.to_string();

        // Programming language keywords and common terms
        result = result.replace("impl", "implementation");
        result = result.replace("func", "function");
        result = result.replace("var", "variable");
        result = result.replace("const", "constant");
        result = result.replace("struct", "structure");
        result = result.replace("enum", "enumeration");

        // Common type names
        result = result.replace("Vec<i32>", "vector of integers");
        result = result.replace("HashMap", "hash map");
        result = result.replace("String", "string");
        result = result.replace("bool", "boolean");

        // Expand camelCase and PascalCase
        result = self.expand_camel_case(&result);

        result
    }

    /// Preprocessing for legal text (LegalBERT)
    pub fn preprocess_legal_text(&self, text: &str) -> String {
        let mut result = text.to_string();

        // Legal abbreviations
        let legal_abbrevs = HashMap::from([
            ("USC", "United States Code"),
            ("CFR", "Code of Federal Regulations"),
            ("plaintiff", "party bringing lawsuit"),
            ("defendant", "party being sued"),
            ("tort", "civil wrong"),
            ("v.", "versus"),
            ("vs.", "versus"),
            ("et al.", "and others"),
            ("cf.", "compare"),
            ("ibid.", "in the same place"),
            ("supra", "above mentioned"),
        ]);

        for (abbrev, expansion) in legal_abbrevs {
            result = result.replace(abbrev, expansion);
        }

        // Handle legal citations
        let section_re = Regex::new(r"§(\d+)").expect("regex pattern should be valid");
        result = section_re.replace_all(&result, "section $1").to_string();

        result
    }

    /// Preprocessing for news text (NewsBERT)
    pub fn preprocess_news_text(&self, text: &str) -> String {
        let mut result = text.to_string();

        // Business and economics abbreviations
        let news_abbrevs = HashMap::from([
            ("CEO", "chief executive officer"),
            ("CFO", "chief financial officer"),
            ("CTO", "chief technology officer"),
            ("IPO", "initial public offering"),
            ("SEC", "Securities and Exchange Commission"),
            ("GDP", "gross domestic product"),
            ("CPI", "consumer price index"),
            ("NYSE", "New York Stock Exchange"),
            (
                "NASDAQ",
                "National Association of Securities Dealers Automated Quotations",
            ),
        ]);

        for (abbrev, expansion) in news_abbrevs {
            result = result.replace(abbrev, expansion);
        }

        // Handle financial terms
        result = result.replace("Q1", "first quarter");
        result = result.replace("Q2", "second quarter");
        result = result.replace("Q3", "third quarter");
        result = result.replace("Q4", "fourth quarter");

        // Handle percentages
        let percent_re = Regex::new(r"(\d+\.?\d*)%").expect("regex pattern should be valid");
        result = percent_re.replace_all(&result, "$1 percent").to_string();

        result
    }

    /// Preprocessing for social media text (SocialMediaBERT)
    pub fn preprocess_social_media_text(&self, text: &str) -> String {
        let mut result = text.to_string();

        // Handle social media abbreviations
        result = result.replace("lol", "laugh out loud");
        result = result.replace("omg", "oh my god");
        result = result.replace("btw", "by the way");
        result = result.replace("fyi", "for your information");
        result = result.replace("imo", "in my opinion");
        result = result.replace("tbh", "to be honest");
        result = result.replace("smh", "shaking my head");
        result = result.replace("rn", "right now");
        result = result.replace("irl", "in real life");

        // Handle hashtags and mentions
        result = result.replace('#', "hashtag ");
        result = result.replace('@', "mention ");

        // Handle emoticons (basic)
        result = result.replace(":)", "happy");
        result = result.replace(":(", "sad");
        result = result.replace(":D", "very happy");
        result = result.replace(";)", "winking");
        result = result.replace(":P", "playful");
        result = result.replace(":/", "confused");

        // Handle emphasis
        result = result.replace("!!", "exclamation");
        result = result.replace("???", "question");

        result
    }

    /// Expand camelCase to separate words
    pub fn expand_camel_case(&self, text: &str) -> String {
        if text.is_empty() {
            return String::new();
        }

        let mut result = String::new();
        let chars: Vec<char> = text.chars().collect();

        for (i, &ch) in chars.iter().enumerate() {
            // Add space before every uppercase letter (except the first character)
            if i > 0 && ch.is_uppercase() {
                result.push(' ');
            }

            result.push(ch.to_lowercase().next().unwrap_or(ch));
        }

        result
    }

    /// Simple tokenization for demonstration
    pub fn tokenize(&self, text: &str) -> Result<Vec<u32>> {
        // In real implementation, this would use a proper tokenizer
        // For now, convert each character to a token
        let tokens: Vec<u32> = text
            .chars()
            .take(self.config.max_sequence_length)
            .map(|c| c as u32 % 30522) // Map to vocab range
            .collect();

        Ok(tokens)
    }

    /// Get maximum sequence length
    pub fn max_sequence_length(&self) -> usize {
        self.config.max_sequence_length
    }

    /// Check if text should be truncated
    pub fn needs_truncation(&self, text: &str) -> bool {
        text.len() > self.config.max_sequence_length
    }

    /// Truncate text to maximum sequence length
    pub fn truncate_text(&self, text: &str) -> String {
        if text.len() <= self.config.max_sequence_length {
            text.to_string()
        } else {
            text.chars().take(self.config.max_sequence_length).collect()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::transformer::types::TransformerType;

    #[test]
    fn test_scientific_preprocessing() {
        let config = TransformerConfig {
            transformer_type: TransformerType::SciBERT,
            ..Default::default()
        };
        let preprocessor = TransformerPreprocessor::new(config);

        let text = "DNA synthesis with ATP and Co2 at 25°C using 5mg/ml concentration";
        let processed = preprocessor.preprocess_scientific_text(text);
        assert!(processed.contains("deoxyribonucleic acid"));
        assert!(processed.contains("adenosine triphosphate"));
        assert!(processed.contains("carbon dioxide"));
        assert!(processed.contains("degrees celsius"));
        assert!(processed.contains("milligrams per milliliter"));
    }

    #[test]
    fn test_biomedical_preprocessing() {
        let config = TransformerConfig {
            transformer_type: TransformerType::BioBERT,
            ..Default::default()
        };
        let preprocessor = TransformerPreprocessor::new(config);

        let text = "p53 and BRCA1 mutations affect TNF-α via mRNA expression in CNS";
        let processed = preprocessor.preprocess_biomedical_text(text);
        assert!(processed.contains("tumor protein p53"));
        assert!(processed.contains("breast cancer gene 1"));
        assert!(processed.contains("tumor necrosis factor"));
        assert!(processed.contains("messenger ribonucleic acid"));
        assert!(processed.contains("central nervous system"));
    }

    #[test]
    fn test_code_preprocessing() {
        let config = TransformerConfig {
            transformer_type: TransformerType::CodeBERT,
            ..Default::default()
        };
        let preprocessor = TransformerPreprocessor::new(config);

        let text = "MyClass impl func calculateValue() returns Vec<i32>";
        let processed = preprocessor.preprocess_code_text(text);
        assert!(processed.contains("my class"));
        assert!(processed.contains("implementation"));
        assert!(processed.contains("function"));
        assert!(processed.contains("calculate value"));
    }

    #[test]
    fn test_camel_case_expansion() {
        let config = TransformerConfig::default();
        let preprocessor = TransformerPreprocessor::new(config);

        assert_eq!(preprocessor.expand_camel_case("MyClass"), "my class");
        assert_eq!(
            preprocessor.expand_camel_case("calculateValue"),
            "calculate value"
        );
        assert_eq!(
            preprocessor.expand_camel_case("getUserNameFromAPI"),
            "get user name from a p i"
        );
        assert_eq!(preprocessor.expand_camel_case(""), "");
    }

    #[test]
    fn test_uri_cleaning() {
        let config = TransformerConfig::default();
        let preprocessor = TransformerPreprocessor::new(config);

        let uri = "http://example.org/DNA_molecule#structure";
        let cleaned = preprocessor.clean_uri(uri);
        assert!(cleaned.contains("example"));
        assert!(cleaned.contains("DNA"));
        assert!(cleaned.contains("molecule"));
        assert!(cleaned.contains("structure"));
        assert!(!cleaned.contains("http://"));
        assert!(!cleaned.contains("#"));
    }
}