From 15ff07c4b3c6a034b7b3c02084b5e9f319e883b2 Mon Sep 17 00:00:00 2001 From: boyska Date: Mon, 18 May 2020 14:57:27 +0200 Subject: [PATCH] parser has its own function --- src/main.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 87d9960..abe73ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,26 @@ use std::env; -use unidecode::unidecode; -use regex::Regex; +use std::io::Error; use std::io::BufReader; use std::io::BufRead; -//use std::io; use std::fs::File; + +use unidecode::unidecode; +use regex::Regex; + struct Dictionary { words: Vec, } +fn dictionary_from_iterable(lines: impl Iterator>) -> 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}; +} + fn line_to_word(l: String) -> String { let l = unidecode(&l); let l = l.to_lowercase(); @@ -20,16 +32,10 @@ fn main() { let fname = &args[1]; let regexp = &args[2]; - let mut w = vec![]; let f = File::open(fname).unwrap(); let buf = BufReader::new(&f); - for line in buf.lines() { - let line = line.unwrap(); - // TODO: normalizza: lascia solo a-z, converti gli accenti, ecc. - w.push(line_to_word(line)) - } + let d = dictionary_from_iterable(buf.lines()); - let d = Dictionary{words: w}; let re = Regex::new(regexp).unwrap(); for w in &d.words { if re.is_match(w) {