diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9896aa3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "regexle" +version = "0.1.0" +edition = "2021" + +[dependencies] +csv = "1.3.0" +fancy-regex = "0.13.0" +rayon = "1.10.0" +serde = "1.0.209" +serde_derive = "1.0.209" +wasm_crossword_generator = "0.0.4" +rand = "0.8.5" + +[[bin]] +name = "checker" +path = "src/checker/bin/main.rs" + +[[bin]] +name = "generator" +path = "src/generator/bin/main.rs" + diff --git a/checker/Cargo.toml b/checker/Cargo.toml deleted file mode 100644 index 5eb1c0f..0000000 --- a/checker/Cargo.toml +++ /dev/null @@ -1,11 +0,0 @@ -[package] -name = "regex-checker" -version = "0.1.0" -edition = "2021" - -[dependencies] -csv = "1.3.0" -fancy-regex = "0.13.0" -rayon = "1.10.0" -serde = "1.0.209" -serde_derive = "1.0.209" diff --git a/checker/processed_answers.csv b/processed_answers.csv similarity index 100% rename from checker/processed_answers.csv rename to processed_answers.csv diff --git a/checker/regex.csv b/regex.csv similarity index 100% rename from checker/regex.csv rename to regex.csv diff --git a/checker/src/main.rs b/src/checker/bin/main.rs similarity index 89% rename from checker/src/main.rs rename to src/checker/bin/main.rs index 2d1fabc..3183a13 100644 --- a/checker/src/main.rs +++ b/src/checker/bin/main.rs @@ -3,11 +3,8 @@ use std::error::Error; use fancy_regex::Regex; use rayon::iter::{IntoParallelIterator, IntoParallelRefIterator}; use rayon::iter::ParallelIterator; -use crate::utils::{read_csv, read_lines, write_to_csv}; -use crate::word::{Answer, Word}; - -mod word; -mod utils; +use regexle::utils::{read_csv, read_lines, write_to_csv}; +use regexle::word::{Answer, Word}; fn map_to_answers(regex: String, words: &Vec) -> Vec { @@ -41,7 +38,7 @@ fn main() -> Result<(), Box> { let regexes = read_lines(regex_chart)?; let word_freq_chart = "./word_freq.csv"; - let mut words = read_csv(word_freq_chart)?; + let mut words: Vec = read_csv(word_freq_chart)?; words.truncate(10000); let processed_words: Vec = words.into_par_iter().filter(|word| word.word.len() > 2).collect(); diff --git a/src/generator/bin/main.rs b/src/generator/bin/main.rs new file mode 100644 index 0000000..c70331f --- /dev/null +++ b/src/generator/bin/main.rs @@ -0,0 +1,51 @@ +use std::error::Error; +use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; +use wasm_crossword_generator::*; +use regexle::utils::read_csv; +use regexle::word::Answer; + + +fn print_puzzle(puzz: PerWordPuzzle) { + for word in puzz.puzzle.solution.words.iter() { + println!("{:?}", word); + } + puzz.puzzle.grid.iter().for_each(|row| { + row.row.iter().for_each(|cell| + print!("{:?}", cell) + ); + println!(); + }); +} +fn main() -> Result<(), Box> { + let records: Vec = read_csv("processed_answers.csv")?; + + let words: Vec = records.par_iter().map(|ans| Word { text: ans.answer.clone(), clue: Option::from(ans.question.clone()) }).collect(); + // A real SolutionConf would probably want "requirements" to allow for retrying crossword + // generation. Because there is only one word, we know we'll get the world's simplest puzzle in + // one try. + let solution_conf = SolutionConf { + words, + max_words: 20, + width: 10, + height: 10, + requirements: None, + initial_placement: None, + }; + + // A PerWordPuzzle is one where the player only guesses words, not locations. The player is + // immediately informed if the guess is correct or not. + let mut puzzle = PerWordPuzzle::new(solution_conf)?; + // println!("{:?}", puzzle.puzzle.grid); + print_puzzle(puzzle); + // let guess = PlacedWord { + // // Because it is a PerWordPuzzle, the placement is ignored, unlike other Playmodes. + // placement: Placement { x: 0, y: 0, direction: rand::random() }, + // // NOTE: you don't need to match the "clue" field, it is ignored for purposes of PartialEq + // word: Word { text: "library".to_string(), clue: None }, + // }; + // let guess_result = puzzle.guess_word(guess)?; + // + // // Because there is only one word, the guess will result in "Complete" instead of "Correct" + // assert_eq!(guess_result, GuessResult::Complete); + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..aafec8c --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,3 @@ + +pub mod utils; +pub mod word; diff --git a/checker/src/utils.rs b/src/utils.rs similarity index 76% rename from checker/src/utils.rs rename to src/utils.rs index 1e2dadd..80bdabb 100644 --- a/checker/src/utils.rs +++ b/src/utils.rs @@ -3,15 +3,19 @@ use std::fs::File; use std::io::{self, BufRead}; use std::path::Path; use csv::{QuoteStyle, ReaderBuilder, WriterBuilder}; -use crate::word::{Answer, Word}; +use serde::de::DeserializeOwned; +use serde::{Serialize}; -pub fn read_csv(file_path: &str) -> Result, Box> { +pub fn read_csv(file_path: &str) -> Result, Box> +where + T: DeserializeOwned, +{ let file = File::open(file_path)?; let mut rdr = ReaderBuilder::new().has_headers(true).from_reader(file); let mut records = Vec::new(); for result in rdr.deserialize() { - let record: Word = result?; + let record= result?; records.push(record); } @@ -19,7 +23,9 @@ pub fn read_csv(file_path: &str) -> Result, Box> { } -pub fn write_to_csv(records: Vec, path: &str) -> Result<(), Box> { +pub fn write_to_csv(records: Vec, path: &str) -> Result<(), Box> +where + T: Serialize,{ // Create a new CSV writer with the file path let file = File::create(path)?; let mut wtr = WriterBuilder::new() diff --git a/checker/src/word.rs b/src/word.rs similarity index 51% rename from checker/src/word.rs rename to src/word.rs index 87135af..7715afb 100644 --- a/checker/src/word.rs +++ b/src/word.rs @@ -2,13 +2,13 @@ use serde_derive::{Deserialize, Serialize}; #[derive(Debug, Deserialize)] pub struct Word { - pub(crate) word: String, - pub(crate) count: i64, + pub word: String, + pub count: i64, } #[derive(Debug, Deserialize, Serialize)] pub struct Answer { - pub(crate) question: String, - pub(crate) answer: String, - pub(crate) count: i64, + pub question: String, + pub answer: String, + pub count: i64, } \ No newline at end of file diff --git a/checker/word_freq.csv b/word_freq.csv similarity index 100% rename from checker/word_freq.csv rename to word_freq.csv