fetchium-core 1.0.0

Fetchium core library — search, extract, rank, validate, cache
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
//! Evidence Graph Builder (EGB) — builds citation/corroboration graphs between results.
//!
//! Detects when multiple sources agree on the same facts, strengthening confidence.
//! Uses term-overlap and URL cross-references to build an evidence network.
//! Results with high corroboration scores are more trustworthy.

use crate::types::ResultItem;
use std::collections::{HashMap, HashSet};

/// Configuration for the evidence graph builder.
#[derive(Debug, Clone)]
pub struct EvidenceConfig {
    /// Minimum Jaccard similarity to consider two results corroborating.
    pub corroboration_threshold: f64,
    /// Bonus multiplier applied to well-corroborated results.
    pub corroboration_boost: f64,
    /// Maximum number of edges per node (prevents dense graphs).
    pub max_edges_per_node: usize,
}

impl Default for EvidenceConfig {
    fn default() -> Self {
        Self {
            corroboration_threshold: 0.15,
            corroboration_boost: 1.2,
            max_edges_per_node: 5,
        }
    }
}

/// An edge in the evidence graph representing agreement between two results.
#[derive(Debug, Clone)]
pub struct EvidenceEdge {
    /// Index of the source result.
    pub from: usize,
    /// Index of the target result.
    pub to: usize,
    /// Strength of the corroboration (Jaccard similarity).
    pub strength: f64,
    /// Terms shared between the two results.
    pub shared_terms: Vec<String>,
}

/// A node in the evidence graph with its corroboration score.
#[derive(Debug, Clone)]
pub struct EvidenceNode {
    /// Index into the original results.
    pub result_index: usize,
    /// How many other results corroborate this one.
    pub corroboration_count: usize,
    /// Aggregate corroboration strength (sum of edge weights).
    pub corroboration_strength: f64,
    /// The result is from a unique source (no corroboration).
    pub is_unique_claim: bool,
}

/// The complete evidence graph.
#[derive(Debug, Clone)]
pub struct EvidenceGraph {
    pub nodes: Vec<EvidenceNode>,
    pub edges: Vec<EvidenceEdge>,
    /// Average corroboration across all results.
    pub mean_corroboration: f64,
    /// Fraction of results with at least one corroboration.
    pub coverage: f64,
}

/// Build an evidence graph from search results.
///
/// Computes pairwise similarity between all results and builds a graph
/// where edges represent agreement/corroboration.
pub fn build_evidence_graph(results: &[ResultItem], config: &EvidenceConfig) -> EvidenceGraph {
    if results.is_empty() {
        return EvidenceGraph {
            nodes: vec![],
            edges: vec![],
            mean_corroboration: 0.0,
            coverage: 0.0,
        };
    }

    let n = results.len();

    // Extract term sets for all results
    let term_sets: Vec<HashSet<String>> = results.iter().map(extract_terms).collect();

    // Extract domains to avoid self-corroboration from same source
    let domains: Vec<String> = results.iter().map(|r| extract_domain(&r.url)).collect();

    // Build edges
    let mut edges = Vec::new();
    let mut edge_counts: HashMap<usize, usize> = HashMap::new();

    for i in 0..n {
        for j in (i + 1)..n {
            // Skip same-domain results (not independent corroboration)
            if !domains[i].is_empty() && domains[i] == domains[j] {
                continue;
            }

            // Check edge limits
            let i_count = edge_counts.get(&i).copied().unwrap_or(0);
            let j_count = edge_counts.get(&j).copied().unwrap_or(0);
            if i_count >= config.max_edges_per_node || j_count >= config.max_edges_per_node {
                continue;
            }

            let similarity = jaccard(&term_sets[i], &term_sets[j]);
            if similarity >= config.corroboration_threshold {
                let shared: Vec<String> =
                    term_sets[i].intersection(&term_sets[j]).cloned().collect();

                edges.push(EvidenceEdge {
                    from: i,
                    to: j,
                    strength: similarity,
                    shared_terms: shared,
                });

                *edge_counts.entry(i).or_insert(0) += 1;
                *edge_counts.entry(j).or_insert(0) += 1;
            }
        }
    }

    // Build nodes
    let mut node_strengths: Vec<f64> = vec![0.0; n];
    let mut node_counts: Vec<usize> = vec![0; n];

    for edge in &edges {
        node_strengths[edge.from] += edge.strength;
        node_strengths[edge.to] += edge.strength;
        node_counts[edge.from] += 1;
        node_counts[edge.to] += 1;
    }

    let nodes: Vec<EvidenceNode> = (0..n)
        .map(|i| EvidenceNode {
            result_index: i,
            corroboration_count: node_counts[i],
            corroboration_strength: node_strengths[i],
            is_unique_claim: node_counts[i] == 0,
        })
        .collect();

    // Compute graph-level metrics
    let total_strength: f64 = node_strengths.iter().sum();
    let mean_corroboration = if n > 0 {
        total_strength / n as f64
    } else {
        0.0
    };
    let corroborated_count = node_counts.iter().filter(|&&c| c > 0).count();
    let coverage = if n > 0 {
        corroborated_count as f64 / n as f64
    } else {
        0.0
    };

    EvidenceGraph {
        nodes,
        edges,
        mean_corroboration,
        coverage,
    }
}

