memvid-core 2.0.139

Core library for Memvid v2, a crash-safe, deterministic, single-file AI memory.
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
//! Multi-page table merging.
//!
//! This module handles the detection and merging of tables that span
//! multiple pages in a document.

use super::types::{ExtractedTable, TableExtractionOptions, TableQuality, TableRow};

/// Merge tables that span multiple pages.
///
/// Tables are candidates for merging when:
/// 1. They are on consecutive pages
/// 2. They have the same column count
/// 3. They are from the same source file
/// 4. Their headers are similar (if both have headers)
///
/// # Arguments
/// * `tables` - Vector of tables to potentially merge
/// * `options` - Extraction options containing merge thresholds
///
/// # Returns
/// Vector of tables with multi-page tables merged
#[must_use]
pub fn merge_multi_page_tables(
    tables: Vec<ExtractedTable>,
    options: &TableExtractionOptions,
) -> Vec<ExtractedTable> {
    if !options.merge_multi_page || tables.len() < 2 {
        return tables;
    }

    // Sort tables by page order
    let mut sorted_tables = tables;
    sorted_tables.sort_by_key(|t| (t.page_start, t.page_end));

    let mut merged: Vec<ExtractedTable> = Vec::new();
    let mut skip_indices = std::collections::HashSet::new();

    for (i, table) in sorted_tables.iter().enumerate() {
        if skip_indices.contains(&i) {
            continue;
        }

        let mut current = table.clone();

        // Look for continuation on subsequent pages
        for (j, candidate) in sorted_tables.iter().enumerate().skip(i + 1) {
            if skip_indices.contains(&j) {
                continue;
            }

            // Check if candidate should be merged with current
            let merge_score = calculate_merge_score(&current, candidate, options);

            if merge_score >= options.header_similarity_threshold {
                current = merge_two_tables(current, candidate.clone());
                skip_indices.insert(j);
            } else {
                // Tables are sorted, so if this one doesn't merge, neither will later ones
                // (unless they're on a much later page)
                if candidate.page_start > current.page_end + 2 {
                    break;
                }
            }
        }

        merged.push(current);
    }

    merged
}

/// Calculate a score indicating how likely two tables should be merged.
///
/// Returns a score from 0.0 to 1.0, where higher means more likely to merge.
fn calculate_merge_score(
    first: &ExtractedTable,
    second: &ExtractedTable,
    options: &TableExtractionOptions,
) -> f32 {
    let mut score = 0.0f32;
    let mut factors = 0;

    // Factor 1: Must be consecutive pages (or nearly consecutive)
    let page_gap = second.page_start as i32 - first.page_end as i32;
    if page_gap == 1 {
        score += 1.0;
        factors += 1;
    } else if page_gap == 0 {
        // Same page - likely different tables
        return 0.0;
    } else if page_gap <= 2 {
        // Small gap - might be continuation with intervening content
        score += 0.5;
        factors += 1;
    } else {
        // Too far apart
        return 0.0;
    }

    // Factor 2: Same source file
    if first.source_file == second.source_file {
        score += 1.0;
        factors += 1;
    } else {
        return 0.0;
    }

    // Factor 3: Same column count
    if first.n_cols == second.n_cols {
        score += 1.0;
        factors += 1;
    } else {
        // Different column count - not a continuation
        return 0.0;
    }

    // Factor 4: Header similarity (if both have headers)
    if !first.headers.is_empty() && !second.headers.is_empty() {
        let similarity = calculate_header_similarity(&first.headers, &second.headers);
        if similarity >= options.header_similarity_threshold {
            score += similarity;
            factors += 1;
        } else {
            // Headers are different - probably different tables
            score -= 0.5;
        }
    } else if first.headers.is_empty() && second.headers.is_empty() {
        // Both without headers - could be continuation
        score += 0.5;
        factors += 1;
    }

    // Factor 5: Detection mode compatibility
    if first.detection_mode == second.detection_mode {
        score += 0.5;
        factors += 1;
    }

    // Factor 6: Position heuristics
    // First table should end near bottom of page, second should start near top
    // (We don't have precise position data, so this is a placeholder)
    score += 0.3;
    factors += 1;

    if factors == 0 {
        return 0.0;
    }

    score / factors as f32
}

