gliner/model/output/decoded/
sort.rs

1//! Sort spans by offsets (which is expected by greedy-search)
2
3use composable::Composable;
4use crate::util::result::Result;
5use super::SpanOutput;
6
7#[derive(Default)] 
8pub struct SpanSort {}
9
10
11/// Composable: SpanOutput => SpanOutput
12impl Composable<SpanOutput, SpanOutput> for SpanSort {
13    fn apply(&self, input: SpanOutput) -> Result<SpanOutput> {      
14        let mut spans = input.spans;
15        for sequence in &mut spans {
16            // "Unstable" sort (which is perfectly safe despite the name ;) is more efficient, and sufficient 
17            // in our case as we don't need to preserve the initial order of equal elements. Also note that
18            // calling `cmp()` on a tuple does exactly what we ant here (sort by start, then end, offsets).
19            sequence.sort_unstable_by(|s1, s2| s1.offsets().cmp(&s2.offsets()));
20        }
21        Ok(SpanOutput::new(input.texts, input.entities, spans))
22    }
23}