From 714bccec8d1f4d0759ecadc46c34760bc40f6561 Mon Sep 17 00:00:00 2001 From: Enan Ajmain <3nan.ajmain@gmail.com> Date: Wed, 21 Dec 2022 12:55:10 +0600 Subject: [PATCH] feat: add support for filtering with dictionary --- Cargo.toml | 2 +- src/lib.rs | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 44f74de..94210c8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wordnik" -version = "0.2.4" +version = "0.2.5" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/lib.rs b/src/lib.rs index 0ee0858..432d199 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -130,13 +130,25 @@ impl Wordnik { + &self.api_key; let res = self.make_request(url)?; let mut definitions: Vec = serde_json::from_value(res)?; - definitions.retain(|def| def.definition != "" ); + definitions.retain(|def| def.definition != ""); Ok(definitions) } /// Get definitions of a word. /// - /// # Example + /// # Arguments + /// + /// * `word` - The word whose meaning you want + /// * `dicts` - The dictionaries you want to use. + /// Pass an empty vector if you want to use all. + /// The available dictionaries are: + /// - "ahd-5": American Heritage Dictionary + /// - "century": The Century Dictionary + /// - "gcide": GNU version of the Collaborative International Dictionary of English + /// - "wiktionary": Wiktionary + /// - "wordnet": WordNet 3.0 Copyright 2006 by Princeton University + /// + /// # Examples /// /// ``` /// use wordnik::Wordnik; @@ -146,20 +158,22 @@ impl Wordnik { /// "https://api.wordnik.com/v4/word.json/".to_string(), /// ); /// - /// let v = api.get_definitions_pretty("word").unwrap(); + /// let v = api.get_definitions_pretty("word", vec!["ahd-5", "wordnet"]).unwrap(); /// println!("{}", v); /// ``` pub fn get_definitions_pretty( &self, word: &str, + dicts: Vec<&str>, ) -> Result> { let definitions = self.get_definitions(word)?; let mut hm_definitions: HashMap> = HashMap::new(); + let filter_dict = dicts.len() > 0; for i in &definitions { if hm_definitions.contains_key(&i.attribution_text) { let v = hm_definitions.get_mut(&i.attribution_text).unwrap(); v.push(i.clone()); - } else { + } else if !filter_dict || dicts.contains(&i.source_dictionary.as_str()) { let mut v: Vec = Vec::new(); v.push(i.clone()); hm_definitions.insert(i.attribution_text.clone(), v); -- 2.45.2