/// Calculate similarity between two header sets.
///
/// Returns a score from 0.0 (completely different) to 1.0 (identical).
fn calculate_header_similarity(h1: &[String], h2: &[String]) -> f32 {
    if h1.is_empty() || h2.is_empty() {
        return 0.0;
    }

    if h1.len() != h2.len() {
        return 0.0;
    }

    // Normalize headers for comparison
    let norm1: Vec<String> = h1
        .iter()
        .map(|s| s.to_lowercase().trim().to_string())
        .collect();

    let norm2: Vec<String> = h2
        .iter()
        .map(|s| s.to_lowercase().trim().to_string())
        .collect();

    // Count exact matches
    let exact_matches = norm1.iter().zip(&norm2).filter(|(a, b)| a == b).count();

    // Count partial matches (one contains the other)
    let partial_matches = norm1
        .iter()
        .zip(&norm2)
        .filter(|(a, b)| a != b && (a.contains(b.as_str()) || b.contains(a.as_str())))
        .count();

    let total = h1.len();
    (exact_matches as f32 + partial_matches as f32 * 0.5) / total as f32
}

/// Merge two tables into one.
fn merge_two_tables(mut first: ExtractedTable, second: ExtractedTable) -> ExtractedTable {
    // Update page range
    first.page_end = second.page_end;

    // Determine if second table's first row is a repeated header
    let skip_header = should_skip_header(&first, &second);

    // Get rows to add from second table
    let rows_to_add: Vec<TableRow> = if skip_header {
        second
            .rows
            .into_iter()
            .filter(|r| !r.is_header_row)
            .collect()
    } else {
        second.rows
    };

    // Renumber rows and append
    let offset = first.rows.len();
    for mut row in rows_to_add {
        row.row_index += offset;
        first.rows.push(row);
    }

    // Update counts
    first.n_rows = first.rows.iter().filter(|r| !r.is_header_row).count();

    // Merge warnings
    first.warnings.extend(second.warnings);
    first.warnings.push(format!(
        "Merged with table from page {} (detected as continuation)",
        second.page_start
    ));

    // Update extraction time
    first.extraction_ms += second.extraction_ms;

    // Adjust quality for merged tables
    first.quality = combined_quality(first.quality, second.quality);
    first.confidence_score = f32::midpoint(first.confidence_score, second.confidence_score) - 0.05;
    first.confidence_score = first.confidence_score.max(0.0);

    first
}

/// Determine if the second table's header row should be skipped.
fn should_skip_header(first: &ExtractedTable, second: &ExtractedTable) -> bool {
    // If second table doesn't have headers, nothing to skip
    if second.headers.is_empty() {
        return false;
    }

    // If first table doesn't have headers, keep second's headers
    if first.headers.is_empty() {
        return false;
    }

    // Check header similarity - if very similar, it's a repeated header
    let similarity = calculate_header_similarity(&first.headers, &second.headers);
    similarity >= 0.8
}

/// Combine quality ratings from two tables.
fn combined_quality(q1: TableQuality, q2: TableQuality) -> TableQuality {
    match (q1, q2) {
        (TableQuality::High, TableQuality::High) => TableQuality::High,
        (TableQuality::Low, _) | (_, TableQuality::Low) => TableQuality::Medium,
        _ => TableQuality::Medium,
    }
}

