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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
//! Translate German to (almost) perfect Meddlfrängisch.
//!
//! # Usage
//!
//! ```rust
//! fn main() {
//!     println!("{}", meddl_translate::translate("Hallo"));
//! }
//! ```
//!
//! # Examples
//!
//! ```shell
//! $ cargo run --example hello
//! ```
//! ```shell
//! $ cargo run --example long-text
//! ```
//!
//! # Excluding words from being translated
//!
//! ```json
//! "ignored": [
//!     "den"
//! ]
//! ```
//!
//! Example containing an ignored word:
//!
//! ```shell
//! $ cargo run --example ignored
//! ```
//!
//! # Benchmark
//!
//! ```shell
//! $ cargo bench
//! ```
//!
//! You need to use Rust nightly for running the benchmark.

mod util;

use serde_json::Value;
use regex::{Regex};
#[cfg(feature = "interlude")]
use util::{is_ignored_word, get_random_index, is_one_percent_chance, capitalize_word};
#[cfg(not(feature = "interlude"))]
use util::{is_ignored_word, get_random_index, capitalize_word};

fn parse_translation() -> Option<Value> {
    let translation_string = include_str!("de-oger.json");
    Some(serde_json::from_str(&translation_string).expect("Could not parse translation."))
}

/// This function translates a string slice from German to Meddlfrängisch.
///
/// # Example
///
/// ```
/// fn main() {
///     let meddl_fraengisch = meddl_translate::translate("Hallo Welt");
/// }
/// ```
pub fn translate(original: &str) -> String {
    if original.len() == 0 {
        return String::from("");
    }

    let words: Vec<&str> = original.split(" ").collect();
    let translation: Value = parse_translation().unwrap();
    let punctuation_regex = Regex::new(r"[.,\\/#!?$%\^&\*;:{}=\-_`~()]").expect("Could not compile punctuation regex.");
    let mut meddl = String::new();

    for i in 0..words.len() {
        let punctuation = punctuation_regex
            .find(words[i])
            .map(|punc| punc.as_str())
            .unwrap_or("");
        let cow = punctuation_regex.replace_all(words[i], "");
        let mut word_no_punctuation = String::new();
        word_no_punctuation.push_str(&cow);

        let translated_punctuation;
        // edge case where input is e.g. "you & me".
        // & gets replaced with "", crashing the translate_word function
        // as "" can't be accessed by [0].
        if word_no_punctuation.len() == 0 {
            word_no_punctuation.push_str(words[i]);
            translated_punctuation = String::from("");
        } else {
            translated_punctuation = translate_punctuation(&punctuation, &translation);
        }


        #[cfg(feature = "interlude")]
        let mut translated_word = translate_word(&word_no_punctuation, &translation);
        #[cfg(not(feature = "interlude"))]
        let translated_word = translate_word(&word_no_punctuation, &translation);

        #[cfg(feature = "interlude")]
        if is_one_percent_chance() {
            translated_word = add_interlude(&translated_word, &translation);
        }

        meddl.push_str(&translated_word);
        meddl.push_str(&translated_punctuation);
        meddl.push_str(" ");
    }

    String::from(meddl.trim())
}

fn translate_word<'a>(word: &'a str, translation: &'a Value) -> String {
    let is_noun = word
        .chars()
        .collect::<Vec<char>>()[0]
        .is_uppercase();

    let mut word = translate_quotation_marks(&word, translation);

    if is_ignored_word(&word, &translation) {
        return word;
    }

    if let Some(_key) = translation["translations"].get(&word) {
        let possible_translations = translation["translations"][&word]
            .as_array()
            .unwrap();
        let random = get_random_index(&possible_translations);

        let translated_word = possible_translations[random]
            .as_str()
            .unwrap_or(&word);
        word = String::from(translated_word);
    } else {
        word = twist_en(&word, &translation);
        
    }

    word = word.to_lowercase();

    word = translate_beginning(&word, &translation);
    word = twist_chars(&word, &translation);

    if is_noun {
        return capitalize_word(&word);
    }

    word
}

