12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- use std::env;
- use std::fs::File;
- use std::io::BufRead;
- use std::io::BufReader;
- use std::io::Error;
- use std::iter::Iterator;
- use regex::Regex;
- use unidecode::unidecode;
- #[derive(Default)]
- struct Dictionary {
- words: Vec<String>,
- iter_position: usize,
- }
- /*impl Dictionary {
- fn default() -> Dictionary {
- Dictionary{words: vec![], iter_position: 0}
- }
- }*/
- impl Iterator for Dictionary {
- type Item = String;
- fn next(&mut self) -> Option<Self::Item> {
- if self.words.len() >= self.iter_position {
- None
- } else {
- let v = self.words[self.iter_position].clone();
- self.iter_position += 1;
- Some(v)
- }
- }
- }
- fn dictionary_from_iterable(lines: impl Iterator<Item = Result<String, Error>>) -> Dictionary {
- let mut w = vec![];
- for line in lines {
- let line = line.unwrap();
- // TODO: normalizza: lascia solo a-z, converti gli accenti, ecc.
- w.push(line_to_word(line))
- }
- return Dictionary {
- words: w,
- ..Dictionary::default()
- };
- }
- fn line_to_word(l: String) -> String {
- let l = unidecode(&l);
- let l = l.to_lowercase();
- l
- }
- fn main() {
- let args: Vec<String> = env::args().collect();
- let fname = &args[1];
- let regexp = &args[2];
- let f = File::open(fname).unwrap();
- let buf = BufReader::new(&f);
- let d = dictionary_from_iterable(buf.lines());
- let re = Regex::new(regexp).unwrap();
- for w in d {
- if re.is_match(w.as_str()) {
- println!("{}", w)
- }
- }
- }
|