utem/english/
mod.rs

1// english/mod.rs
2// UTEM: Universal Text Encoding as Meaning
3// Copyright 2017 (c) Aldaron's Tech
4// Copyright 2017 (c) Jeron Lau
5// Licensed under the MIT LICENSE
6
7mod greetings;
8mod dictionary;
9
10use std::collections::HashMap;
11
12use dictionary::*;
13
14fn is_ender(c: char) -> bool {
15	match c {
16		'.' | '!' | '?' | '‽' => true,
17		_ => false,
18	}
19}
20
21fn unphrase(_/*dict*/: &HashMap<String, Vec<&'static str>>, string: &str) -> String {
22	let out = string.to_string();
23
24	let out = out.replace("'sup", PHRASE_ASK_GOINGS_SLANG);
25	let out = out.replace("what's been going on with you", PHRASE_ASK_GOINGS_FORMAL);
26	let out = out.replace("what's up", PHRASE_ASK_GOINGS_INFORMAL);
27
28	out
29}
30
31fn reformat(dict: &HashMap<String, Vec<&'static str>>, string: &str)
32	-> String
33{
34	let mut out = String::new();
35	let unphrase = unphrase(dict, &string.to_lowercase());
36	let a : Vec<_> = unphrase.split(|x| x == ' ' || x == ',').collect();
37
38	for i in a {
39		if i.is_empty() == false {
40			let i = i.to_lowercase();
41			let i = if let Some(i) = dict.get(&i) {
42				i[0]
43			} else {
44				&i
45//				panic!("no word \"{}\" in the dictionary", i)
46			};
47
48//			println!("i {}", i);
49			out.push_str(&i);
50//			out.push_str(i);
51			out.push(' ');
52		}
53	}
54
55	out
56}
57
58/*pub fn decode(utem_text: &[u8]) -> String {
59	String::new()
60}*/
61
62pub fn encode(utf8_text: &str) -> Vec<u8> {
63	let dict = dictionary::from();
64	let utem = Vec::new();
65//	let mut last = 0;
66	let mut index = 0;
67	let group: Vec<_> = utf8_text.split(is_ender).collect();
68
69	for i in utf8_text.match_indices(is_ender) {
70		let reformatted = reformat(&dict, group[index]);
71
72		match i.1 {
73			"." => println!(". {}", reformatted),
74			"!" => println!("! {}", reformatted),
75			"?" => println!("? {}", reformatted),
76			"‽" => println!("‽ {}", reformatted),
77			_ => unreachable!(),
78		}
79
80//		last = i.0;
81		index += 1;
82	}
83
84	let end = reformat(&dict, group[index]);
85	if end.is_empty() == false {
86		println!("_ {}", end);
87	}
88	println!();
89
90	utem
91}
92
93pub fn translate(ranslat: &str) -> String {
94	// Direct include syntax: %this text is not translated.
95	if ranslat.starts_with("%") {
96		return ranslat[1..].to_string();
97	}
98
99	ranslat.to_string()
100}