fn twist_chars<'a>(word: &'a str, translation: &'a Value) -> String {
    let twisted_chars = translation["twistedChars"]
        .as_object()
        .unwrap();
    let mut word = String::from(word);

    for pair in twisted_chars.iter() {
        if word.contains(pair.0) {
            word = word
                .replace(pair.0, pair.1
                    .as_str()
                    .unwrap()
                    .to_lowercase()
                    .as_str(),
                );
        }
    }

    String::from(word)
}

fn twist_en<'a>(word: &'a str, translation: &'a Value) -> String {
    let mut twisted = String::from(word);

    let ens = translation["en"]
        .as_object()
        .unwrap();

    for array in ens.iter() {
        let to_replace = array.0;
        if word.ends_with(to_replace) {
            let position = word.rfind(to_replace).unwrap();

            twisted
                .replace_range(position..word.len(), array.1
                    .as_str()
                    .unwrap()
                )
        }
    }

    twisted
}

fn translate_punctuation<'a>(punctuation: &'a str, translation: &'a Value) -> String {
    return match punctuation {
        "." => {
            let dot_pool = translation["dot"]
                .as_array()
                .unwrap();
            let random = get_random_index(dot_pool);
            let translated_dot = dot_pool[random]
                .as_str()
                .unwrap();

            String::from(translated_dot)
        }
        "!" => {
            let exclamation_mark_pool = translation["exclamationMark"]
                .as_array()
                .unwrap();
            let random = get_random_index(exclamation_mark_pool);
            let translated_exclamation_mark = exclamation_mark_pool[random]
                .as_str()
                .unwrap();

            String::from(translated_exclamation_mark)
        }
        "?" => {
            let question_mark_pool = translation["questionMark"]
                .as_array()
                .unwrap();
            let random = get_random_index(question_mark_pool);
            let translated_question_mark = question_mark_pool[random]
                .as_str()
                .unwrap();

            String::from(translated_question_mark)
        }
        _ => String::from(punctuation)
    };
}

fn translate_quotation_marks(word: &str, translation: &Value) -> String {
    if word.starts_with("\"") {
        return word.replacen("\"", translation["quotationMark"]
            .as_str()
            .unwrap(),
        1);
    }
    String::from(word)
}

fn translate_beginning(word: &str, translation: &Value) -> String {
    let beginnings = translation["twistBeginning"].as_object().unwrap();

    for beginning_object in beginnings.iter() {
        if word.starts_with(beginning_object.0) {
            return word.replacen(beginning_object.0, beginning_object.1.as_str().unwrap(), 1)
        }
    }

    String::from(word)
}

#[cfg(feature = "interlude")]
fn add_interlude(word_to_add_to: &str, translation: &Value) -> String {
    let interlude = translation["interlude"]
        .as_str()
        .unwrap();

        let word_with_interlude = format!(
            "{}{}",
            word_to_add_to,
            interlude
        );

        word_with_interlude
}

#[cfg(test)]
mod tests {
    mod translate {
        use crate::translate;

        #[test]
        fn should_translate_everything() {
            assert_eq!(translate("Der Meddltranslator wurde in Rust programmiert"), "Der Meddldranslador wurde in Rusd brogrammierd");
        }

        #[test]
        fn should_return_empty_string_on_empty_string_on_input() {
            assert_eq!(translate(""), "");
        }
    }

    mod translate_word {
        use super::super::*;
        #[test]
        fn should_ignore_word() {
            let translation = serde_json::from_str("{\"ignored\": [\"whatever\"], \"translations\": { \"whatever\": [\"something\"]}}").unwrap();

            assert_eq!(translate_word("whatever", &translation), "whatever");
        }

        #[test]
        fn should_translate_word() {
            let translation = serde_json::from_str("{\"translations\": { \"Whatever\": [\"Something\"]}, \"ignored\": [], \"en\": {}, \"twistedChars\": {}, \"twistBeginning\": {}}").unwrap();

            assert_eq!(translate_word("Whatever", &translation), "Something");
        }

