Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ltengine/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ use static_files::resource_dir;

fn main() -> std::io::Result<()> {
resource_dir("./resources").build()
}
}
10 changes: 6 additions & 4 deletions ltengine/src/banner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub fn print_banner(){
println!(r#"
pub fn print_banner() {
println!(
r#"
__________________________________________
__ ____________ _
/ / /_ __/ ____/___ ____ _(_)___ ___
Expand All @@ -10,5 +11,6 @@ pub fn print_banner(){

Local AI Machine Translation
___________________________________________
"#);
}
"#
);
}
17 changes: 9 additions & 8 deletions ltengine/src/error_response.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use actix_web::http::StatusCode;
use actix_web::{HttpResponse, ResponseError, body::BoxBody};
use serde::Serialize;
use std::fmt::{Display, Formatter, Result as FmtResult};
use serde_json::to_string_pretty;
use actix_web::{ResponseError, HttpResponse, body::BoxBody};
use actix_web::http::StatusCode;
use std::fmt::{Display, Formatter, Result as FmtResult};

#[derive(Debug, Serialize)]
pub struct ErrorResponse {
pub error: String,
pub status: u16
pub error: String,
pub status: u16,
}

impl Display for ErrorResponse {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
write!(f, "{}", to_string_pretty(self).unwrap())
write!(f, "{}", to_string_pretty(self).unwrap())
}
}

Expand All @@ -22,7 +22,8 @@ impl ResponseError for ErrorResponse {
}

fn error_response(&self) -> HttpResponse<BoxBody> {
HttpResponse::build(self.status_code()).json(serde_json::json!({"error": self.error.clone()}))
HttpResponse::build(self.status_code())
.json(serde_json::json!({"error": self.error.clone()}))
}
}

Expand All @@ -33,4 +34,4 @@ impl From<actix_web::Error> for ErrorResponse {
status: err.as_response_error().status_code().as_u16(),
}
}
}
}
46 changes: 27 additions & 19 deletions ltengine/src/languages.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::Serialize;
use once_cell::sync::Lazy;
use whatlang::{Lang, Detector};
use serde::Serialize;
use std::collections::HashMap;
use whatlang::{Detector, Lang};

const LANGS: &[(&str, &str, &str)] = &[
("en", "", "English"),
Expand Down Expand Up @@ -65,24 +65,22 @@ pub struct Language {
pub lang_detect: Option<&'static Lang>,

#[serde(skip)]
pub internal_code: &'static str
pub internal_code: &'static str,
}


pub static LANGUAGES: Lazy<Vec<Language>> = Lazy::new(|| {

// From whatlang names to our names
let eng_name_map: HashMap<&'static str, &'static str> = [
("Mandarin", "Chinese")
].iter().cloned().collect();

let eng_name_map: HashMap<&'static str, &'static str> =
[("Mandarin", "Chinese")].iter().cloned().collect();

let mut lang_detect_map: HashMap<&'static str, &'static Lang> = HashMap::new();
for lang in Lang::all() {
let eng_name = lang.eng_name();
lang_detect_map.insert(eng_name_map.get(eng_name).unwrap_or(&eng_name), lang);
}

LANGS.iter()
LANGS
.iter()
.map(|&(code, alias, name)| {
let targets: Vec<&str> = LANGS
.iter()
Expand All @@ -94,29 +92,35 @@ pub static LANGUAGES: Lazy<Vec<Language>> = Lazy::new(|| {
name,
targets: Box::leak(targets.into_boxed_slice()),
lang_detect: lang_detect_map.get(name).map(|v| &**v),
internal_code: code
internal_code: code,
}
})
.collect()
});

static LANGUAGES_MAP: Lazy<HashMap<&'static str, &'static Language>> = Lazy::new(|| {
LANGUAGES.iter().map(|lang| (lang.internal_code, lang)).collect()
LANGUAGES
.iter()
.map(|lang| (lang.internal_code, lang))
.collect()
});

static CODE_TO_INTERNAL_CODE_MAP: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
LANGUAGES.iter().map(|lang| (lang.code, lang.internal_code)).collect()
LANGUAGES
.iter()
.map(|lang| (lang.code, lang.internal_code))
.collect()
});

pub fn get_language_from_code(code: &String) -> Option<&'static Language>{
pub fn get_language_from_code(code: &String) -> Option<&'static Language> {
let code_str = code.as_str();
let internal_code = CODE_TO_INTERNAL_CODE_MAP.get(code_str).unwrap_or(&code_str);
LANGUAGES_MAP.get(internal_code).map(|v| &**v)
}

pub struct LangDetect{
pub struct LangDetect {
pub language: &'static Language,
pub confidence: i32
pub confidence: i32,
}

pub fn detect_lang(q: &String) -> LangDetect {
Expand All @@ -130,9 +134,13 @@ pub fn detect_lang(q: &String) -> LangDetect {
let lang = info.lang();
let confidence = info.confidence();

LANGUAGES.iter()
LANGUAGES
.iter()
.find(|l| l.lang_detect == Some(&lang))
.map(|l| LangDetect { language: l, confidence: (confidence * 100.0) as i32 })
.map(|l| LangDetect {
language: l,
confidence: (confidence * 100.0) as i32,
})
.unwrap_or(LangDetect {
language: &LANGUAGES[0],
confidence: 0,
Expand All @@ -143,4 +151,4 @@ pub fn detect_lang(q: &String) -> LangDetect {
confidence: 0,
}
}
}
}
Loading
Loading