forked from pierre/GrammalecteClient
Add simple suggestion
This commit is contained in:
parent
ea7a043d23
commit
8f52bbf121
36
src/lib.rs
36
src/lib.rs
@ -57,6 +57,11 @@ pub struct SpellingError {
|
||||
pub error_type: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct SuggestResult {
|
||||
suggestions: Vec<String>,
|
||||
}
|
||||
|
||||
pub struct GrammalecteClient {
|
||||
base_url: String,
|
||||
}
|
||||
@ -80,7 +85,7 @@ impl GrammalecteClient {
|
||||
/// Run spell check on text
|
||||
pub async fn spell_check(&self, text: &str) -> Result<CheckResult, Box<dyn Error>> {
|
||||
let url = format!("{}/gc_text/fr", self.base_url);
|
||||
log::info!("Will use URL {}", url);
|
||||
log::debug!("Will use URL {} for spell check", url);
|
||||
|
||||
let mut params = HashMap::new();
|
||||
params.insert("text", text);
|
||||
@ -95,6 +100,23 @@ impl GrammalecteClient {
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
/// Ask for word suggestion
|
||||
pub async fn suggest(&self, token: &str) -> Result<SuggestResult, Box<dyn Error>> {
|
||||
let url = format!("{}/suggest/fr", self.base_url);
|
||||
log::debug!("Will use URL {} for word suggestion", url);
|
||||
|
||||
let mut params = HashMap::new();
|
||||
params.insert("token", token);
|
||||
|
||||
Ok(reqwest::Client::new()
|
||||
.post(&url)
|
||||
.form(¶ms)
|
||||
.send()
|
||||
.await?
|
||||
.json()
|
||||
.await?)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -109,4 +131,16 @@ mod test {
|
||||
let res = GrammalecteClient::default().spell_check(msg).await.unwrap();
|
||||
println!("RESULT = {:#?}", res);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn simple_suggestion() {
|
||||
let _ = env_logger::builder().is_test(true).try_init();
|
||||
|
||||
let res = GrammalecteClient::default()
|
||||
.suggest("bonjou")
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(res.suggestions.contains(&"bonjour".to_string()));
|
||||
println!("RESULT = {:#?}", res);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user