vortaroboto

Log | Files | Refs | README

commit 168ad0e9e8d3363d6e2d4fb56d8171e139049802
parent 1829542de9da809708d5f3464d4ef77a56c6a8c6
Author: thomas.vigouroux@univ-grenoble-alpes.fr <thomas.vigouroux@univ-grenoble-alpes.fr>
Date:   Thu, 12 Jan 2023 07:18:55 +0000

aldoni traduk helpilo
Diffstat:
MCargo.lock | 10++++++----
MCargo.toml | 2++
Msrc/main.rs | 15+++++++++++++++
Asrc/tradukhelpu.rs | 33+++++++++++++++++++++++++++++++++
4 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -1110,9 +1110,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.5.4" +version = "1.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" dependencies = [ "aho-corasick", "memchr", @@ -1121,9 +1121,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.6.25" +version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" [[package]] name = "remove_dir_all" @@ -1676,7 +1676,9 @@ dependencies = [ "console-subscriber", "futures", "irc", + "lazy_static", "log", + "regex", "reqwest", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml @@ -20,3 +20,5 @@ cached = "0.26.2" chrono = { version = "0.4.19", features = ["serde"] } async-trait = "0.1.15" console-subscriber = "0.1.0" +regex = "1.7.1" +lazy_static = "1.4.0" diff --git a/src/main.rs b/src/main.rs @@ -21,11 +21,17 @@ use pack::{pack_data, unpack_data}; mod cmds; use cmds::stats::{add_words, stats}; +mod tradukhelpu; + const FONTLIGILO: &'static str = "https://git.vigoux.giize.com/vortaroboto/"; const NICK: &'static str = "vortaroboto"; const LINEO_SEP: &'static str = "\r\n"; const DORM_TEMPO: u64 = 500; +lazy_static::lazy_static!( + static ref TRAD_REG: regex::Regex = regex::Regex::new("\"((?P<lang>[a-z]{3}):)?(?P<txt>[^\"]+)\"").unwrap(); +); + enum NumerSelektilo { Cxiuj, Numero(usize), @@ -140,6 +146,15 @@ async fn main() -> irc::error::Result<()> { } }; sender.send_privmsg(target, msg)?; + } else { + for m in TRAD_REG.captures_iter(msg.as_str()) { + let srclang = m.name("lang").map(|m| m.as_str()).unwrap_or("eng"); + let txt = m.name("txt").unwrap().as_str(); + + if let Ok(trad) = tradukhelpu::trad(txt, srclang).await { + sender.send_privmsg(target, format!("{}: por diri \"{}\" en esperanto vi povus diri \"{}\"", message.source_nickname().unwrap(), txt, trad)); + } + } } } _ => (), diff --git a/src/tradukhelpu.rs b/src/tradukhelpu.rs @@ -0,0 +1,33 @@ +use futures::prelude::*; +use serde::{Deserialize, de::DeserializeOwned}; + +#[derive(Deserialize)] +#[serde(rename_all="camelCase")] +struct Response { + response_data: Traduk, + response_status: u16, + response_details: Option<String> +} + +#[derive(Deserialize)] +#[serde(rename_all="camelCase")] +struct Traduk { + translated_text: String +} + +pub async fn trad(content: &str, srclang: &str) -> Result<String, String> { + let client = reqwest::Client::new(); + + if srclang.len() != 3 { + return Err("Invalid lang".into()); + } + + let resp: Response = client.post("https://beta.apertium.org/apy/translate") + .query(&[ + ("langpair", format!("{}|epo", srclang)), + ("format", "txt".into()), + ("q", content.into()) + ]).send().and_then(|r| r.json()).map_err(|e| e.to_string()).await?; + + Ok(resp.response_data.translated_text) +}