aspero-engine 0.5.1

Routing, caching and resilience engine for aspero
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
//! Turning scored candidates into a verdict, why ([`Reason`]), and a
//! one-sentence reason in English.

use aspero_core::matching::{self, PLAUSIBLE, VERIFIED};
use aspero_core::publication::first_page;
use aspero_core::{Agreement, Claim, Update, UpdateKind};

use crate::resolve::{Candidate, ClaimExt, ResolveReport};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Verdict {
    /// A work matching the claim exists (and so do its claimed identifiers).
    Verified,
    /// A work partly matching the claim exists; check the details.
    Plausible,
    /// The claimed identifier is wrong: it doesn't exist, or it belongs to
    /// a work that doesn't match the rest of the claim.
    Mismatch,
    /// No matching work was found.
    NotFound,
    /// Providers failed, so nothing conclusive can be said.
    Unverifiable,
}

impl Verdict {
    pub fn as_str(self) -> &'static str {
        match self {
            Verdict::Verified => "verified",
            Verdict::Plausible => "plausible",
            Verdict::Mismatch => "mismatch",
            Verdict::NotFound => "not_found",
            Verdict::Unverifiable => "unverifiable",
        }
    }
}

#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Verification {
    pub verdict: Verdict,
    /// One sentence, for people and agents: [`why`](Self::why) and
    /// [`notes`](Self::notes) in English.
    pub reason: String,
    /// Why, as data, for front ends that word it themselves.
    pub why: Reason,
    /// What to know about the work the verdict is about, whatever the
    /// verdict: its retraction or withdrawal, and its newer version.
    pub notes: Vec<Update>,
    /// The candidate the verdict is about, as an index into
    /// `report.candidates`: the matching work, or the best guess at what was
    /// meant.
    pub best: Option<usize>,
    pub report: ResolveReport,
}

/// Why a verdict was reached. "The work" is the verdict's candidate
/// ([`Verification::best`]); "the identifiers" are those the claim gave.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Reason {
    /// The citation matches the work. (Verified)
    Matches,
    /// The citation partly matches the work. (Plausible)
    PartlyMatches { differs: Vec<Difference> },
    /// The closest work shares the title, but other details differ.
    /// (Mismatch)
    Closest { differs: Vec<Difference> },
    /// No matching work. (NotFound)
    NoMatch,
    /// The identifiers exist; the claim gives nothing else to compare.
    /// (Verified)
    IdExists,
    /// The identifiers exist and the citation agrees. (Verified)
    IdAgrees,
    /// The identifiers exist and the citation partly matches. (Plausible)
    IdPartlyMatches { differs: Vec<Difference> },
    /// The identifiers are another work (candidate `cited`); the citation
    /// matches the work. (Mismatch)
    IdElsewhere { cited: usize },
    /// The identifiers' work (the work) doesn't match the citation, and no
    /// other does. (Mismatch)
    IdDisagrees,
    /// The identifiers don't exist; the citation matches the work.
    /// (Mismatch)
    IdMissingElsewhere,
    /// The identifiers don't exist, and no matching work was found.
    /// (NotFound)
    IdMissing,
    /// Providers failed, so nothing conclusive can be said. (Unverifiable)
    ProvidersFailed,
}

impl Reason {
    /// A short machine-friendly name.
    pub fn as_str(&self) -> &'static str {
        match self {
            Reason::Matches => "matches",
            Reason::PartlyMatches { .. } => "partly_matches",
            Reason::Closest { .. } => "closest",
            Reason::NoMatch => "no_match",
            Reason::IdExists => "id_exists",
            Reason::IdAgrees => "id_agrees",
            Reason::IdPartlyMatches { .. } => "id_partly_matches",
            Reason::IdElsewhere { .. } => "id_elsewhere",
            Reason::IdDisagrees => "id_disagrees",
            Reason::IdMissingElsewhere => "id_missing_elsewhere",
            Reason::IdMissing => "id_missing",
            Reason::ProvidersFailed => "providers_failed",
        }
    }

    /// The fields that differ, where the reason has them.
    pub fn differs(&self) -> &[Difference] {
        match self {
            Reason::PartlyMatches { differs }
            | Reason::Closest { differs }
            | Reason::IdPartlyMatches { differs } => differs,
            _ => &[],
        }
    }
}

/// A cited field the work disagrees with, and the work's value if it's
/// worth showing.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Difference {
    pub field: CitedField,
    pub actual: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CitedField {
    Title,
    Authors,
    Year,
    Journal,
    Volume,
    FirstPage,
}

impl CitedField {
    pub fn as_str(self) -> &'static str {
        match self {
            CitedField::Title => "title",
            CitedField::Authors => "authors",
            CitedField::Year => "year",
            CitedField::Journal => "journal",
            CitedField::Volume => "volume",
            CitedField::FirstPage => "first_page",
        }
    }
}