/// Apply corroboration boosts to result scores.
///
/// Results confirmed by multiple independent sources get their score boosted.
pub fn apply_evidence_boost(
    results: &mut [ResultItem],
    graph: &EvidenceGraph,
    config: &EvidenceConfig,
) {
    for node in &graph.nodes {
        if node.corroboration_count > 0 && node.result_index < results.len() {
            let boost = 1.0
                + (config.corroboration_boost - 1.0) * (node.corroboration_count as f64).min(3.0)
                    / 3.0;
            let result = &mut results[node.result_index];
            let current = result.score.unwrap_or(0.5);
            result.score = Some((current * boost).min(1.0));
        }
    }
}

// ─── Internal helpers ──────────────────────────────────────

fn extract_terms(item: &ResultItem) -> HashSet<String> {
    let text = format!("{} {}", item.title, item.snippet).to_lowercase();
    text.split(|c: char| !c.is_alphanumeric())
        .filter(|t| t.len() >= 3 && !is_stop_word(t))
        .map(|t| t.to_string())
        .collect()
}

fn extract_domain(url: &str) -> String {
    url::Url::parse(url)
        .ok()
        .and_then(|u| u.host_str().map(|h| h.to_string()))
        .unwrap_or_default()
}

fn jaccard(a: &HashSet<String>, b: &HashSet<String>) -> f64 {
    if a.is_empty() && b.is_empty() {
        return 0.0;
    }
    let intersection = a.intersection(b).count();
    let union = a.union(b).count();
    if union == 0 {
        return 0.0;
    }
    intersection as f64 / union as f64
}

