cdx_core/extensions/semantic/
entity.rs1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
7#[serde(rename_all = "camelCase")]
8pub struct EntityLink {
9 pub uri: String,
11
12 pub entity_type: EntityType,
14
15 #[serde(default, skip_serializing_if = "Option::is_none")]
17 pub label: Option<String>,
18
19 #[serde(default, skip_serializing_if = "Option::is_none")]
21 pub confidence: Option<f64>,
22
23 #[serde(default, skip_serializing_if = "Option::is_none")]
25 pub source: Option<KnowledgeBase>,
26}
27
28impl EntityLink {
29 #[must_use]
31 pub fn new(uri: impl Into<String>, entity_type: EntityType) -> Self {
32 Self {
33 uri: uri.into(),
34 entity_type,
35 label: None,
36 confidence: None,
37 source: None,
38 }
39 }
40
41 #[must_use]
43 pub fn with_label(mut self, label: impl Into<String>) -> Self {
44 self.label = Some(label.into());
45 self
46 }
47
48 #[must_use]
50 pub fn with_confidence(mut self, confidence: f64) -> Self {
51 self.confidence = Some(confidence.clamp(0.0, 1.0));
52 self
53 }
54
55 #[must_use]
57 pub fn with_source(mut self, source: KnowledgeBase) -> Self {
58 self.source = Some(source);
59 self
60 }
61
62 #[must_use]
64 pub fn wikipedia(title: impl Into<String>, entity_type: EntityType) -> Self {
65 let title = title.into();
66 let uri = format!("https://en.wikipedia.org/wiki/{}", title.replace(' ', "_"));
67 Self::new(uri, entity_type)
68 .with_label(title)
69 .with_source(KnowledgeBase::Wikipedia)
70 }
71
72 #[must_use]
74 pub fn wikidata(qid: impl Into<String>, entity_type: EntityType) -> Self {
75 let qid = qid.into();
76 let uri = format!("https://www.wikidata.org/wiki/{qid}");
77 Self::new(uri, entity_type).with_source(KnowledgeBase::Wikidata)
78 }
79}
80
81#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, strum::Display)]
83#[serde(rename_all = "camelCase")]
84#[strum(serialize_all = "kebab-case")]
85pub enum EntityType {
86 Person,
88 Organization,
90 Place,
92 Event,
94 Product,
96 CreativeWork,
98 Concept,
100 Scientific,
102 TimePeriod,
104 Other,
106}
107
108#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
110#[serde(rename_all = "lowercase")]
111pub enum KnowledgeBase {
112 Wikipedia,
114 Wikidata,
116 Dbpedia,
118 Schema,
120 Loc,
122 Geonames,
124 Other,
126}