1use std::borrow::Cow;
2
3use std::io;
4
5use io::BufWriter;
6use io::Write;
7
8use io::BufRead;
9
10use rust_stemmers::Algorithm;
11use rust_stemmers::Stemmer;
12
13pub fn str2algorithm(s: &str) -> Option<Algorithm> {
14 match s.to_lowercase().as_str() {
15 "arabic" => Some(Algorithm::Arabic),
16 "danish" => Some(Algorithm::Danish),
17 "dutch" => Some(Algorithm::Dutch),
18 "english" => Some(Algorithm::English),
19 "finnish" => Some(Algorithm::Finnish),
20 "french" => Some(Algorithm::French),
21 "german" => Some(Algorithm::German),
22 "greek" => Some(Algorithm::Greek),
23 "hungarian" => Some(Algorithm::Hungarian),
24 "italian" => Some(Algorithm::Italian),
25 "norwegian" => Some(Algorithm::Norwegian),
26 "portuguese" => Some(Algorithm::Portuguese),
27 "romanian" => Some(Algorithm::Romanian),
28 "russian" => Some(Algorithm::Russian),
29 "spanish" => Some(Algorithm::Spanish),
30 "swedish" => Some(Algorithm::Swedish),
31 "tamil" => Some(Algorithm::Tamil),
32 "turkish" => Some(Algorithm::Turkish),
33 _ => None,
34 }
35}
36
37pub struct Normalizer {
38 pub stemmer: Stemmer,
39}
40
41impl Normalizer {
42 pub fn normalize<'a>(&self, original: &'a str) -> Cow<'a, str> {
43 self.stemmer.stem(original)
44 }
45
46 pub fn write_normalized<I, W>(&self, original: I, mut writer: W) -> Result<(), io::Error>
47 where
48 I: Iterator<Item = Result<String, io::Error>>,
49 W: Write,
50 {
51 for rline in original {
52 let line: String = rline?;
53 let normalized = self.normalize(&line);
54 writeln!(&mut writer, "{normalized}")?;
55 }
56 writer.flush()
57 }
58
59 pub fn stdin2lines2normalized2stdout(&self) -> Result<(), io::Error> {
60 let il = io::stdin().lock();
61 let lines = il.lines();
62 let o = io::stdout();
63 let mut ol = o.lock();
64 self.write_normalized(lines, BufWriter::new(&mut ol))?;
65 ol.flush()
66 }
67}
68
69#[cfg(test)]
70mod tests {
71 use super::*;
72
73 #[test]
74 fn test_str2algorithm() {
75 assert_eq!(str2algorithm("english"), Some(Algorithm::English));
76 assert_eq!(str2algorithm("English"), Some(Algorithm::English));
77 assert_eq!(str2algorithm("french"), Some(Algorithm::French));
78 assert_eq!(str2algorithm("invalid"), None);
79 }
80}