/// Find potential table continuation candidates.
///
/// This is a utility function that identifies tables that might
/// be continuations of other tables without actually merging them.
#[must_use]
pub fn find_continuation_candidates(
    tables: &[ExtractedTable],
    options: &TableExtractionOptions,
) -> Vec<(usize, usize, f32)> {
    let mut candidates = Vec::new();

    for (i, first) in tables.iter().enumerate() {
        for (j, second) in tables.iter().enumerate().skip(i + 1) {
            let score = calculate_merge_score(first, second, options);
            if score >= options.header_similarity_threshold * 0.8 {
                candidates.push((i, j, score));
            }
        }
    }

    // Sort by score descending
    candidates.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));

    candidates
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::table::types::TableCell;

    fn make_test_table(
        id: &str,
        source: &str,
        page_start: u32,
        page_end: u32,
        headers: Vec<&str>,
        n_rows: usize,
    ) -> ExtractedTable {
        let mut table = ExtractedTable::new(id, source);
        table.page_start = page_start;
        table.page_end = page_end;
        table.headers = headers.iter().map(|s| (*s).to_string()).collect();
        table.n_cols = headers.len();
        table.n_rows = n_rows;

        // Add header row
        let header_cells: Vec<TableCell> = headers
            .iter()
            .enumerate()
            .map(|(i, h)| TableCell::new(*h, i).as_header())
            .collect();
        table
            .rows
            .push(TableRow::new(0, page_start, header_cells).as_header());

        // Add data rows
        for row_idx in 0..n_rows {
            let cells: Vec<TableCell> = (0..headers.len())
                .map(|col| TableCell::new(format!("r{}c{}", row_idx + 1, col), col))
                .collect();
            table
                .rows
                .push(TableRow::new(row_idx + 1, page_start, cells));
        }

        table
    }

    #[test]
    fn test_header_similarity_identical() {
        let h1 = vec![
            "Date".to_string(),
            "Amount".to_string(),
            "Description".to_string(),
        ];
        let h2 = vec![
            "Date".to_string(),
            "Amount".to_string(),
            "Description".to_string(),
        ];

        let similarity = calculate_header_similarity(&h1, &h2);
        assert!((similarity - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_header_similarity_case_insensitive() {
        let h1 = vec!["DATE".to_string(), "AMOUNT".to_string()];
        let h2 = vec!["date".to_string(), "amount".to_string()];

        let similarity = calculate_header_similarity(&h1, &h2);
        assert!((similarity - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_header_similarity_different() {
        let h1 = vec!["Date".to_string(), "Amount".to_string()];
        let h2 = vec!["Name".to_string(), "Address".to_string()];

        let similarity = calculate_header_similarity(&h1, &h2);
        assert!(similarity < 0.5);
    }

    #[test]
    fn test_merge_consecutive_tables() {
        let t1 = make_test_table("t1", "doc.pdf", 1, 1, vec!["A", "B", "C"], 5);
        let t2 = make_test_table("t2", "doc.pdf", 2, 2, vec!["A", "B", "C"], 3);

        let options = TableExtractionOptions::default();
        let merged = merge_multi_page_tables(vec![t1, t2], &options);

        assert_eq!(merged.len(), 1);
        assert_eq!(merged[0].page_start, 1);
        assert_eq!(merged[0].page_end, 2);
        // 5 + 3 rows (header row not counted)
        assert_eq!(merged[0].n_rows, 8);
    }

    #[test]
    fn test_no_merge_different_columns() {
        let t1 = make_test_table("t1", "doc.pdf", 1, 1, vec!["A", "B", "C"], 5);
        let t2 = make_test_table("t2", "doc.pdf", 2, 2, vec!["X", "Y"], 3);

        let options = TableExtractionOptions::default();
        let merged = merge_multi_page_tables(vec![t1, t2], &options);

        assert_eq!(merged.len(), 2);
    }

    #[test]
    fn test_no_merge_non_consecutive_pages() {
        let t1 = make_test_table("t1", "doc.pdf", 1, 1, vec!["A", "B"], 5);
        let t2 = make_test_table("t2", "doc.pdf", 5, 5, vec!["A", "B"], 3);

        let options = TableExtractionOptions::default();
        let merged = merge_multi_page_tables(vec![t1, t2], &options);

        assert_eq!(merged.len(), 2);
    }

    #[test]
    fn test_merge_disabled() {
        let t1 = make_test_table("t1", "doc.pdf", 1, 1, vec!["A", "B"], 5);
        let t2 = make_test_table("t2", "doc.pdf", 2, 2, vec!["A", "B"], 3);

        let options = TableExtractionOptions {
            merge_multi_page: false,
            ..Default::default()
        };
        let merged = merge_multi_page_tables(vec![t1, t2], &options);

        assert_eq!(merged.len(), 2);
    }

    #[test]
    fn test_combined_quality() {
        assert_eq!(
            combined_quality(TableQuality::High, TableQuality::High),
            TableQuality::High
        );
        assert_eq!(
            combined_quality(TableQuality::High, TableQuality::Medium),
            TableQuality::Medium
        );
        assert_eq!(
            combined_quality(TableQuality::High, TableQuality::Low),
            TableQuality::Medium
        );
        assert_eq!(
            combined_quality(TableQuality::Low, TableQuality::Low),
            TableQuality::Medium
        );
    }
}