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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use serde::ser::{Serialize, Serializer, SerializeSeq};
use lang::Language;
pub fn tokenize(text: &str) -> Vec<String> {
text.split(|c: char| c.is_whitespace() || c == '-')
.filter(|s| s.len() > 0)
.map(|s| s.trim().to_lowercase())
.collect()
}
pub type PipelineFn = fn(String) -> Option<String>;
#[derive(Debug)]
pub struct Pipeline {
pub(crate) queue: Vec<(String, PipelineFn)>,
}
impl Serialize for Pipeline {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut seq = serializer.serialize_seq(Some(self.queue.len()))?;
for &(ref name, _) in &self.queue {
seq.serialize_element(&name)?;
}
seq.end()
}
}
impl Default for Pipeline {
fn default() -> Self {
::lang::en::make_pipeline()
}
}
impl Pipeline {
pub fn for_language(lang: Language) -> Pipeline {
match lang {
Language::Danish => ::lang::da::make_pipeline(),
Language::Dutch => ::lang::du::make_pipeline(),
Language::English => ::lang::en::make_pipeline(),
Language::Finnish => ::lang::fi::make_pipeline(),
Language::French => ::lang::fr::make_pipeline(),
Language::German => ::lang::de::make_pipeline(),
Language::Italian => ::lang::it::make_pipeline(),
Language::Portuguese => ::lang::pt::make_pipeline(),
Language::Romanian => ::lang::ro::make_pipeline(),
Language::Russian => ::lang::ru::make_pipeline(),
Language::Spanish => ::lang::es::make_pipeline(),
Language::Swedish => ::lang::sv::make_pipeline(),
Language::Turkish => ::lang::tr::make_pipeline(),
_ => panic!("Dont use the `__NonExhaustive` variant!"),
}
}
pub fn run(&self, tokens: Vec<String>) -> Vec<String> {
let mut ret = vec![];
for token in tokens {
let mut token = Some(token);
for &(_, func) in &self.queue {
if let Some(t) = token {
token = func(t);
} else {
break;
}
}
if let Some(t) = token {
ret.push(t);
}
}
ret
}
}