1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#[macro_use] extern crate cached; extern crate fxhash; use crate::fill::{parallel::ParallelFiller, Filler}; use fxhash::FxHashMap; use trie::Trie; use crate::crossword::Crossword; use crate::{crossword::Direction, ngram::bigrams}; use std::{fs::File, sync::Arc, time::Instant}; pub mod crossword; pub mod fill; mod ngram; mod order; mod parse; pub mod trie; pub fn fill_crossword(contents: String, words: Vec<String>) -> Result<Crossword, String> { let crossword = Crossword::new(contents).unwrap(); let (bigrams, trie) = index_words(words); let filler = ParallelFiller::new(Arc::new(trie), Arc::new(bigrams)); filler.fill(&crossword) } pub fn default_indexes() -> (FxHashMap<(char, char), usize>, Trie) { let now = Instant::now(); let file = File::open("./trie.bincode").unwrap(); let load = bincode::deserialize_from::<File, Trie>(file); let trie = load.unwrap(); println!("Loaded trie in {}ms", now.elapsed().as_millis()); let now = Instant::now(); let file = File::open("./bigrams.bincode").unwrap(); let load = bincode::deserialize_from::<File, FxHashMap<(char, char), usize>>(file); let bigrams = load.unwrap(); println!("Loaded bigrams in {}ms", now.elapsed().as_millis()); (bigrams, trie) } pub fn default_words() -> Vec<String> { let file = File::open("wordlist.json").unwrap(); serde_json::from_reader(file).expect("JSON was not well-formatted") } pub fn index_words(raw_data: Vec<String>) -> (FxHashMap<(char, char), usize>, Trie) { let bigram = bigrams(&raw_data); let trie = Trie::build(raw_data); (bigram, trie) } #[cfg(test)] mod tests { use crate::FxHashMap; use std::time::Instant; use crate::{default_words, index_words, trie::Trie, File}; #[test] #[ignore] fn rebuild_serialized_indexes() { let (bigrams, trie) = index_words(default_words()); let trie_file = File::create("trie.bincode").unwrap(); let trie_result = bincode::serialize_into(trie_file, &trie); assert!(trie_result.is_ok()); let bigrams_file = File::create("bigrams.bincode").unwrap(); let bigrams_result = bincode::serialize_into(bigrams_file, &bigrams); assert!(bigrams_result.is_ok()); } #[test] fn test_trie_load() { let now = Instant::now(); let file = File::open("./trie.bincode").unwrap(); let load = bincode::deserialize_from::<File, Trie>(file); assert!(load.is_ok()); println!("Loaded trie in {}", now.elapsed().as_millis()); } #[test] fn test_bigrams_load() { let file = File::open("bigrams.bincode").unwrap(); let load = bincode::deserialize_from::<File, FxHashMap<(char, char), usize>>(file); assert!(load.is_ok()); } }