1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::ops::Deref;
use crate::error::Error;
use oxilangtag::{LanguageTag, LanguageTagParseError};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Identification<T: Deref<Target = str> + Clone> {
label: LanguageTag<T>,
prob: f32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct IdentificationSer {
label: String,
prob: f32,
}
impl<T> From<Identification<T>> for IdentificationSer
where
T: Deref<Target = str> + Clone,
{
fn from(i: Identification<T>) -> Self {
Self {
label: i.label.to_string(),
prob: i.prob,
}
}
}
impl TryFrom<IdentificationSer> for Identification<String> {
type Error = LanguageTagParseError;
fn try_from(i: IdentificationSer) -> Result<Self, Self::Error> {
Ok(Self {
label: LanguageTag::parse(i.label)?,
prob: i.prob,
})
}
}
impl<T: Deref<Target = str> + Clone> Identification<T> {
pub fn new(label: LanguageTag<T>, prob: f32) -> Self {
Self { label, prob }
}
pub fn label(&self) -> &LanguageTag<T> {
&self.label
}
pub fn prob(&self) -> &f32 {
&self.prob
}
}
pub trait Identifier<T: Deref<Target = str> + Clone> {
fn identify(&self, sentence: T) -> Result<Option<Identification<T>>, Error>;
}
#[cfg(test)]
mod tests {
}