Browse Source

parser has its own function

boyska 4 years ago
parent
commit
15ff07c4b3
1 changed files with 16 additions and 10 deletions
  1. 16 10
      src/main.rs

+ 16 - 10
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<String>,
 }
 
+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};
+}
+
 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) {