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
111impl crate::BatchCapable for ALBERTNER {
112 fn extract_entities_batch(
113 &self,
114 texts: &[&str],
115 language: Option<&str>,
116 ) -> Result<Vec<Vec<Entity>>> {
117 #[cfg(feature = "onnx")]
118 {
119 self.inner.extract_entities_batch(texts, language)
120 }
121 #[cfg(not(feature = "onnx"))]
122 {
123 Err(crate::Error::FeatureNotAvailable(
124 "ALBERT NER requires 'onnx' feature".to_string(),
125 ))
126 }
127 }
128}
129
130impl crate::StreamingCapable for ALBERTNER {
131 fn extract_entities_streaming(&self, chunk: &str, offset: usize) -> Result<Vec<Entity>> {
132 #[cfg(feature = "onnx")]
133 {
134 self.inner.extract_entities_streaming(chunk, offset)
135 }
136 #[cfg(not(feature = "onnx"))]
137 {
138 Err(crate::Error::FeatureNotAvailable(
139 "ALBERT NER requires 'onnx' feature".to_string(),
140 ))
141 }
142 }
143}
144
145#[cfg(test)]
146mod tests {
147 use super::*;
148
149 #[test]
150 fn test_albert_name() {
151 if let Ok(model) = ALBERTNER::new("albert-base-v2") {
152 assert_eq!(model.name(), "albert");
153 }
154 }
156}