|
1 | 1 | //! Technical-token layout corrections. |
2 | 2 |
|
3 | 3 | use crate::keyboard::is_cyrillic_letter; |
4 | | -use crate::word_recognizer::is_ascii_technical_token; |
| 4 | +use crate::token_language::is_known_ru_token; |
| 5 | +use crate::word_recognizer::{ |
| 6 | + is_ascii_technical_or_brand_token, is_ascii_technical_token, is_upper_ascii_acronym, |
| 7 | +}; |
5 | 8 |
|
6 | 9 | use super::hyphen::{ |
7 | 10 | has_known_cyrillic_hyphen_fragment, is_cyrillic_hyphenated_word_for_layout, |
@@ -36,6 +39,10 @@ pub fn correct_duplicate_layout_prefix_on_ascii_token(token: &str) -> Option<Str |
36 | 39 | } |
37 | 40 |
|
38 | 41 | pub fn correct_wrong_layout_ascii_technical_token(token: &str) -> Option<String> { |
| 42 | + if let Some(repaired) = correct_ascii_prefix_with_ru_layout_tail(token) { |
| 43 | + return Some(repaired); |
| 44 | + } |
| 45 | + |
39 | 46 | if !token.contains('-') || !is_plain_cyrillic_technical_source(token) { |
40 | 47 | return None; |
41 | 48 | } |
@@ -63,6 +70,33 @@ pub fn correct_wrong_layout_ascii_technical_token(token: &str) -> Option<String> |
63 | 70 | } |
64 | 71 | } |
65 | 72 |
|
| 73 | +fn correct_ascii_prefix_with_ru_layout_tail(token: &str) -> Option<String> { |
| 74 | + let (prefix, tail) = token.split_once('-')?; |
| 75 | + if prefix.is_empty() |
| 76 | + || tail.is_empty() |
| 77 | + || tail.contains('-') |
| 78 | + || !tail.chars().all(|ch| ch.is_ascii_alphabetic()) |
| 79 | + { |
| 80 | + return None; |
| 81 | + } |
| 82 | + if !is_ascii_layout_anchor(prefix) { |
| 83 | + return None; |
| 84 | + } |
| 85 | + |
| 86 | + let converted_tail = crate::dict::convert(tail, crate::dict::Direction::Us2Ru); |
| 87 | + if converted_tail == tail || !is_known_ru_token(&converted_tail) { |
| 88 | + return None; |
| 89 | + } |
| 90 | + |
| 91 | + Some(format!("{prefix}-{converted_tail}")) |
| 92 | +} |
| 93 | + |
| 94 | +fn is_ascii_layout_anchor(prefix: &str) -> bool { |
| 95 | + prefix.is_ascii() |
| 96 | + && prefix.chars().any(|ch| ch.is_ascii_alphabetic()) |
| 97 | + && (is_upper_ascii_acronym(prefix) || is_ascii_technical_or_brand_token(prefix)) |
| 98 | +} |
| 99 | + |
66 | 100 | pub fn should_keep_plain_cyrillic_before_ascii_technical(original: &str, converted: &str) -> bool { |
67 | 101 | original.chars().count() >= 4 |
68 | 102 | && original.chars().all(is_cyrillic_letter) |
|
0 commit comments