2018-10-03 16:21:33 +02:00
|
|
|
use std::io::{self, BufReader, BufRead};
|
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::fs::File;
|
|
|
|
|
2018-10-09 18:23:35 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CommonWords(HashSet<String>);
|
2018-10-03 16:21:33 +02:00
|
|
|
|
2018-10-09 18:23:35 +02:00
|
|
|
impl CommonWords {
|
|
|
|
pub fn from_file<P>(path: P) -> io::Result<Self>
|
|
|
|
where P: AsRef<Path>
|
|
|
|
{
|
|
|
|
let file = File::open(path)?;
|
|
|
|
let file = BufReader::new(file);
|
|
|
|
let mut set = HashSet::new();
|
|
|
|
for line in file.lines().filter_map(|l| l.ok()) {
|
|
|
|
let word = line.trim().to_owned();
|
|
|
|
set.insert(word);
|
2018-10-03 16:21:33 +02:00
|
|
|
}
|
2018-10-09 18:23:35 +02:00
|
|
|
Ok(CommonWords(set))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contains(&self, word: &str) -> bool {
|
|
|
|
self.0.contains(word)
|
2018-10-03 16:21:33 +02:00
|
|
|
}
|
|
|
|
}
|