First draft of optimisation
This commit is contained in:
parent
6598a9407e
commit
c8100a4257
2 changed files with 128 additions and 113 deletions
|
@ -6,12 +6,15 @@ use std::{
|
|||
|
||||
use kradical_parsing::radk;
|
||||
|
||||
pub fn search_by_radical(query: &mut String, radk_list: &[radk::Membership], stroke_info: &[HashSet<char>]) -> Option<()> {
|
||||
let mut result: HashSet<_> = HashSet::new();
|
||||
let mut aux: HashSet<_> = HashSet::new();
|
||||
|
||||
if !radk_list.is_empty() && !stroke_info.is_empty() {
|
||||
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 aux: HashSet<_> = HashSet::new();
|
||||
unsafe {
|
||||
if !(*radk_list).is_empty() && !stroke_info.is_empty() {
|
||||
result.clear();
|
||||
for i in 0..30 {
|
||||
vec[i].clear();
|
||||
}
|
||||
|
||||
/* First iteration: get the baseline for the results */
|
||||
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)?;
|
||||
}
|
||||
|
||||
for k in radk_list.iter() {
|
||||
for k in (*radk_list).iter() {
|
||||
if k.radical.glyph.contains(rad) {
|
||||
for input in &k.kanji {
|
||||
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)?;
|
||||
}
|
||||
|
||||
for k in radk_list.iter() {
|
||||
for k in (*radk_list).iter() {
|
||||
if k.radical.glyph.contains(rad) {
|
||||
for input in &k.kanji {
|
||||
aux.insert(input);
|
||||
}
|
||||
result = &result & &aux;
|
||||
*result = &*result & &*aux;
|
||||
aux.clear();
|
||||
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 */
|
||||
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 {
|
||||
vec.push(Vec::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());
|
||||
//}
|
||||
|
||||
/*
|
||||
* 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)
|
||||
* 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() {
|
||||
if s.contains(&(r.chars().next().unwrap())) { /* r is a String that has just one character */
|
||||
vec[i].push(r);
|
||||
|
@ -78,21 +81,24 @@ pub fn search_by_radical(query: &mut String, radk_list: &[radk::Membership], str
|
|||
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\
|
||||
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.");
|
||||
} else {
|
||||
eprintln!("File \"/usr/local/share/ykdt/kanji_strokes\" is missing!");
|
||||
}
|
||||
}
|
||||
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 radicals: Vec<char> = Vec::new();
|
||||
let rad;
|
||||
|
||||
unsafe {
|
||||
loop {
|
||||
print!("How many strokes does your radical have? ");
|
||||
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>() {
|
||||
Ok(strk) => {
|
||||
let mut i = 1;
|
||||
for k in radk_list.iter() {
|
||||
for k in (*radk_list).iter() {
|
||||
if k.radical.strokes == strk {
|
||||
print!("{}{} ", i, k.radical.glyph);
|
||||
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> {
|
||||
let file: String;
|
||||
|
|
22
src/main.rs
22
src/main.rs
|
@ -6,7 +6,9 @@ use std::{
|
|||
io::{stdin, stdout, Write, IsTerminal},
|
||||
path::PathBuf,
|
||||
process::{Command, Stdio},
|
||||
cell::RefCell,
|
||||
env,
|
||||
collections::HashSet,
|
||||
};
|
||||
|
||||
use word_search::word_search;
|
||||
|
@ -42,10 +44,18 @@ fn main() -> Result<(), ureq::Error> {
|
|||
};
|
||||
|
||||
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 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();
|
||||
|
||||
|
@ -74,10 +84,9 @@ fn main() -> Result<(), ureq::Error> {
|
|||
|
||||
if query.starts_with(':') || query.starts_with(':') { /* Kanji search */
|
||||
if try_load {
|
||||
radk_list = { match radk::parse_file(&path) {
|
||||
Ok(radk_list) => radk_list,
|
||||
Err(_e) => radk_list,
|
||||
}
|
||||
match radk::parse_file(&path) {
|
||||
Ok(list) => { radk_list.replace(list); },
|
||||
Err(_e) => ()
|
||||
};
|
||||
stroke_info = { match get_stroke_info() {
|
||||
Ok(stroke_info) => stroke_info,
|
||||
|
@ -87,10 +96,9 @@ fn main() -> Result<(), ureq::Error> {
|
|||
try_load = false;
|
||||
}
|
||||
/* 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");
|
||||
}
|
||||
|
||||
} else if query.starts_with('_') || query.starts_with('_') { /* Sentence search */
|
||||
let bytes = query.chars().next().unwrap().len_utf8();
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue