Skip to main content

veil_engine/
ner.rs

1//! Names found by a statistical model (named-entity recognition).
2//!
3//! The engine does no I/O, so the model itself lives elsewhere and is
4//! plugged in through [`EntityRecognizer`].
5
6use crate::kind::EntityKind;
7use crate::RecognizerError;
8
9/// A name the model found. Offsets are bytes into the text it was given.
10#[derive(Clone, Debug, PartialEq)]
11pub struct Entity {
12    /// Byte offset where the name starts.
13    pub start: usize,
14    /// Byte offset just past the name.
15    pub end: usize,
16    /// One of `PERSON`, `ORG` or `LOCATION`.
17    pub kind: EntityKind,
18    /// The model's confidence, 0 to 1.
19    pub score: f32,
20}
21
22/// Finds names in text. Implementations should return every entity they
23/// find with its score; the engine applies the threshold from the rules.
24pub trait EntityRecognizer: Send + Sync {
25    /// Returns every name found in `text`, with byte offsets into it.
26    fn recognize(&self, text: &str) -> Result<Vec<Entity>, RecognizerError>;
27}