Browse Source

updates

master
Andrew Zah 1 year ago
parent
commit
f908b610af
Signed by: andrewzah GPG Key ID: 23A65DACB7CAA68B
  1. 55
      src/main.rs
  2. 27
      src/utils.rs
  3. 2
      tests/words.txt

55
src/main.rs

@ -1,6 +1,6 @@
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::io::{self, BufRead, BufReader};
use std::path::PathBuf;
use std::result::Result;
use std::io::Write;
@ -13,14 +13,13 @@ use tabwriter::TabWriter;
mod errors;
mod models;
mod utils;
use crate::errors::CarajilloError;
use crate::models::{OpenDictRequest, OpenDictResponse};
static API_BASE_URL: &str = "https://opendict.korean.go.kr/api/";
// https://opendict.korean.go.kr/api/search?certkey_no=2727&key=<key>&target_type=search&part=word&q=%EC%88%98%EB%A9%B4&sort=dict&start=1&num=10
static API_BASE_URL: &str = "https://opendict.korean.go.kr/api/";
/// application args
#[derive(FromArgs)]
@ -68,27 +67,53 @@ fn process_word(word: &str, key: &str) -> Result<(), CarajilloError> {
let req_conf = OpenDictRequest::new(key, word);
let url = req_conf.build_url();
let body: String = ureq::post(&url)
.call()?
.into_string()?;
// let body = std::fs::read_to_string("./tests/나무.xml")?;
// let body: String = ureq::post(&url)
// .call()?
// .into_string()?;
let body = std::fs::read_to_string("./tests/나무.xml")?;
let parsed = quick_xml::de::from_str::<OpenDictResponse>(&body)?;
let senses = parsed.senses();
let mut sense_str = format!("Definitions for {}:\n", &word);
for (idx, sense) in senses.iter().enumerate() {
let formatted = format!("{}\t{}\t{:?}\t{}\n", idx,
Red.paint(&sense.origin), &sense.pos, sense.definition);
let pos = sense.pos.clone().unwrap_or(String::new());
let formatted = format!("{}\t{}\t{}\t{}\n", idx,
Red.paint(&sense.origin), Cyan.paint(pos), sense.definition);
sense_str.push_str(&formatted);
}
let mut tw = TabWriter::new(vec![]);
write!(&mut tw, "{}", &sense_str)?;
let mut tw = TabWriter::new(io::stdout());
writeln!(&mut tw, "{}", &sense_str)?;
tw.flush()?;
println!("{}", &sense_str);
let mut parsed_input = vec![];
loop {
let user_input = utils::get_user_input()?;
parsed_input = utils::parse_user_input(user_input);
if parsed_input.len() > 0 {
break;
}
println!("No valid inputs received.");
}
let mut selected_senses = vec![];
for selection in parsed_input {
if let Some(sense) = senses.iter().nth(selection as usize) {
selected_senses.push(sense.clone());
} else {
println!("No definition for {} found. Ignoring!", selection);
}
}
writeln!(&mut tw, "Selected definitions for {}", &word);
for sense in selected_senses {
let formatted_str = format!("{}\t{}", Red.paint(sense.origin),
sense.definition);
writeln!(&mut tw, "{}", &formatted_str)?;
}
tw.flush();
Ok(())
}

27
src/utils.rs

@ -0,0 +1,27 @@
use std::io::{Write, stdin, stdout};
use crate::errors::CarajilloError;
pub fn get_user_input() -> Result<String, CarajilloError> {
let mut out = String::new();
println!("Select which definitions you want. For example: 1 3 7");
let _ = stdout().flush();
stdin().read_line(&mut out)?;
Ok(out.strip_suffix("\n")
.or(out.strip_suffix("\r\n"))
.unwrap_or(&out).to_string())
}
pub fn parse_user_input(s: String) -> Vec<i32> {
let mut ret: Vec<i32> = s
.split(' ')
.into_iter()
.filter_map(|s| s.parse::<i32>().ok())
.collect();
ret.dedup();
ret
}

2
tests/words.txt

@ -1 +1 @@
한국
수면
Loading…
Cancel
Save