memory-indexer 0.1.0

An in-memory full-text fuzzy search indexer.
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
use std::{
    collections::{HashMap, HashSet},
    time::{SystemTime, UNIX_EPOCH},
};

use super::{SegmentScript, TextNormalizer, TokenWithScript, script_runs, tokenize_char_ngrams};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DictionaryLanguage {
    Japanese,
    Hangul,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScriptDictionary {
    pub version: Option<String>,
    pub entries: HashSet<String>,
}

impl ScriptDictionary {
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DictionaryConfig {
    pub japanese: Option<ScriptDictionary>,
    pub hangul: Option<ScriptDictionary>,
}

#[derive(Debug, Clone, Default)]
pub struct DictionarySegmenter {
    pub config: DictionaryConfig,
}

impl DictionarySegmenter {
    pub fn new(config: DictionaryConfig) -> Self {
        Self { config }
    }

    pub fn export(&self) -> Vec<DictionaryExport> {
        let mut exports = Vec::new();
        if let Some(dict) = &self.config.japanese {
            if let Some(export) = export_dictionary(DictionaryLanguage::Japanese, dict) {
                exports.push(export);
            }
        }
        if let Some(dict) = &self.config.hangul {
            if let Some(export) = export_dictionary(DictionaryLanguage::Hangul, dict) {
                exports.push(export);
            }
        }
        exports
    }

    pub fn segment(
        &self,
        segment: &str,
        base_start: usize,
        script: SegmentScript,
        normalizer: &dyn TextNormalizer,
        out: &mut Vec<TokenWithScript>,
        seen: &mut HashSet<(String, usize, usize)>,
    ) -> bool {
        let Some(dictionary) = self.dictionary_for_script(script) else {
            return false;
        };
        if dictionary.is_empty() {
            return false;
        }

        let mut char_offsets: Vec<usize> = segment.char_indices().map(|(i, _)| i).collect();
        char_offsets.push(segment.len());
        let char_len = char_offsets.len().saturating_sub(1);
        if char_len == 0 {
            return true;
        }

        let mut covered = vec![false; char_len];
        let mut matched_any = false;
        let mut idx = 0;

        while idx < char_len {
            let mut matched_range: Option<(usize, usize)> = None;
            for end in (idx + 1..=char_len).rev() {
                let start_byte = char_offsets[idx];
                let end_byte = char_offsets[end];
                let candidate = &segment[start_byte..end_byte];
                if dictionary.entries.contains(candidate) {
                    matched_range = Some((idx, end));
                    break;
                }
            }

            if let Some((start_idx, end_idx)) = matched_range {
                matched_any = true;
                let start_byte = char_offsets[start_idx];
                let end_byte = char_offsets[end_idx];
                normalizer.normalize(
                    &segment[start_byte..end_byte],
                    base_start + start_byte,
                    script,
                    out,
                    seen,
                );
                for i in start_idx..end_idx {
                    covered[i] = true;
                }
                idx = end_idx;
            } else {
                idx += 1;
            }
        }

        if !matched_any {
            tokenize_char_ngrams(segment, base_start, script, normalizer, out, seen);
            return true;
        }

        let mut start = 0;
        while start < char_len {
            if covered[start] {
                start += 1;
                continue;
            }
            let mut end = start + 1;
            while end < char_len && !covered[end] {
                end += 1;
            }
            let start_byte = char_offsets[start];
            let end_byte = char_offsets[end];
            tokenize_char_ngrams(
                &segment[start_byte..end_byte],
                base_start + start_byte,
                script,
                normalizer,
                out,
                seen,
            );
            start = end;
        }

        true
    }

    fn dictionary_for_script(&self, script: SegmentScript) -> Option<&ScriptDictionary> {
        match script {
            SegmentScript::Hiragana | SegmentScript::Katakana => self.config.japanese.as_ref(),
            SegmentScript::Hangul => self.config.hangul.as_ref(),
            _ => None,
        }
    }
}

#[derive(Debug, Clone)]
pub struct DictionaryMetadata {
    pub language: DictionaryLanguage,
    pub version: String,
    pub entry_count: usize,
    pub generated_at: SystemTime,
}

#[derive(Debug, Clone)]
pub struct DictionaryExport {
    pub metadata: DictionaryMetadata,
    pub entries: Vec<String>,
}

pub fn export_dictionary(
    language: DictionaryLanguage,
    dictionary: &ScriptDictionary,
) -> Option<DictionaryExport> {
    if dictionary.is_empty() {
        return None;
    }
    let mut entries: Vec<String> = dictionary.entries.iter().cloned().collect();
    entries.sort();

    let metadata = DictionaryMetadata {
        language,
        version: dictionary
            .version
            .clone()
            .unwrap_or_else(|| format!("{}-unversioned", language_prefix(language))),
        entry_count: entries.len(),
        generated_at: SystemTime::now(),
    };

    Some(DictionaryExport { metadata, entries })
}

#[derive(Debug, Clone)]
pub struct DictionaryTrainingConfig {
    pub min_freq: usize,
    pub min_token_len: usize,
    pub max_token_len: usize,
    pub max_entries: usize,
    pub version: Option<String>,
}

impl Default for DictionaryTrainingConfig {
    fn default() -> Self {
        Self {
            min_freq: 2,
            min_token_len: 2,
            max_token_len: 8,
            max_entries: 8_000,
            version: None,
        }
    }
}

pub fn train_dictionary_for_language(
    corpus: &[String],
    language: DictionaryLanguage,
    config: DictionaryTrainingConfig,
) -> ScriptDictionary {
    let min_token_len = config.min_token_len.max(1);
    let max_token_len = config.max_token_len.max(min_token_len);
    let mut counts: HashMap<String, usize> = HashMap::new();

    for text in corpus {
        for (script, start, end) in script_runs(text) {
            if !matches_language(script, language) {
                continue;
            }
            let segment = &text[start..end];
            let mut char_offsets: Vec<usize> = segment.char_indices().map(|(i, _)| i).collect();
            char_offsets.push(segment.len());
            let char_len = char_offsets.len().saturating_sub(1);
            for i in 0..char_len {
                for len in min_token_len..=max_token_len {
                    if i + len > char_len {
                        break;
                    }
                    let start_byte = char_offsets[i];
                    let end_byte = char_offsets[i + len];
                    let candidate = &segment[start_byte..end_byte];
                    if candidate.chars().any(|c| c.is_whitespace()) {
                        continue;
                    }
                    *counts.entry(candidate.to_string()).or_insert(0) += 1;
                }
            }
        }
    }

    let mut entries: Vec<(String, usize)> = counts
        .into_iter()
        .filter(|(_, freq)| *freq >= config.min_freq)
        .collect();
    entries.sort_by(|a, b| {
        b.1.cmp(&a.1)
            .then_with(|| b.0.len().cmp(&a.0.len()))
            .then_with(|| a.0.cmp(&b.0))
    });
    if config.max_entries > 0 && entries.len() > config.max_entries {
        entries.truncate(config.max_entries);
    }

    let entries_set: HashSet<String> = entries.into_iter().map(|(entry, _)| entry).collect();
    ScriptDictionary {
        version: Some(version_or_default(language, &config.version)),
        entries: entries_set,
    }
}

pub fn train_dictionary_config(
    corpus: &[String],
    config: DictionaryTrainingConfig,
) -> DictionaryConfig {
    let japanese =
        train_dictionary_for_language(corpus, DictionaryLanguage::Japanese, config.clone());
    let hangul = train_dictionary_for_language(corpus, DictionaryLanguage::Hangul, config);

    DictionaryConfig {
        japanese: (!japanese.is_empty()).then_some(japanese),
        hangul: (!hangul.is_empty()).then_some(hangul),
    }
}

fn version_or_default(language: DictionaryLanguage, provided: &Option<String>) -> String {
    if let Some(version) = provided {
        return version.clone();
    }
    let ts = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    format!("{}-{ts}", language_prefix(language))
}

fn language_prefix(language: DictionaryLanguage) -> &'static str {
    match language {
        DictionaryLanguage::Japanese => "ja",
        DictionaryLanguage::Hangul => "ko",
    }
}

fn matches_language(script: SegmentScript, language: DictionaryLanguage) -> bool {
    match (language, script) {
        (DictionaryLanguage::Japanese, SegmentScript::Hiragana)
        | (DictionaryLanguage::Japanese, SegmentScript::Katakana) => true,
        (DictionaryLanguage::Hangul, SegmentScript::Hangul) => true,
        _ => false,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tokenizer::{SegmentScript, normalize_query};
    use std::collections::HashSet;
    use tempfile::tempdir;

    #[test]
    fn segments_dictionary_tokens_and_fallbacks() {
        let mut entries = HashSet::new();
        entries.insert("こん".to_string());
        let config = DictionaryConfig {
            japanese: Some(ScriptDictionary {
                version: Some("v1".to_string()),
                entries,
            }),
            hangul: None,
        };
        let segmenter = DictionarySegmenter::new(config);
        let normalizer = normalize_query();
        let mut out = Vec::new();
        let mut seen = HashSet::new();

        let used = segmenter.segment(
            "こんにちは",
            0,
            SegmentScript::Hiragana,
            normalizer.as_ref(),
            &mut out,
            &mut seen,
        );

        assert!(used, "expected dictionary to be applied when provided");
        assert!(
            out.iter().any(|t| t.term == "こん"),
            "expected dictionary token present, got {:?}",
            out
        );
        assert!(
            out.iter().any(|t| t.start == 12),
            "expected fallback tokens for unmatched spans, got {:?}",
            out
        );
    }

    #[test]
    fn trains_and_exports_dictionaries() {
        let corpus = vec![
            "こんにちは世界".to_string(),
            "こんにちは友達".to_string(),
            "안녕하세요 세계".to_string(),
        ];
        let config = DictionaryTrainingConfig {
            min_freq: 1,
            min_token_len: 2,
            max_token_len: 3,
            max_entries: 4,
            version: Some("v1".to_string()),
        };

        let dictionaries = train_dictionary_config(&corpus, config);
        let segmenter = DictionarySegmenter::new(dictionaries.clone());
        let exports = segmenter.export();

        assert!(
            dictionaries.japanese.is_some(),
            "expected japanese dictionary"
        );
        assert!(dictionaries.hangul.is_some(), "expected hangul dictionary");
        assert_eq!(exports.len(), 2, "expected exports per language");
        let ja_export = exports
            .iter()
            .find(|e| matches!(e.metadata.language, DictionaryLanguage::Japanese))
            .expect("japanese export present");
        assert_eq!(ja_export.metadata.entry_count, ja_export.entries.len());
        assert!(
            ja_export
                .metadata
                .generated_at
                .elapsed()
                .unwrap_or_default()
                .as_secs()
                < 5,
            "expected recent generated_at, got {:?}",
            ja_export.metadata.generated_at
        );
        assert!(
            ja_export.metadata.version.starts_with("v1"),
            "expected provided version, got {}",
            ja_export.metadata.version
        );
    }

    #[test]
    fn saves_and_loads_dictionary_config() {
        let dir = tempdir().unwrap();
        let path = dir.path().join("dict.json");

        let mut entries = HashSet::new();
        entries.insert("こん".to_string());
        let config = DictionaryConfig {
            japanese: Some(ScriptDictionary {
                version: Some("v1".to_string()),
                entries,
            }),
            hangul: None,
        };

        save_dictionary(&path, &config).unwrap();
        let loaded = load_dictionary(&path).unwrap();
        assert_eq!(
            loaded.japanese.unwrap().entries.len(),
            1,
            "expected saved japanese entries"
        );
    }

    fn save_dictionary(path: &std::path::Path, config: &DictionaryConfig) -> std::io::Result<()> {
        let data = serde_json::to_vec(config).map_err(to_io_err)?;
        std::fs::write(path, data)
    }

    fn load_dictionary(path: &std::path::Path) -> std::io::Result<DictionaryConfig> {
        let data = std::fs::read(path)?;
        serde_json::from_slice(&data).map_err(to_io_err)
    }

    fn to_io_err(err: impl std::fmt::Display) -> std::io::Error {
        std::io::Error::new(std::io::ErrorKind::Other, err.to_string())
    }
}