fn is_stop_word(word: &str) -> bool {
    const STOP: &[&str] = &[
        "the", "and", "for", "are", "but", "not", "you", "all", "can", "was", "one", "our", "out",
        "has", "had", "how", "its", "may", "new", "now", "see", "way", "who", "did", "get", "let",
        "say", "she", "too", "use", "this", "that", "with", "have", "from", "they", "been", "some",
        "when", "what", "your", "each", "make", "like", "just", "than", "them", "very", "will",
        "more", "also",
    ];
    STOP.contains(&word)
}

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

    fn make_item(title: &str, snippet: &str, url: &str) -> ResultItem {
        ResultItem {
            title: title.into(),
            url: url.into(),
            snippet: snippet.into(),
            rank: 0,
            backend: BackendId::DuckDuckGo,
            score: Some(0.5),
            published_date: None,
        }
    }

    #[test]
    fn empty_results_empty_graph() {
        let graph = build_evidence_graph(&[], &EvidenceConfig::default());
        assert!(graph.nodes.is_empty());
        assert!(graph.edges.is_empty());
        assert_eq!(graph.mean_corroboration, 0.0);
    }

    #[test]
    fn single_result_no_edges() {
        let items = vec![make_item(
            "Rust",
            "A systems language",
            "https://rust-lang.org",
        )];
        let graph = build_evidence_graph(&items, &EvidenceConfig::default());
        assert_eq!(graph.nodes.len(), 1);
        assert!(graph.edges.is_empty());
        assert!(graph.nodes[0].is_unique_claim);
    }

    #[test]
    fn corroborating_results_create_edge() {
        let items = vec![
            make_item(
                "Rust Memory Safety",
                "Rust guarantees memory safety through ownership and borrowing",
                "https://a.com/rust",
            ),
            make_item(
                "Rust Safety Features",
                "Rust ownership system ensures memory safety without garbage collection",
                "https://b.com/rust",
            ),
        ];
        let graph = build_evidence_graph(&items, &EvidenceConfig::default());
        assert!(
            !graph.edges.is_empty(),
            "corroborating results should create edges"
        );
        assert!(graph.coverage > 0.0);
    }

    #[test]
    fn unrelated_results_no_edges() {
        let items = vec![
            make_item(
                "Rust Programming",
                "Systems language with ownership",
                "https://a.com",
            ),
            make_item(
                "Cooking Recipe",
                "How to bake chocolate cake flour sugar",
                "https://b.com",
            ),
        ];
        let graph = build_evidence_graph(&items, &EvidenceConfig::default());
        assert!(
            graph.edges.is_empty(),
            "unrelated results should have no edges"
        );
    }

    #[test]
    fn same_domain_not_corroboration() {
        let items = vec![
            make_item(
                "Rust Article 1",
                "Rust ownership memory safety borrow",
                "https://example.com/a",
            ),
            make_item(
                "Rust Article 2",
                "Rust ownership memory safety lifetimes",
                "https://example.com/b",
            ),
        ];
        let graph = build_evidence_graph(&items, &EvidenceConfig::default());
        assert!(
            graph.edges.is_empty(),
            "same-domain results should not corroborate"
        );
    }

    #[test]
    fn evidence_boost_increases_scores() {
        let mut items = vec![
            make_item(
                "Rust Safety",
                "Rust memory safety ownership borrow",
                "https://a.com",
            ),
            make_item(
                "Rust Ownership",
                "Rust memory safety borrow checker system",
                "https://b.com",
            ),
        ];
        let config = EvidenceConfig::default();
        let graph = build_evidence_graph(&items, &config);

        let scores_before: Vec<f64> = items.iter().map(|r| r.score.unwrap_or(0.0)).collect();
        apply_evidence_boost(&mut items, &graph, &config);
        let scores_after: Vec<f64> = items.iter().map(|r| r.score.unwrap_or(0.0)).collect();

        if !graph.edges.is_empty() {
            assert!(
                scores_after[0] >= scores_before[0],
                "corroborated result should be boosted"
            );
        }
    }

    #[test]
    fn max_edges_respected() {
        let config = EvidenceConfig {
            max_edges_per_node: 2,
            corroboration_threshold: 0.1,
            ..EvidenceConfig::default()
        };

        // Create 5 similar results from different domains
        let items: Vec<ResultItem> = (0..5)
            .map(|i| {
                make_item(
                    "Rust memory safety ownership borrow checker",
                    "The Rust programming language guarantees memory safety",
                    &format!("https://site{i}.com/article"),
                )
            })
            .collect();

        let graph = build_evidence_graph(&items, &config);

        // No node should have more than max_edges edges
        for node in &graph.nodes {
            assert!(
                node.corroboration_count <= config.max_edges_per_node,
                "node {} has {} edges, max is {}",
                node.result_index,
                node.corroboration_count,
                config.max_edges_per_node,
            );
        }
    }

    #[test]
    fn coverage_metric_correct() {
        let items = vec![
            make_item(
                "Rust Safety",
                "Rust ownership borrow memory safe",
                "https://a.com",
            ),
            make_item(
                "Rust Borrow",
                "Rust ownership borrow memory checker",
                "https://b.com",
            ),
            make_item(
                "Cooking Tips",
                "How to bake cookies flour sugar eggs",
                "https://c.com",
            ),
        ];
        let graph = build_evidence_graph(&items, &EvidenceConfig::default());

        // Cooking result should be unique, Rust results may corroborate
        assert!(graph.coverage <= 1.0);
        assert!(graph.coverage >= 0.0);
    }
}