Browse Source

updates

master
Andrew Zah 1 year ago
parent
commit
5d8eff6b72
Signed by: andrewzah GPG Key ID: 23A65DACB7CAA68B
  1. 43
      Cargo.lock
  2. 2
      Cargo.toml
  3. 42
      src/main.rs
  4. 65
      src/models.rs
  5. 2
      tests/words.txt
  6. 1
      tests/나무.xml

43
Cargo.lock

@ -1,5 +1,14 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]]
name = "argh"
version = "0.1.4"
@ -45,9 +54,11 @@ checksum = "9c59e7af012c713f529e7a3ee57ce9b31ddd858d4b512923602f74608b009631"
name = "carajillo"
version = "0.1.0"
dependencies = [
"ansi_term",
"argh",
"quick-xml",
"serde",
"tabwriter",
"ureq",
]
@ -181,6 +192,21 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "ring"
version = "0.16.20"
@ -256,6 +282,17 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "tabwriter"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "36205cfc997faadcc4b0b87aaef3fbedafe20d38d4959a7ca6ff803564051111"
dependencies = [
"lazy_static",
"regex",
"unicode-width",
]
[[package]]
name = "tinyvec"
version = "1.2.0"
@ -295,6 +332,12 @@ version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]]
name = "unicode-xid"
version = "0.2.2"

2
Cargo.toml

@ -6,7 +6,9 @@ edition = "2018"
license = "AGPL-3.0-only"
[dependencies]
ansi_term = "0.12"
argh = "0.1"
quick-xml = { version = "0.22", features = [ "serialize" ] }
serde = { version = "1.0", features = [ "derive" ] }
ureq = "2.1"
tabwriter = { version = "1", features = ["ansi_formatting"] }

42
src/main.rs

@ -3,8 +3,13 @@ use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use std::result::Result;
use std::io::Write;
use ansi_term::Colour::{
Cyan, Green, Red, Yellow
};
use argh::{self, FromArgs};
use tabwriter::TabWriter;
mod errors;
mod models;
@ -15,7 +20,7 @@ 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=D2D5C90646A9451DECA5E7AB3E30F727&target_type=search&part=word&q=%EC%88%98%EB%A9%B4&sort=dict&start=1&num=10
// 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
/// application args
#[derive(FromArgs)]
@ -45,10 +50,14 @@ fn main() -> Result<(), CarajilloError> {
.filter_map(Result::ok)
.collect();
let dict_results: Vec<()> = words
.into_iter()
.filter_map(|w| process_word(&w, &kdict_key).ok())
.collect();
for word in words {
process_word(&word, &kdict_key)?;
}
// let dict_results: Vec<()> = words
// .into_iter()
// .filter_map(|w| process_word(&w, &kdict_key).ok())
// .collect();
Ok(())
}
@ -62,19 +71,24 @@ fn process_word(word: &str, key: &str) -> Result<(), CarajilloError> {
let body: String = ureq::post(&url)
.call()?
.into_string()?;
// let body = std::fs::read_to_string("./tests/나무.xml")?;
println!("{}", body.chars().take(750).collect::<String>());
let parsed = quick_xml::de::from_str::<OpenDictResponse>(&body)?;
// println!("{}", &body);
let senses = parsed.senses();
match quick_xml::de::from_str::<OpenDictResponse>(&body) {
Ok(parsed) => {
println!("ok:\n{:?}", parsed);
},
Err(e) => {
eprintln!("err!\n{}", e);
},
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);
sense_str.push_str(&formatted);
}
let mut tw = TabWriter::new(vec![]);
write!(&mut tw, "{}", &sense_str)?;
tw.flush()?;
println!("{}", &sense_str);
Ok(())
}

65
src/models.rs

@ -19,7 +19,7 @@ impl OpenDictRequest {
part: String::from("word"),
query: String::from(word),
start: String::from("1"),
num: String::from("10"),
num: String::from("100"),
}
}
@ -58,50 +58,51 @@ impl OpenDictRequest {
#[derive(Debug, Deserialize)]
pub struct OpenDictResponse {
#[serde(rename = "$value")]
pub channel: OpenDictChannel,
}
#[derive(Debug, Deserialize)]
pub struct OpenDictChannel {
// #[serde(rename = "$value")]
// pub item: Vec<OpenDictItem>,
pub title: String,
pub link: String,
pub total: String,
pub description: String,
pub start: String,
pub link: String,
#[serde(rename = "item")]
pub items: Vec<OpenDictItem>,
// #[serde(rename = "$value")]
pub num: i32,
#[serde(rename = "lastBuildDate")]
pub last_build_date: String,
}
// #[serde(rename = "$value")]
pub start: i32,
impl OpenDictResponse {
pub fn senses(&self) -> Vec<OpenDictSense> {
let mut out = vec![];
// #[serde(rename = "$value")]
pub title: String,
for item in &self.items {
for sense in &item.senses {
out.push(sense.clone())
}
}
// #[serde(rename = "$value")]
pub total: i32,
out
}
}
#[derive(Debug, Deserialize)]
pub struct OpenDictItem {
// #[serde(rename = "sense")]
// pub sense: Vec<OpenDictSense>,
pub word: String,
// pub word: String,
#[serde(rename = "sense")]
pub senses: Vec<OpenDictSense>,
}
// TODO: typ->type
#[derive(Debug, Deserialize)]
#[derive(Clone, Debug, Deserialize)]
pub struct OpenDictSense {
definition: String,
link: Vec<String>,
origin: String,
pos: String,
sense_no: i32,
target_code: String,
typ: String,
pub definition: String,
pub link: Vec<String>,
pub origin: String,
pub pos: Option<String>,
pub sense_no: i32,
pub target_code: String,
#[serde(rename = "type")]
pub typ: String,
}

2
tests/words.txt

@ -1 +1 @@
수면
한국

1
tests/나무.xml

@ -58,7 +58,6 @@

Loading…
Cancel
Save