impl std::fmt::Display for Difference {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.field.as_str().replace('_', " "))?;
        match (&self.actual, self.field) {
            (Some(v), CitedField::Journal) => write!(f, " (\"{v}\")"),
            (Some(v), _) => write!(f, " ({v})"),
            (None, _) => Ok(()),
        }
    }
}

/// The same title plus matching authors *or* year, with the other wrong: a
/// real work cited with an error (wrong year, misattributed authors), not a
/// missing one. The title alone proves little: short titles like "Protein
/// Folding" occur inside many longer ones.
fn near_miss(c: &Candidate) -> bool {
    let a = &c.assessment;
    a.title.agreement == Agreement::Match
        && (a.authors.agreement == Agreement::Match || a.year.agreement == Agreement::Match)
}

/// The fields that don't fully agree. With `known_only`, only fields both
/// sides gave and that disagree; otherwise also those the citation doesn't
/// confirm.
fn differences(c: &Candidate, known_only: bool) -> Vec<Difference> {
    let a = &c.assessment;
    let w = &c.record.work;
    let off = |check: &matching::FieldCheck| match known_only {
        true => matches!(check.agreement, Agreement::Mismatch | Agreement::Partial),
        false => check.agreement != Agreement::Match,
    };
    let diff = |field, actual: Option<String>| Difference { field, actual };
    [
        off(&a.title).then(|| diff(CitedField::Title, None)),
        off(&a.authors).then(|| diff(CitedField::Authors, None)),
        matches!(a.year.agreement, Agreement::Mismatch | Agreement::Partial)
            .then(|| diff(CitedField::Year, w.issued.map(|d| d.year().to_string()))),
        (a.container.agreement == Agreement::Mismatch)
            .then(|| diff(CitedField::Journal, w.container_title.clone())),
        (a.volume.agreement == Agreement::Mismatch)
            .then(|| diff(CitedField::Volume, w.volume.clone())),
        (a.page.agreement == Agreement::Mismatch).then(|| {
            let first = w.page.as_deref().map(|p| first_page(p).to_owned());
            diff(CitedField::FirstPage, first)
        }),
    ]
    .into_iter()
    .flatten()
    .collect()
}

/// The differences as a clause: "its year (2017) differs", "its authors and
/// year (2017) differ".
fn clause(differs: &[Difference]) -> Option<String> {
    let fields: Vec<String> = differs.iter().map(ToString::to_string).collect();
    match fields.len() {
        0 => None,
        1 => Some(format!("its {} differs", fields[0])),
        _ => Some(format!("its {} differ", fields.join(" and "))),
    }
}

fn describe(c: &Candidate) -> String {
    let w = &c.record.work;
    let title = w.title.as_deref().unwrap_or("(untitled)");
    let year = w
        .issued
        .map(|d| format!(" ({})", d.year()))
        .unwrap_or_default();
    let id = c.ids.first().map(|i| format!(", {i}")).unwrap_or_default();
    format!("\"{title}\"{year}{id}")
}

/// Turns gathered candidates into a verdict.
pub(crate) fn judge(claim: &Claim, report: ResolveReport) -> Verification {
    let (verdict, why, best) = decide(claim, &report);
    // Existing is not the same as citable or current: say so whatever the
    // verdict.
    let notes: Vec<Update> = best
        .map(|i| &report.candidates[i])
        .into_iter()
        .flat_map(|c| {
            [
                c.retraction(),
                c.updates.iter().find(|u| u.kind == UpdateKind::NewVersion),
            ]
        })
        .flatten()
        .cloned()
        .collect();
    let mut reason = explain(claim, &report, &why, best);
    if !notes.is_empty() {
        let notes: Vec<String> = notes.iter().map(describe_update).collect();
        reason = format!("{reason}; note: {}", notes.join("; "));
    }
    Verification {
        verdict,
        reason,
        why,
        notes,
        best,
        report,
    }
}

