Skip to content

Commit

Permalink
Fixed auto encryption punctuation errors by also matching words with …
Browse files Browse the repository at this point in the history
…punctuation removed against the dictionary
  • Loading branch information
taeh98 committed Sep 25, 2020
1 parent 78c5035 commit 946fdbd
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 35 deletions.
71 changes: 40 additions & 31 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2018"
[dependencies]
rand = "0.7.3"
druid = { git = "https://github.com/linebender/druid.git" }
regex = "1.3.9"

[profile.release]
opt-level = 3
25 changes: 21 additions & 4 deletions src/front_end/state/logic/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::borrow::Borrow;

use rand::Rng;
use regex::Regex;

mod dictionary;

//TODO: fix bug that auto decryption works fine without punctuation but doesn't with (for example) full stops at the end of words.

static ALPHABET_LOWER: [char; 26] = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
static ALPHABET_UPPER: [char; 26] = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
Expand Down Expand Up @@ -89,8 +88,26 @@ fn try_decrypt(shift_value: i8, ciphertext: &String, dictionary_words: &[&'stati
let words: Vec<String> = possible_plaintext.split(" ").map(|s: &str| String::from(s)).collect();
let mut words_in_dict: i8 = 0;

for word in &words {
if dictionary_words.contains(&&**&word.to_lowercase()) { words_in_dict += 1; }
for mixed_case_word in &words {
let word: String = String::from(mixed_case_word.to_lowercase());
if dictionary_words.contains(&&**&word) {
words_in_dict += 1;
continue;
} else {
if word.contains("'") {
let tokens: Vec<&str> = word.split("'").collect();
if dictionary_words.contains(&&*tokens[0]) {
words_in_dict += 1;
continue;
}
}

let re = Regex::new(r"[^a-z^A-Z]").unwrap();
let result = String::from(re.replace_all(&*word, ""));
if dictionary_words.contains(&&*result.to_lowercase()) {
words_in_dict += 1;
}
}
}

let is_passable_text: bool = words_in_dict as f32 / words.len() as f32 >= PASSABLE_PROPORTION_WORDS_IN_DICT;
Expand Down

0 comments on commit 946fdbd

Please sign in to comment.