aien-translate
aien-dev/aien-translate
Brought by drakestapleton
aien-translate / src / guard.rs
| 1 | //! Checks that a translation kept everything that must not change. |
| 2 | //! |
| 3 | //! If a check fails, the caller keeps the original text instead of showing a |
| 4 | //! translation that may have broken a link, command or name. |
| 5 | |
| 6 | use regex::Regex; |
| 7 | use std::sync::OnceLock; |
| 8 | |
| 9 | fn token_re() -> &'static Regex { |
| 10 | static RE: OnceLock<Regex> = OnceLock::new(); |
| 11 | RE.get_or_init(|| { |
| 12 | Regex::new(concat!( |
| 13 | r"https?://[^\s<>()\[\]]+[^\s<>()\[\].,;:!?]", // URLs |
| 14 | r"|[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", // emails |
| 15 | r"|`[^`\n]+`", // inline code |
| 16 | r"|\{\{[^{}]*\}\}|\{[A-Za-z0-9_]*\}", // {{x}} and {x} placeholders |
| 17 | r"|%[sd]|%\d\$[sd]", // printf placeholders |
| 18 | r"|</?g\d+>|<x\d+/>", // inline-tag markers from scripts/translate_site.py |
| 19 | )) |
| 20 | .unwrap() |
| 21 | }) |
| 22 | } |
| 23 | |
| 24 | /// Tokens in `source` that must appear unchanged in a translation. |
| 25 | pub fn protected(source: &str, keep: &[String]) -> Vec<String> { |
| 26 | let mut out: Vec<String> = token_re().find_iter(source).map(|m| m.as_str().to_string()).collect(); |
| 27 | for term in keep { |
| 28 | if !term.is_empty() && contains_word(source, term) { |
| 29 | out.push(term.clone()); |
| 30 | } |
| 31 | } |
| 32 | out.sort(); |
| 33 | out.dedup(); |
| 34 | out |
| 35 | } |
| 36 | |
| 37 | /// True when `term` occurs in `text` and is not glued to other letters. |
| 38 | fn contains_word(text: &str, term: &str) -> bool { |
| 39 | let mut start = 0; |
| 40 | while let Some(pos) = text[start..].find(term) { |
| 41 | let i = start + pos; |
| 42 | let before = text[..i].chars().next_back(); |
| 43 | let after = text[i + term.len()..].chars().next(); |
| 44 | let edge = |c: Option<char>| match c { None => true, Some(c) => !c.is_alphanumeric() }; |
| 45 | if edge(before) && edge(after) { |
| 46 | return true; |
| 47 | } |
| 48 | start = i + term.len(); |
| 49 | } |
| 50 | false |
| 51 | } |
| 52 | |
| 53 | /// Undo additions the model sometimes makes: code backticks around words the |
| 54 | /// source did not mark as code. |
| 55 | pub fn tidy(source: &str, translated: &str) -> String { |
| 56 | let t = translated.trim(); |
| 57 | if !source.contains('`') && t.contains('`') { |
| 58 | t.replace('`', "") |
| 59 | } else { |
| 60 | t.to_string() |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /// Why a translation was rejected, or None if it passed. |
| 65 | pub fn check(source: &str, translated: &str, keep: &[String]) -> Option<String> { |
| 66 | let t = translated.trim(); |
| 67 | if t.is_empty() { |
| 68 | return Some("empty translation".into()); |
| 69 | } |
| 70 | let limit = source.chars().count() * 4 + 80; |
| 71 | if t.chars().count() > limit { |
| 72 | return Some("translation far longer than the source".into()); |
| 73 | } |
| 74 | for tok in protected(source, keep) { |
| 75 | if !translated.contains(&tok) { |
| 76 | return Some(format!("lost protected text: {tok}")); |
| 77 | } |
| 78 | } |
| 79 | None |
| 80 | } |
| 81 | |
| 82 | /// Text with nothing to translate (numbers, symbols, a lone URL or code span). |
| 83 | pub fn nothing_to_translate(text: &str) -> bool { |
| 84 | let stripped = token_re().replace_all(text, ""); |
| 85 | !stripped.chars().any(|c| c.is_alphabetic()) |
| 86 | } |
| 87 | |
| 88 | #[cfg(test)] |
| 89 | mod tests { |
| 90 | use super::*; |
| 91 | |
| 92 | fn keep() -> Vec<String> { |
| 93 | vec!["AIEN".into(), "Omega".into()] |
| 94 | } |
| 95 | |
| 96 | #[test] |
| 97 | fn finds_urls_code_and_names() { |
| 98 | let p = protected("See https://aienos.com/docs. Run `aien start` with Omega {name}.", &keep()); |
| 99 | assert!(p.contains(&"https://aienos.com/docs".to_string())); |
| 100 | assert!(p.contains(&"`aien start`".to_string())); |
| 101 | assert!(p.contains(&"Omega".to_string())); |
| 102 | assert!(p.contains(&"{name}".to_string())); |
| 103 | } |
| 104 | |
| 105 | #[test] |
| 106 | fn current_system_and_kernel_names_are_kept() { |
| 107 | let keep: Vec<String> = crate::DEFAULT_KEEP.iter().map(|s| s.to_string()).collect(); |
| 108 | let p = protected("AienOS: an agent-first sovereign operating system on the Aienux kernel", &keep); |
| 109 | assert!(p.contains(&"AienOS".to_string())); |
| 110 | assert!(p.contains(&"Aienux".to_string())); |
| 111 | } |
| 112 | |
| 113 | #[test] |
| 114 | fn name_inside_a_word_is_not_protected() { |
| 115 | assert!(protected("AIENOS boots", &keep()).is_empty()); |
| 116 | } |
| 117 | |
| 118 | #[test] |
| 119 | fn rejects_lost_link() { |
| 120 | assert!(check("Visit https://x.io now", "Besuchen Sie jetzt", &keep()).is_some()); |
| 121 | assert!(check("Visit https://x.io now", "Besuchen Sie jetzt https://x.io", &keep()).is_none()); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn rejects_runaway_output() { |
| 126 | let long = "a".repeat(500); |
| 127 | assert!(check("hi", &long, &keep()).is_some()); |
| 128 | } |
| 129 | |
| 130 | #[test] |
| 131 | fn tidy_strips_added_backticks_only() { |
| 132 | assert_eq!(tidy("Run aien start", "Führen Sie `aien start` aus"), "Führen Sie aien start aus"); |
| 133 | assert_eq!(tidy("Run `aien start`", " `aien start` ausführen "), "`aien start` ausführen"); |
| 134 | } |
| 135 | |
| 136 | #[test] |
| 137 | fn inline_tag_markers_must_survive() { |
| 138 | let src = "Read the <g1>docs</g1> and run <x2/>."; |
| 139 | assert!(check(src, "Lies die <g1>Doku</g1> und führe <x2/> aus.", &keep()).is_none()); |
| 140 | assert!(check(src, "Lies die Doku und führe <x2/> aus.", &keep()).is_some()); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn detects_untranslatable() { |
| 145 | assert!(nothing_to_translate("12,345 / 67%")); |
| 146 | assert!(nothing_to_translate("https://aienos.com")); |
| 147 | assert!(!nothing_to_translate("Hello")); |
| 148 | } |
| 149 | } |