main.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. use std::env;
  2. use std::fs::File;
  3. use std::io::BufRead;
  4. use std::io::BufReader;
  5. use std::io::Error;
  6. use std::iter::Iterator;
  7. use regex::Regex;
  8. use unidecode::unidecode;
  9. #[derive(Default)]
  10. struct Dictionary {
  11. words: Vec<String>,
  12. iter_position: usize,
  13. }
  14. /*impl Dictionary {
  15. fn default() -> Dictionary {
  16. Dictionary{words: vec![], iter_position: 0}
  17. }
  18. }*/
  19. impl Iterator for Dictionary {
  20. type Item = String;
  21. fn next(&mut self) -> Option<Self::Item> {
  22. if self.words.len() >= self.iter_position {
  23. None
  24. } else {
  25. let v = self.words[self.iter_position].clone();
  26. self.iter_position += 1;
  27. Some(v)
  28. }
  29. }
  30. }
  31. fn dictionary_from_iterable(lines: impl Iterator<Item = Result<String, Error>>) -> Dictionary {
  32. let mut w = vec![];
  33. for line in lines {
  34. let line = line.unwrap();
  35. // TODO: normalizza: lascia solo a-z, converti gli accenti, ecc.
  36. w.push(line_to_word(line))
  37. }
  38. return Dictionary {
  39. words: w,
  40. ..Dictionary::default()
  41. };
  42. }
  43. fn line_to_word(l: String) -> String {
  44. let l = unidecode(&l);
  45. let l = l.to_lowercase();
  46. l
  47. }
  48. fn main() {
  49. let args: Vec<String> = env::args().collect();
  50. let fname = &args[1];
  51. let regexp = &args[2];
  52. let f = File::open(fname).unwrap();
  53. let buf = BufReader::new(&f);
  54. let d = dictionary_from_iterable(buf.lines());
  55. let re = Regex::new(regexp).unwrap();
  56. for w in d {
  57. if re.is_match(w.as_str()) {
  58. println!("{}", w)
  59. }
  60. }
  61. }