        #[test]
        fn should_translate_nn_correctly() {
            let translation = serde_json::from_str("{\"translations\": { \"wenn\": [\"wen\"]}, \"ignored\": [], \"en\": {}, \"twistedChars\": {}, \"twistBeginning\": {}}").unwrap();

            assert_eq!(translate_word("wenn", &translation), "wen");
        }
    }

    mod twist_chars {
        use serde_json::Value;
        use crate::twist_chars;

        #[test]
        fn should_twist_chars() {
            let translation = serde_json::from_str("{\"twistedChars\": {\"ck\": \"gg\"}}").unwrap();

            assert_eq!(twist_chars("wicked", &translation), "wigged");
        }

        #[test]
        fn should_twist_multiple_chars() {
            let translation: Value = serde_json::from_str("{\"twistedChars\": {\"z\": \"ds\", \"p\": \"b\"}}").unwrap();

            assert_eq!(twist_chars("pommespanzer", &translation), "bommesbandser");
        }
    }

    mod twist_en {
        use crate::twist_en;

        #[test]
        fn should_twist_en_end_of_word() {
            let translation = serde_json::from_str("{\"en\": {\"en!\": \"ne!\"}, \"ignored\": []}").unwrap();

            assert_eq!(twist_en("laufen!", &translation), "laufne!");
        }

        #[test]
        fn should_twist_en_ignore_char_within() {
            let translation = serde_json::from_str("{\"en\": {\"en\": \"ne\"}, \"ignored\": []}").unwrap();

            assert_eq!(twist_en("denken", &translation), "denkne");
        }
    }

    mod translate_punctuation {
        use crate::translate_punctuation;

        #[test]
        fn should_translate_punctuation_dot() {
            let translation = serde_json::from_str("{\"dot\": [\" dot suffix.\"]}").unwrap();

            assert_eq!(translate_punctuation(".", &translation), " dot suffix.");
        }

        #[test]
        fn should_translation_punctuation_exclamation_mark() {
            let translation = serde_json::from_str("{\"exclamationMark\": [\" exclamation mark suffix!\"]}").unwrap();

            assert_eq!(translate_punctuation("!", &translation), " exclamation mark suffix!");
        }

        #[test]
        fn should_translate_punctuation_question_mark() {
            let translation = serde_json::from_str("{\"questionMark\": [\" question mark suffix?\"]}").unwrap();

            assert_eq!(translate_punctuation("?", &translation), " question mark suffix?");
        }

        #[test]
        fn should_translate_punctuation_return_anything_else() {
            let translation = serde_json::from_str("{}").unwrap();

            assert_eq!(translate_punctuation("~", &translation), "~");
        }
    }

    mod translate_quotation_marks {
        use crate::translate_quotation_marks;

        #[test]
        fn should_translate_quotation_marks() {
            let translation = serde_json::from_str("{\"quotationMark\":\"I cite: \\\"\"}").unwrap();

            assert_eq!(translate_quotation_marks("\"word\"", &translation), "I cite: \"word\"");
        }

    }

    mod translate_beginning {
        use crate::translate_beginning;

        #[test]
        fn should_translate_st() {
            let translation = serde_json::from_str("{\"twistBeginning\": {\"st\": \"schd\"}}").unwrap();

            assert_eq!(translate_beginning("stein", &translation), "schdein");
        }

        #[test]
        fn should_translate_sp() {
            let translation = serde_json::from_str("{\"twistBeginning\": {\"sp\": \"schb\"}}").unwrap();

            assert_eq!(translate_beginning("spinne", &translation), "schbinne");
        }

        #[test]
        fn should_ignore_anything_else() {
            let translation = serde_json::from_str("{\"twistBeginning\": {\"sp\": \"schb\"}}").unwrap();

            assert_eq!(translate_beginning("hallo", &translation), "hallo");
        }
    }

    mod edge_cases {
        use crate::translate;

        #[test]
        fn should_work_with_single_punctuation() {
            assert_eq!(translate("."), ".");
            assert_eq!(translate("you & me"), "you & me");
        }
    }
}