use async_trait::async_trait;
use klieo_core::error::MemoryError;
use klieo_memory_graph::{EntityExtractor, EntityRef, EntityType};
use std::collections::HashSet;
pub struct BuiltinExtractor {
typed_id_re: regex::Regex,
}
impl Default for BuiltinExtractor {
fn default() -> Self {
Self {
typed_id_re: regex::Regex::new(r"\b[A-Z][A-Z0-9]*-\d+\b")
.expect("BuiltinExtractor typed-id regex is statically valid"),
}
}
}
#[async_trait]
impl EntityExtractor for BuiltinExtractor {
async fn extract(
&self,
text: &str,
hints: &[EntityRef],
) -> Result<Vec<EntityRef>, MemoryError> {
let mut seen: HashSet<(String, String)> = HashSet::new();
let mut out: Vec<EntityRef> = Vec::with_capacity(hints.len());
for hint in hints {
let key = (hint.entity_type.as_str().to_owned(), hint.name.clone());
if seen.insert(key) {
out.push(hint.clone());
}
}
for hit in self.typed_id_re.find_iter(text) {
let candidate = EntityRef::new(EntityType::Ticket, hit.as_str());
let key = (
candidate.entity_type.as_str().to_owned(),
candidate.name.clone(),
);
if seen.insert(key) {
out.push(candidate);
}
}
Ok(out)
}
}