1use crate::{Entity, EntityType, Model, Result};
29
30#[cfg(feature = "onnx")]
31use crate::backends::onnx::BertNEROnnx;
32
33pub struct ALBERTNER {
38 #[cfg(feature = "onnx")]
39 inner: BertNEROnnx,
40 #[allow(dead_code)] model_name: String,
43}
44
45impl ALBERTNER {
46 pub fn new(model_name: &str) -> Result<Self> {
51 #[cfg(feature = "onnx")]
52 {
53 let inner = BertNEROnnx::new(model_name)?;
55 Ok(Self {
56 inner,
57 model_name: model_name.to_string(),
58 })
59 }
60 #[cfg(not(feature = "onnx"))]
61 {
62 Err(crate::Error::FeatureNotAvailable(
63 "ALBERT NER requires 'onnx' feature".to_string(),
64 ))
65 }
66 }
67}
68
69impl Model for ALBERTNER {
70 fn extract_entities(&self, text: &str, language: Option<&str>) -> Result<Vec<Entity>> {
71 #[cfg(feature = "onnx")]
72 {
73 self.inner.extract_entities(text, language)
74 }
75 #[cfg(not(feature = "onnx"))]
76 {
77 Err(crate::Error::FeatureNotAvailable(
78 "ALBERT NER requires 'onnx' feature".to_string(),
79 ))
80 }
81 }
82
83 fn supported_types(&self) -> Vec<EntityType> {
84 vec![
85 EntityType::Person,
86 EntityType::Organization,
87 EntityType::Location,
88 ]
89 }
90
91 fn is_available(&self) -> bool {
92 #[cfg(feature = "onnx")]
93 {
94 self.inner.is_available()
95 }
96 #[cfg(not(feature = "onnx"))]
97 {
98 false
99 }
100 }
101
102 fn name(&self) -> &'static str {
103 "albert"
104 }
105
106 fn description(&self) -> &'static str {
107 "ALBERT NER - efficient, small model (11MB) with competitive performance"
108 }
109
110 fn capabilities(&self) -> crate::ModelCapabilities {
111 crate::ModelCapabilities {
112 batch_capable: true,
113 streaming_capable: true,
114 ..Default::default()
115 }
116 }
117}
118
119impl crate::NamedEntityCapable for ALBERTNER {}
120
121impl crate::BatchCapable for ALBERTNER {
122 fn extract_entities_batch(
123 &self,
124 texts: &[&str],
125 language: Option<&str>,
126 ) -> Result<Vec<Vec<Entity>>> {
127 #[cfg(feature = "onnx")]
128 {
129 self.inner.extract_entities_batch(texts, language)
130 }
131 #[cfg(not(feature = "onnx"))]
132 {
133 Err(crate::Error::FeatureNotAvailable(
134 "ALBERT NER requires 'onnx' feature".to_string(),
135 ))
136 }
137 }
138}
139
140impl crate::StreamingCapable for ALBERTNER {
141 fn extract_entities_streaming(&self, chunk: &str, offset: usize) -> Result<Vec<Entity>> {
142 #[cfg(feature = "onnx")]
143 {
144 self.inner.extract_entities_streaming(chunk, offset)
145 }
146 #[cfg(not(feature = "onnx"))]
147 {
148 Err(crate::Error::FeatureNotAvailable(
149 "ALBERT NER requires 'onnx' feature".to_string(),
150 ))
151 }
152 }
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158
159 #[test]
160 fn test_albert_name() {
161 if let Ok(model) = ALBERTNER::new("albert-base-v2") {
162 assert_eq!(model.name(), "albert");
163 }
164 }
166}