mirror of
https://github.com/SinTan1729/unscrambler-rust.git
synced 2025-04-19 17:30:02 -05:00
Compare commits
No commits in common. "main" and "1.2" have entirely different histories.
8 changed files with 370117 additions and 95 deletions
|
@ -6,7 +6,4 @@ edition = "2021"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
xz2 = "0.1.7"
|
|
||||||
|
|
||||||
[build-dependencies]
|
|
||||||
xz2 = "0.1.7"
|
xz2 = "0.1.7"
|
6
LICENSE
6
LICENSE
|
@ -631,8 +631,8 @@ to attach them to the start of each source file to most effectively
|
||||||
state the exclusion of warranty; and each file should have at least
|
state the exclusion of warranty; and each file should have at least
|
||||||
the "copyright" line and a pointer to where the full notice is found.
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
Unscrambler-rust: A simple unscrambler program written in Rust.
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
Copyright (C) 2023 Sayantan Santra
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
it under the terms of the GNU General Public License as published by
|
it under the terms of the GNU General Public License as published by
|
||||||
|
@ -652,7 +652,7 @@ Also add information on how to contact you by electronic and paper mail.
|
||||||
If the program does terminal interaction, make it output a short
|
If the program does terminal interaction, make it output a short
|
||||||
notice like this when it starts in an interactive mode:
|
notice like this when it starts in an interactive mode:
|
||||||
|
|
||||||
Unscrambler-rust Copyright (C) 2023 Sayantan Santra
|
<program> Copyright (C) <year> <name of author>
|
||||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
This is free software, and you are welcome to redistribute it
|
This is free software, and you are welcome to redistribute it
|
||||||
under certain conditions; type `show c' for details.
|
under certain conditions; type `show c' for details.
|
||||||
|
|
12
README.md
12
README.md
|
@ -1,15 +1,9 @@
|
||||||
# Unscrambler written in Rust
|
# Unscrambler written in Rust
|
||||||
|
|
||||||
I'm learning Rust, so this is just a rewrite of a simple old project into Rust.
|
I'm learning Rust, so this is just a rewrite of an simple old project in Rust.
|
||||||
|
|
||||||
[Link to old C++ project.](https://github.com/SinTan1729/Unscrambler)
|
[Link to old project.](https://github.com/SinTan1729/Unscrambler)
|
||||||
|
|
||||||
## Usage
|
|
||||||
|
|
||||||
Simply download the `unscrambler` binary from the latest release and run it. The interface is self-explanatory.
|
|
||||||
|
|
||||||
### Note
|
### Note
|
||||||
|
|
||||||
The main `src/wordlist` was pulled from [words_alpha.txt by dwyl](https://github.com/dwyl/english-words/).
|
The main `src/wordlist` was pulled from [words_alpha.txt by dwyl](https://github.com/dwyl/english-words/) and processed using Rust. Processing code was really simple, so didn't put it up here. The processing included pre-sorting the each line in `src/wordlist` to create `src/wordlist_sorted` and then compressing both using `xz`.
|
||||||
|
|
||||||
In order to use a different `wordlist.txt`, place the file inside `src/` and delete the `*.xz` files there. Then run `cargo build` or `cargo build --release`.
|
|
59
build.rs
59
build.rs
|
@ -1,59 +0,0 @@
|
||||||
use std::{fs, io::Read};
|
|
||||||
use xz2::read::XzEncoder;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// check if the compressed dictionary files exist, run if missing
|
|
||||||
// so, in order to rebuild the compressed files, just delete them
|
|
||||||
if !fs::metadata("src/dict/wordlist.txt.xz").is_ok() {
|
|
||||||
compress_wordlist();
|
|
||||||
}
|
|
||||||
if !fs::metadata("src/dict/wordlist_sorted.txt.xz").is_ok() {
|
|
||||||
compress_sorted_wordlist();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn compress_wordlist() {
|
|
||||||
// specify location for dictionary files and read wordlist.txt
|
|
||||||
let dict_dir = "src/dict/";
|
|
||||||
let wordlist = fs::read_to_string([dict_dir, "wordlist.txt"].join(""))
|
|
||||||
.expect("The file wordlist.txt is missing!");
|
|
||||||
// compress wordlist.txt using xz compression and save it
|
|
||||||
let wordlist_bytes = wordlist.as_bytes();
|
|
||||||
let mut compressor = XzEncoder::new(wordlist_bytes, 9);
|
|
||||||
let mut compressed_wordlist = Vec::new();
|
|
||||||
compressor.read_to_end(&mut compressed_wordlist).unwrap();
|
|
||||||
fs::write([dict_dir, "wordlist.txt.xz"].join(""), compressed_wordlist).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn compress_sorted_wordlist() {
|
|
||||||
// specify location for dictionary files
|
|
||||||
let dict_dir = "src/dict/";
|
|
||||||
|
|
||||||
// create wordlist_sorted from wordlist.txt
|
|
||||||
let wordlist = fs::read_to_string([dict_dir, "wordlist.txt"].join(""))
|
|
||||||
.expect("The file wordlist.txt is missing!");
|
|
||||||
let mut wordlist_sorted = String::new();
|
|
||||||
for word in wordlist.split_terminator("\n") {
|
|
||||||
wordlist_sorted = [wordlist_sorted, sorted(word), "\n".to_string()].join("");
|
|
||||||
}
|
|
||||||
|
|
||||||
//compress wordlist_sorted using xz compression and save it
|
|
||||||
let wordlist_sorted_bytes = wordlist_sorted.as_bytes();
|
|
||||||
let mut compressor_sorted = XzEncoder::new(wordlist_sorted_bytes, 9);
|
|
||||||
let mut compressed_wordlist_sorted = Vec::new();
|
|
||||||
compressor_sorted
|
|
||||||
.read_to_end(&mut compressed_wordlist_sorted)
|
|
||||||
.unwrap();
|
|
||||||
fs::write(
|
|
||||||
[dict_dir, "wordlist_sorted.txt.xz"].join(""),
|
|
||||||
compressed_wordlist_sorted,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
// function for sorting
|
|
||||||
fn sorted(word: &str) -> String {
|
|
||||||
let mut word_chars: Vec<char> = word.chars().collect();
|
|
||||||
word_chars.sort_by(|a, b| a.cmp(b));
|
|
||||||
String::from_iter(word_chars)
|
|
||||||
}
|
|
Binary file not shown.
370105
src/dict/wordlist_sorted.txt
Normal file
370105
src/dict/wordlist_sorted.txt
Normal file
File diff suppressed because it is too large
Load diff
Binary file not shown.
27
src/main.rs
27
src/main.rs
|
@ -2,9 +2,6 @@ use std::io::{self, prelude::*, Write};
|
||||||
use xz2::read::XzDecoder;
|
use xz2::read::XzDecoder;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// welcome message
|
|
||||||
println!("*** Welcome to unscrambler! ***");
|
|
||||||
|
|
||||||
// load the compressed dictionary files (embedded in compile-time)
|
// load the compressed dictionary files (embedded in compile-time)
|
||||||
let wordlist_cmp: &[u8] = include_bytes!("dict/wordlist.txt.xz");
|
let wordlist_cmp: &[u8] = include_bytes!("dict/wordlist.txt.xz");
|
||||||
let wordlist_sorted_cmp: &[u8] = include_bytes!("dict/wordlist_sorted.txt.xz");
|
let wordlist_sorted_cmp: &[u8] = include_bytes!("dict/wordlist_sorted.txt.xz");
|
||||||
|
@ -55,29 +52,17 @@ fn main() {
|
||||||
if indices.len() == 0 {
|
if indices.len() == 0 {
|
||||||
println!("No matches found!");
|
println!("No matches found!");
|
||||||
} else {
|
} else {
|
||||||
let mut out_list = Vec::new();
|
println!("The matched words are:");
|
||||||
for index in indices {
|
for index in indices {
|
||||||
out_list.push(sentence_case(&wordlist[index + 1..index - 1 + input.len()]));
|
println!(
|
||||||
}
|
"{}",
|
||||||
if out_list.len() == 1 {
|
sentence_case(&wordlist[index + 1..index - 1 + input.len()])
|
||||||
println!("The only matched word is {}.", out_list[0]);
|
);
|
||||||
} else {
|
|
||||||
print!("The {} matched words are ", out_list.len());
|
|
||||||
out_list.iter().enumerate().for_each(|(pos, word)| {
|
|
||||||
print!("{}", word);
|
|
||||||
if pos < out_list.len() - 2 {
|
|
||||||
print!(", ");
|
|
||||||
} else if pos < out_list.len() - 1 {
|
|
||||||
print!(" and ");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
print!(".\n");
|
|
||||||
io::stdout().flush().unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ask if we want to go again
|
// ask if we want to go again
|
||||||
print!("Would you like to do it again? (y/N): ");
|
print!("Would you like to do it again? (y/N)");
|
||||||
io::stdout().flush().unwrap();
|
io::stdout().flush().unwrap();
|
||||||
let mut response = String::new();
|
let mut response = String::new();
|
||||||
io::stdin().read_line(&mut response).unwrap();
|
io::stdin().read_line(&mut response).unwrap();
|
||||||
|
|
Loading…
Reference in a new issue