1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! # Balkanoid
//! _The codec that made Russians and Serbs inseparable._
//!
//! ## Historical background
//! It has long been accepted that Serbian is a compact variant of Russian, with less liberal use
//! of vowels. Since the forfeiting of the Riviera in 1991, the loss of tourism revenue has led
//! to further austerity in vowel use. Serbs increasingly needed economically viable ways
//! of communicating, since vowels aren't exactly cheap!
//!
//! # What is it?
//! Balkanoid is a universal transcoder between Serbo-Croatian and Russian languages that is almost
//! entirely isomorphic — it maps from one lingual domain to another with no loss of meaning and
//! some loss of whitespace and capitalisation. Balkanoid works with both English and
//! East-Slavic texts.

pub mod dict;

use crate::codecs::balkanoid::dict::WordResolveError;
use crate::codecs::Codec;
pub use dict::Dict;
use std::borrow::Cow;

/// The Balkanoid codec.
pub struct Balkanoid<'a> {
    dict: &'a Dict,
}

impl<'a> Balkanoid<'a> {
    /// Creates a new Balkanoid codec that works off the given dictionary.
    pub fn new(dict: &'a Dict) -> Self {
        Self { dict }
    }
}

impl Codec for Balkanoid<'_> {
    type ExpandError = WordResolveError;

    fn compress_line(&self, line: &str) -> String {
        let mut buf = String::new();
        // let words = Word::parse_line(line);
        let words = line.split_whitespace();
        // println!("words: {words:?}");
        for (index, word) in words.enumerate() {
            if index > 0 {
                buf.push(' ');
            }
            let compressed_word = compress_word(self.dict, word);
            for _ in 0..compressed_word.leading_spaces {
                buf.push(' ');
            }
            buf.push_str(&compressed_word.body);
        }
        buf
    }

    fn expand_line(&self, line: &str) -> Result<String, Self::ExpandError> {
        let mut buf = String::new();
        let words = EncodedWord::parse_line(line);
        // println!("words: {words:?}");
        for (index, word) in words.into_iter().enumerate() {
            if index > 0 {
                buf.push(' ');
            }
            let expanded_word = expand_word(self.dict, word)?;
            buf.push_str(&expanded_word);
        }
        Ok(buf)
    }
}

#[derive(Debug, PartialEq)]
struct Reduction {
    fingerprint: String,
    leading_capital: bool,
    trailing_capitals: u8,
}

impl Reduction {
    fn new(fingerprint: String, leading_capital: bool, trailing_capitals: u8) -> Self {
        Reduction {
            fingerprint,
            leading_capital,
            trailing_capitals,
        }
    }

    fn is_lowercase(&self) -> bool {
        !self.leading_capital && self.trailing_capitals == 0
    }

    fn take_if_lowercase(self) -> Option<Self> {
        if self.is_lowercase() {
            Some(self)
        } else {
            None
        }
    }
}

impl From<&str> for Reduction {
    fn from(word: &str) -> Self {
        let mut fingerprint = String::new();
        let mut leading_capital = false;
        let mut trailing_capitals = 0;
        for (position, ch) in word.chars().enumerate() {
            if ch.is_uppercase() {
                match position {
                    0 => leading_capital = true,
                    _ => trailing_capitals += 1,
                }

                if !is_vowel(ch) {
                    fingerprint.push(ch.to_lowercase().next().unwrap());
                }
            } else if !is_vowel(ch) {
                fingerprint.push(ch);
            }
        }
        Reduction::new(fingerprint, leading_capital, trailing_capitals)
    }
}

fn is_vowel(ch: char) -> bool {
    matches!(
        ch,
        'a' | 'A'
            | 'e'
            | 'E'
            | 'i'
            | 'I'
            | 'o'
            | 'O'
            | 'u'
            | 'U'
            | 'а'
            | 'А'
            | 'э'
            | 'Э'
            | 'ы'
            | 'Ы'
            | 'у'
            | 'У'
            | 'я'
            | 'Я'
            | 'е'
            | 'Е'
            | 'ё'
            | 'Ё'
            | 'ю'
            | 'Ю'
            | 'и'
            | 'И'
            | 'о'
            | 'О'
    )
}

#[derive(Debug, PartialEq)]
struct EncodedWord {
    leading_spaces: u8,
    body: String,
}

impl EncodedWord {
    fn new(leading_spaces: u8, body: String) -> Self {
        assert!(!body.is_empty());
        EncodedWord {
            leading_spaces,
            body,
        }
    }

    fn parse_line(line: &str) -> Vec<EncodedWord> {
        let mut buf = Some(String::new());
        let mut leading_spaces: u8 = 0;
        let chars = line.chars();
        let mut words = Vec::new();
        for ch in chars {
            if ch == ' ' || ch == '\u{200E}' {
                // we also support the LRM codepoint
                if buf.as_ref().unwrap().is_empty() {
                    leading_spaces += 1;
                } else {
                    words.push(EncodedWord {
                        leading_spaces,
                        body: buf.replace(String::new()).unwrap(),
                    });
                    leading_spaces = 0;
                }
            } else {
                buf.as_mut().unwrap().push(ch);
            }
        }

        if !buf.as_ref().unwrap().is_empty() {
            words.push(EncodedWord {
                leading_spaces,
                body: buf.take().unwrap(),
            });
        }
        words
    }
}