fn decide(claim: &Claim, report: &ResolveReport) -> (Verdict, Reason, Option<usize>) {
    let best = report.candidates.first();
    let best_conf = best.map_or(0.0, |c| c.assessment.confidence);
    let failed = !report.errors.is_empty()
        || report
            .lookups
            .iter()
            .any(|l| l.record.is_none() && !l.is_not_found());

    if claim.ids.is_empty() {
        return match best {
            Some(_) if best_conf >= VERIFIED => (Verdict::Verified, Reason::Matches, Some(0)),
            Some(c) if best_conf >= PLAUSIBLE => (
                Verdict::Plausible,
                Reason::PartlyMatches {
                    differs: differences(c, true),
                },
                Some(0),
            ),
            Some(c) if near_miss(c) => (
                Verdict::Mismatch,
                Reason::Closest {
                    differs: differences(c, false),
                },
                Some(0),
            ),
            _ if failed => (Verdict::Unverifiable, Reason::ProvidersFailed, None),
            _ => (Verdict::NotFound, Reason::NoMatch, None),
        };
    }
    let by_id = report
        .candidates
        .iter()
        .position(|c| c.assessment.ids == Agreement::Match);
    let elsewhere = |skip: Option<usize>| {
        report
            .candidates
            .iter()
            .enumerate()
            .position(|(i, c)| Some(i) != skip && c.assessment.confidence >= PLAUSIBLE)
    };
    match by_id.map(|i| (i, &report.candidates[i])) {
        Some((i, _)) if !claim.is_descriptive() => (Verdict::Verified, Reason::IdExists, Some(i)),
        Some((i, c)) if c.assessment.confidence >= VERIFIED => {
            (Verdict::Verified, Reason::IdAgrees, Some(i))
        }
        Some((i, c)) if c.assessment.confidence >= PLAUSIBLE => (
            Verdict::Plausible,
            Reason::IdPartlyMatches {
                differs: differences(c, true),
            },
            Some(i),
        ),
        Some((i, _)) => match elsewhere(Some(i)) {
            Some(j) => (Verdict::Mismatch, Reason::IdElsewhere { cited: i }, Some(j)),
            None => (Verdict::Mismatch, Reason::IdDisagrees, Some(i)),
        },
        None if failed => (Verdict::Unverifiable, Reason::ProvidersFailed, None),
        None => match elsewhere(None) {
            Some(j) => (Verdict::Mismatch, Reason::IdMissingElsewhere, Some(j)),
            None => (Verdict::NotFound, Reason::IdMissing, None),
        },
    }
}

/// The reason in English, without notes.
fn explain(claim: &Claim, report: &ResolveReport, why: &Reason, best: Option<usize>) -> String {
    let claimed = claim
        .ids
        .iter()
        .map(ToString::to_string)
        .collect::<Vec<_>>()
        .join(", ");
    let work = || {
        best.map(|i| describe(&report.candidates[i]))
            .unwrap_or_default()
    };
    let details = || clause(why.differs());
    match why {
        Reason::Matches => format!("matches {}", work()),
        Reason::PartlyMatches { .. } => format!(
            "partly matches {}; {}",
            work(),
            details().unwrap_or_else(|| "check the details".into())
        ),
        Reason::Closest { .. } => format!(
            "closest work is {}, but {}",
            work(),
            details().unwrap_or_else(|| "its details differ".into())
        ),
        Reason::NoMatch => "no matching work found".into(),
        Reason::IdExists => format!("{claimed} exists: {}", work()),
        Reason::IdAgrees => format!("{claimed} and the citation agree: {}", work()),
        Reason::IdPartlyMatches { .. } => format!(
            "{claimed} exists and partly matches the citation: {}{}",
            work(),
            details().map(|d| format!("; {d}")).unwrap_or_default()
        ),
        Reason::IdElsewhere { cited } => format!(
            "{claimed} is {}, but the citation matches {}",
            describe(&report.candidates[*cited]),
            work()
        ),
        Reason::IdDisagrees => {
            format!("{claimed} is {}, which does not match the citation", work())
        }
        Reason::IdMissingElsewhere => {
            format!("{claimed} does not exist; the citation matches {}", work())
        }
        Reason::IdMissing => format!("{claimed} does not exist and no matching work was found"),
        Reason::ProvidersFailed if claim.ids.is_empty() => {
            "providers failed; nothing conclusive".into()
        }
        Reason::ProvidersFailed => format!("could not look up {claimed}: providers failed"),
    }
}

/// "RETRACTED (2010-02-06, notice doi:…, via retraction-watch)".
pub fn describe_update(u: &Update) -> String {
    // The notice of a new version is the newer work itself.
    if u.kind == UpdateKind::NewVersion {
        let by = u
            .notice
            .as_ref()
            .map(|n| format!(" by {n}"))
            .unwrap_or_default();
        let date = u.date.map(|d| format!(" ({d})")).unwrap_or_default();
        return format!("superseded{by}{date}");
    }
    let what = match &u.kind {
        UpdateKind::Retraction => "RETRACTED".to_owned(),
        UpdateKind::Withdrawal => "WITHDRAWN".to_owned(),
        UpdateKind::ExpressionOfConcern => "expression of concern".to_owned(),
        UpdateKind::Correction => "corrected".to_owned(),
        other => other.as_str().replace('_', " "),
    };
    let details: Vec<String> = [
        u.date.map(|d| d.to_string()),
        u.notice.as_ref().map(|n| format!("notice {n}")),
        u.source.as_ref().map(|s| format!("via {s}")),
    ]
    .into_iter()
    .flatten()
    .collect();
    if details.is_empty() {
        what
    } else {
        format!("{what} ({})", details.join(", "))
    }
}