use std::collections::BTreeMap;
use std::path::PathBuf;
use gaze_types::PiiClass;
use super::error::NerLoadError;
pub const MODEL_FILE: &str = "model.onnx";
pub const TOKENIZER_FILE: &str = "tokenizer.json";
pub const CONFIG_FILE: &str = "config.json";
pub const LABELS_FILE: &str = "labels.json";
pub const CHECKSUMS_FILE: &str = "SHA256SUMS";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LabelMap(pub BTreeMap<String, PiiClass>);
impl LabelMap {
pub fn get(&self, conll_label: &str) -> Option<&PiiClass> {
self.0.get(conll_label)
}
pub fn resolve(&self, tag: &str, entity: &str) -> Option<&PiiClass> {
self.0.get(tag).or_else(|| self.0.get(entity))
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn keys(&self) -> impl Iterator<Item = &str> {
self.0.keys().map(String::as_str)
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct NerOptions {
pub locale: Option<String>,
pub threshold: f32,
}
impl Default for NerOptions {
fn default() -> Self {
Self {
locale: None,
threshold: 0.3,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct NerSpanResult {
pub span: std::ops::Range<usize>,
pub class: PiiClass,
pub score: f32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NerBackendKind {
Ort,
Gliner,
}
impl NerBackendKind {
pub(crate) fn parse(raw: Option<&str>) -> Result<Self, NerLoadError> {
match raw.map(str::trim).filter(|value| !value.is_empty()) {
None => Ok(Self::Ort),
Some("ort") | Some("onnxruntime") | Some("bert-ort") => Ok(Self::Ort),
Some("gliner") | Some("gliner-ort") => Ok(Self::Gliner),
Some(other) => Err(NerLoadError::UnsupportedBackend {
backend: other.to_string(),
}),
}
}
pub(crate) fn as_str(self) -> &'static str {
match self {
Self::Ort => "ort",
Self::Gliner => "gliner",
}
}
}
#[derive(Debug, Clone)]
pub struct VerifiedArtifacts {
pub model_dir: PathBuf,
pub backend_kind: NerBackendKind,
pub recognizer_model_id: String,
pub recognizer_model_version: String,
pub labels: LabelMap,
pub id2label: Vec<String>,
}