#[derive(Debug, PartialEq)]
struct PunctuatedWord<'a> {
    prefix: Cow<'a, str>,
    suffix: Cow<'a, str>,
}

impl <'a> From<&'a str> for PunctuatedWord<'a> {
    fn from(word: &'a str) -> Self {
        let position = word.chars().enumerate().position(|(position, ch)| {
            // println!("position: {position}, char: {ch}");
            match position {
                0 => !ch.is_alphabetic() && ch != '\\', // allow the escape character to be the first in the string
                _ => !ch.is_alphabetic(), // otherwise, split on non-alphabetic characters
            }
        });
        // println!("got position: {position:?}");
        match position {
            None => PunctuatedWord {
                prefix: Cow::Borrowed(word),
                suffix: Cow::Borrowed(""),
            },
            Some(position) => {
                let prefix = word.chars().take(position).collect::<String>();
                let suffix = word.chars().skip(position).collect::<String>();
                PunctuatedWord {
                    prefix: Cow::Owned(prefix),
                    suffix: Cow::Owned(suffix),
                }
            }
        }
    }
}

#[derive(Debug)]
enum CompactionRule {
    InDict,
    NotInDictWithVowels,
    NoFingerprintInDict,
    Conflict,
    LeadingEscape,
}

fn compress_word(dict: &Dict, word: &str) -> EncodedWord {
    assert!(!word.is_empty());
    let punctuated = PunctuatedWord::from(word);

    let (encoded_prefix, _) = {
        let first_char = punctuated.prefix.chars().next();
        match first_char {
            Some('\\') => {
                // the first character marks the start of an escape sequence
                (
                    (0, format!("\\{}", punctuated.prefix)),
                    CompactionRule::LeadingEscape,
                )
            }
            _ => {
                // println!("punctuated: {punctuated:?}");
                let prefix_reduction = Reduction::from(&punctuated.prefix as &str);
                // println!("prefix reduction {prefix_reduction:?}");
                let lowercase_prefix = punctuated.prefix.to_lowercase();
                match dict.position(&prefix_reduction.fingerprint, &lowercase_prefix) {
                    None => {
                        if punctuated.prefix.len() != prefix_reduction.fingerprint.len() {
                            // the input comprises one or more vowels
                            (
                                (0, punctuated.prefix.into_owned()),
                                CompactionRule::NotInDictWithVowels,
                            )
                        } else if !dict.contains_fingerprint(&prefix_reduction.fingerprint) {
                            // the input comprises only consonants and its fingerprint is not in the dict
                            (
                                (0, punctuated.prefix.into_owned()),
                                CompactionRule::NoFingerprintInDict,
                            )
                        } else {
                            // the input comprises only consonants and there are other words in the
                            // dict with a matching fingerprint
                            (
                                (0, format!("\\{}", punctuated.prefix)),
                                CompactionRule::Conflict,
                            )
                        }
                    }
                    Some(position) => {
                        // the dictionary contains the lower-cased input
                        let recapitalised_prefix = restore_capitalisation(
                            prefix_reduction.fingerprint,
                            prefix_reduction.leading_capital,
                            prefix_reduction.trailing_capitals != 0,
                        );
                        ((position, recapitalised_prefix), CompactionRule::InDict)
                    }
                }
            }
        }
    };
    // println!("rule: {rule:?}");
    EncodedWord::new(encoded_prefix.0, encoded_prefix.1 + &punctuated.suffix)
}

fn restore_capitalisation(
    lowercase_word: String,
    leading_capital: bool,
    nonleading_capital: bool,
) -> String {
    if nonleading_capital {
        lowercase_word.to_uppercase()
    } else if leading_capital {
        let mut chars = lowercase_word.chars();
        chars.next().unwrap().to_uppercase().to_string() + chars.as_str()
    } else {
        lowercase_word
    }
}

const ESCAPE: u8 = b'\\';

fn expand_word(dict: &Dict, word: EncodedWord) -> Result<String, WordResolveError> {
    let punctuated = PunctuatedWord::from(word.body.as_str());
    if punctuated.prefix.is_empty() {
        return Ok(word.body);
    }

    let recapitalised_prefix = if punctuated.prefix.as_bytes()[0] == ESCAPE {
        // word begins with an escape sequence
        String::from(&punctuated.prefix[1..punctuated.prefix.len()])
    } else {
        let mut chars = punctuated.prefix.chars();
        let leading_capital = chars.next().unwrap().is_uppercase();
        let nonleading_capital = chars.next().map_or(false, char::is_uppercase);

        if contains_vowels(&punctuated.prefix) {
            // word encoded with vowels
            punctuated.prefix.into_owned()
        } else {
            let lowercase_word = punctuated.prefix.to_lowercase();
            match dict.resolve(&lowercase_word, word.leading_spaces)? {
                None => {
                    // the fingerprint is not in the dictionary
                    punctuated.prefix.into_owned()
                }
                Some(resolved) => {
                    // resolved a word from the dictionary
                    restore_capitalisation(resolved.clone(), leading_capital, nonleading_capital)
                }
            }
        }
    };

    Ok(recapitalised_prefix + &punctuated.suffix)
}

fn contains_vowels(text: &str) -> bool {
    text.chars().any(is_vowel)
}

#[cfg(test)]
mod tests;