new small crate to get rid of smart quotes, etc

This commit is contained in:
Brandon Thomas committed 2022-02-05 22:40:04 -05:00
1 parent aa59e05d5b
commit 75461a49df
5 files changed
+79

No files matched your search

Generated
+12
View File
@@ -4357,6 +4357,18 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]]
name = "tts_common"
version = "0.0.1"
dependencies = [
"anyhow",
"container_common",
"log",
"once_cell",
"regex",
"unicode-segmentation",
]
[[package]]
name = "tungstenite"
version = "0.14.0"
+1
View File
@@ -7,6 +7,7 @@ members = [
"crates/lib/database_queries",
"crates/lib/http_server_common",
"crates/lib/redis_common",
"crates/lib/tts_common",
"crates/lib/twitch_common",
# Application
+26
View File
@@ -0,0 +1,26 @@
[package]
name = "tts_common"
edition = "2018"
version = "0.0.1"
authors = [
"Brandon Thomas <bt@brand.io>",
"Brandon Thomas <echelon@gmail.com>",
]
publish = false
[lib]
name = "tts_common"
path = "src/lib.rs"
[dependencies]
# Internal
container_common = { path = "../container_common" }
# External
anyhow = "1.0.40"
log = "0.4.14"
once_cell = "1.9.0"
regex = "1.5.4"
unicode-segmentation = "1.7.1"
@@ -0,0 +1,39 @@
use once_cell::sync::Lazy;
use std::collections::HashMap;
use log::info;
use unicode_segmentation::UnicodeSegmentation;
static REPLACEMENTS : Lazy<HashMap<String, String>> = Lazy::new(|| {
let mut map = HashMap::new();
map.insert("’".to_string(), "'".to_string());
map.insert("‘".to_string(), "'".to_string());
map.insert("“".to_string(), "\"".to_string());
map.insert("”".to_string(), "\"".to_string());
map
});
pub fn clean_symbols(input_text: &str) -> String {
let segmented= UnicodeSegmentation::graphemes(input_text, true)
.map(|segment| {
if let Some(replace) = REPLACEMENTS.get(segment) {
return replace.as_str();
}
return segment;
})
.collect::<Vec<&str>>();
segmented.join("")
}
#[cfg(test)]
mod tests {
use crate::clean_symbols::clean_symbols;
#[test]
fn filters_smart_quotes() {
assert_eq!(clean_symbols("That’s ok"), "That's ok".to_string());
assert_eq!(clean_symbols("That’s it’s new home"), "That's it's new home".to_string());
assert_eq!(clean_symbols("‘foo’"), "'foo'".to_string());
assert_eq!(clean_symbols("“as ‘shown’ here.”"), "\"as 'shown' here.\"".to_string());
}
}
+1
View File
@@ -0,0 +1 @@
pub mod clean_symbols;