First draft of optimisation

This commit is contained in:
Hiers 2025-01-27 17:18:33 +00:00
parent 6598a9407e
commit c8100a4257
2 changed files with 128 additions and 113 deletions

View file

@ -6,12 +6,15 @@ use std::{
use kradical_parsing::radk; use kradical_parsing::radk;
pub fn search_by_radical(query: &mut String, radk_list: &[radk::Membership], stroke_info: &[HashSet<char>]) -> Option<()> { pub fn search_by_radical<'a>(query: &mut String, radk_list: *const Vec<radk::Membership>, stroke_info: &[HashSet<char>], result: &mut HashSet<&'a String>, aux: &mut HashSet<&'a String>, vec: &mut Vec<Vec<&'a String>>) -> Option<()> {
let mut result: HashSet<_> = HashSet::new(); //let mut result: HashSet<_> = HashSet::new();
let mut aux: HashSet<_> = HashSet::new(); //let mut aux: HashSet<_> = HashSet::new();
unsafe {
if !radk_list.is_empty() && !stroke_info.is_empty() { if !(*radk_list).is_empty() && !stroke_info.is_empty() {
result.clear(); result.clear();
for i in 0..30 {
vec[i].clear();
}
/* First iteration: get the baseline for the results */ /* First iteration: get the baseline for the results */
let mut rad = query.chars().nth(1).unwrap(); let mut rad = query.chars().nth(1).unwrap();
@ -20,7 +23,7 @@ pub fn search_by_radical(query: &mut String, radk_list: &[radk::Membership], str
rad = search_by_strokes(query, radk_list, 1)?; rad = search_by_strokes(query, radk_list, 1)?;
} }
for k in radk_list.iter() { for k in (*radk_list).iter() {
if k.radical.glyph.contains(rad) { if k.radical.glyph.contains(rad) {
for input in &k.kanji { for input in &k.kanji {
result.insert(input); result.insert(input);
@ -36,12 +39,12 @@ pub fn search_by_radical(query: &mut String, radk_list: &[radk::Membership], str
rad = search_by_strokes(query, radk_list, i+2)?; rad = search_by_strokes(query, radk_list, i+2)?;
} }
for k in radk_list.iter() { for k in (*radk_list).iter() {
if k.radical.glyph.contains(rad) { if k.radical.glyph.contains(rad) {
for input in &k.kanji { for input in &k.kanji {
aux.insert(input); aux.insert(input);
} }
result = &result & &aux; *result = &*result & &*aux;
aux.clear(); aux.clear();
break; break;
} }
@ -49,17 +52,17 @@ pub fn search_by_radical(query: &mut String, radk_list: &[radk::Membership], str
} }
/* Hash sets are unordered; Will now order the results by number of strokes */ /* Hash sets are unordered; Will now order the results by number of strokes */
let mut vec: Vec<Vec<&String>> = Vec::with_capacity(30); /* The kanji we care about will have at most 30 strokes */ //let mut vec: Vec<Vec<&String>> = Vec::with_capacity(30); /* The kanji we care about will have at most 30 strokes */
for _i in 0..29 { //for _i in 0..30 {
vec.push(Vec::new()); // vec.push(Vec::new());
} //}
/* /*
* A vector of vectors is useful here to store kanji by number of strokes * A vector of vectors is useful here to store kanji by number of strokes
* First vector's index will indicate the number of strokes (minus 1 because it starts at 0) * First vector's index will indicate the number of strokes (minus 1 because it starts at 0)
* Second vector will hold all of the kanji that is written in that number of strokes * Second vector will hold all of the kanji that is written in that number of strokes
*/ */
for r in &result { for r in result.iter() {
for (i, s) in stroke_info.iter().enumerate() { for (i, s) in stroke_info.iter().enumerate() {
if s.contains(&(r.chars().next().unwrap())) { /* r is a String that has just one character */ if s.contains(&(r.chars().next().unwrap())) { /* r is a String that has just one character */
vec[i].push(r); vec[i].push(r);
@ -78,21 +81,24 @@ pub fn search_by_radical(query: &mut String, radk_list: &[radk::Membership], str
println!(); println!();
} }
} }
} else if radk_list.is_empty() { } else if (*radk_list).is_empty() {
eprintln!("Error while reading radkfile\nIf you don't have the radkfile, download it from\n\ eprintln!("Error while reading radkfile\nIf you don't have the radkfile, download it from\n\
https://www.edrdg.org/krad/kradinf.html and place it in \"~/.local/share/\" on Linux or \"~\\AppData\\Local\\\" on Windows.\n\ https://www.edrdg.org/krad/kradinf.html and place it in \"~/.local/share/\" on Linux or \"~\\AppData\\Local\\\" on Windows.\n\
This file is needed to search radicals by strokes."); This file is needed to search radicals by strokes.");
} else { } else {
eprintln!("File \"/usr/local/share/ykdt/kanji_strokes\" is missing!"); eprintln!("File \"/usr/local/share/ykdt/kanji_strokes\" is missing!");
} }
}
Some(()) Some(())
} }
fn search_by_strokes(query: &mut String, radk_list: &[radk::Membership], n: usize) -> Option<char> { fn search_by_strokes(query: &mut String, radk_list: *const Vec<radk::Membership>, n: usize) -> Option<char> {
let mut strokes = String::new(); let mut strokes = String::new();
let mut radicals: Vec<char> = Vec::new(); let mut radicals: Vec<char> = Vec::new();
let rad; let rad;
unsafe {
loop { loop {
print!("How many strokes does your radical have? "); print!("How many strokes does your radical have? ");
stdout().flush().ok()?; stdout().flush().ok()?;
@ -104,7 +110,7 @@ fn search_by_strokes(query: &mut String, radk_list: &[radk::Membership], n: usiz
match strokes.trim().parse::<u8>() { match strokes.trim().parse::<u8>() {
Ok(strk) => { Ok(strk) => {
let mut i = 1; let mut i = 1;
for k in radk_list.iter() { for k in (*radk_list).iter() {
if k.radical.strokes == strk { if k.radical.strokes == strk {
print!("{}{} ", i, k.radical.glyph); print!("{}{} ", i, k.radical.glyph);
radicals.push(k.radical.glyph.chars().next()?); radicals.push(k.radical.glyph.chars().next()?);
@ -146,6 +152,7 @@ fn search_by_strokes(query: &mut String, radk_list: &[radk::Membership], n: usiz
} }
} }
} }
}
pub fn get_stroke_info() -> Result<Vec<HashSet<char>>, std::io::Error> { pub fn get_stroke_info() -> Result<Vec<HashSet<char>>, std::io::Error> {
let file: String; let file: String;

View file

@ -6,7 +6,9 @@ use std::{
io::{stdin, stdout, Write, IsTerminal}, io::{stdin, stdout, Write, IsTerminal},
path::PathBuf, path::PathBuf,
process::{Command, Stdio}, process::{Command, Stdio},
cell::RefCell,
env, env,
collections::HashSet,
}; };
use word_search::word_search; use word_search::word_search;
@ -42,10 +44,18 @@ fn main() -> Result<(), ureq::Error> {
}; };
let path = get_radkfile_path().unwrap(); let path = get_radkfile_path().unwrap();
let mut radk_list = Vec::new(); //let mut radk_list = Vec::new();
let radk_list = RefCell::new(Vec::new());
let mut stroke_info = Vec::new(); let mut stroke_info = Vec::new();
let mut try_load = true; let mut try_load = true;
let mut result: HashSet<&String> = HashSet::new();
let mut aux: HashSet<&String> = HashSet::new();
let mut vec: Vec<Vec<&String>> = Vec::with_capacity(30); /* The kanji we care about will have at most 30 strokes */
for _i in 0..30 {
vec.push(Vec::new());
}
let options = parse_args(); let options = parse_args();
@ -74,10 +84,9 @@ fn main() -> Result<(), ureq::Error> {
if query.starts_with(':') || query.starts_with('') { /* Kanji search */ if query.starts_with(':') || query.starts_with('') { /* Kanji search */
if try_load { if try_load {
radk_list = { match radk::parse_file(&path) { match radk::parse_file(&path) {
Ok(radk_list) => radk_list, Ok(list) => { radk_list.replace(list); },
Err(_e) => radk_list, Err(_e) => ()
}
}; };
stroke_info = { match get_stroke_info() { stroke_info = { match get_stroke_info() {
Ok(stroke_info) => stroke_info, Ok(stroke_info) => stroke_info,
@ -87,10 +96,9 @@ fn main() -> Result<(), ureq::Error> {
try_load = false; try_load = false;
} }
/* if search_by_radical failed, then something is very wrong */ /* if search_by_radical failed, then something is very wrong */
if search_by_radical(&mut query, &radk_list, &stroke_info).is_none() { if search_by_radical(&mut query, radk_list.as_ptr(), &stroke_info, &mut result, &mut aux, &mut vec).is_none() {
eprintln!("Couldn't parse input"); eprintln!("Couldn't parse input");
} }
} else if query.starts_with('_') || query.starts_with('_') { /* Sentence search */ } else if query.starts_with('_') || query.starts_with('_') { /* Sentence search */
let bytes = query.chars().next().unwrap().len_utf8(); let bytes = query.chars().next().unwrap().len_utf8();