use super::super::confidence::Confidence;
use super::super::types::{IdentityId, TypeLabel};
use super::track::TrackRef;
use serde::{Deserialize, Serialize};
pub use super::super::types::IdentityId as _IdentityIdReexport;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum IdentitySource {
CrossDocCoref {
track_refs: Vec<TrackRef>,
},
KnowledgeBase {
kb_name: String,
kb_id: String,
},
Hybrid {
track_refs: Vec<TrackRef>,
kb_name: String,
kb_id: String,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Identity {
pub id: IdentityId,
pub canonical_name: String,
pub entity_type: Option<TypeLabel>,
pub kb_id: Option<String>,
pub kb_name: Option<String>,
pub description: Option<String>,
pub embedding: Option<Vec<f32>>,
pub aliases: Vec<String>,
pub confidence: Confidence,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source: Option<IdentitySource>,
}
impl Identity {
#[must_use]
pub fn new(id: impl Into<IdentityId>, canonical_name: impl Into<String>) -> Self {
Self {
id: id.into(),
canonical_name: canonical_name.into(),
entity_type: None,
kb_id: None,
kb_name: None,
description: None,
embedding: None,
aliases: Vec::new(),
confidence: Confidence::ONE,
source: None,
}
}
#[must_use]
pub fn from_kb(
id: impl Into<IdentityId>,
canonical_name: impl Into<String>,
kb_name: impl Into<String>,
kb_id: impl Into<String>,
) -> Self {
let kb_name_str = kb_name.into();
let kb_id_str = kb_id.into();
Self {
id: id.into(),
canonical_name: canonical_name.into(),
entity_type: None,
kb_id: Some(kb_id_str.clone()),
kb_name: Some(kb_name_str.clone()),
description: None,
embedding: None,
aliases: Vec::new(),
confidence: Confidence::ONE,
source: Some(IdentitySource::KnowledgeBase {
kb_name: kb_name_str,
kb_id: kb_id_str,
}),
}
}
pub fn add_alias(&mut self, alias: impl Into<String>) {
self.aliases.push(alias.into());
}
#[must_use]
pub const fn id(&self) -> IdentityId {
self.id
}
#[must_use]
pub fn canonical_name(&self) -> &str {
&self.canonical_name
}
#[must_use]
pub fn kb_id(&self) -> Option<&str> {
self.kb_id.as_deref()
}
#[must_use]
pub fn kb_name(&self) -> Option<&str> {
self.kb_name.as_deref()
}
#[must_use]
pub fn aliases(&self) -> &[String] {
&self.aliases
}
#[must_use]
pub const fn confidence(&self) -> Confidence {
self.confidence
}
pub fn set_confidence(&mut self, confidence: f32) {
self.confidence = Confidence::new(confidence as f64);
}
#[must_use]
pub fn source(&self) -> Option<&IdentitySource> {
self.source.as_ref()
}
#[must_use]
pub fn with_embedding(mut self, embedding: Vec<f32>) -> Self {
self.embedding = Some(embedding);
self
}
#[must_use]
pub fn with_type(mut self, entity_type: impl Into<String>) -> Self {
let s = entity_type.into();
self.entity_type = Some(TypeLabel::from(s.as_str()));
self
}
#[must_use]
pub fn with_type_label(mut self, label: TypeLabel) -> Self {
self.entity_type = Some(label);
self
}
#[must_use]
pub fn type_label(&self) -> Option<TypeLabel> {
self.entity_type.clone()
}
#[must_use]
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
}