Checkpoint
This commit is contained in:
@@ -70,12 +70,13 @@ pub fn escape_message(event: &MessageCreate) -> String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct NormalizedMessage {
|
pub struct NormalizedMessage {
|
||||||
/// The normalized message content.
|
/// The normalized message content.
|
||||||
message: String,
|
message: String,
|
||||||
|
|
||||||
/// Mappings from original indices to normalized indices, or None if it was removed.
|
/// Mappings from normalized start indices to original start indices (indices are grapheme
|
||||||
mappings: HashMap<(usize, usize), Option<(usize, usize)>>,
|
/// indices).
|
||||||
|
mappings: Vec<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn normalize(message: String) -> NormalizedMessage {
|
pub fn normalize(message: String) -> NormalizedMessage {
|
||||||
@@ -97,9 +98,7 @@ pub fn normalize(message: String) -> NormalizedMessage {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let mut normalized = String::with_capacity(message.len());
|
let mut normalized = String::with_capacity(message.len());
|
||||||
let mut mappings = HashMap::new();
|
let mut mappings = Vec::new();
|
||||||
|
|
||||||
let mut last_i = 0;
|
|
||||||
|
|
||||||
for (i, mut ch) in message
|
for (i, mut ch) in message
|
||||||
.grapheme_indices(true)
|
.grapheme_indices(true)
|
||||||
@@ -111,18 +110,12 @@ pub fn normalize(message: String) -> NormalizedMessage {
|
|||||||
|
|
||||||
let unidecoded = unidecode(&ch);
|
let unidecoded = unidecode(&ch);
|
||||||
|
|
||||||
let ui_start = normalized.len();
|
for char in unidecoded.chars() {
|
||||||
let ui_end = ui_start + unidecoded.len();
|
if char.is_alphanumeric() {
|
||||||
|
normalized.push(char);
|
||||||
if unidecoded.chars().all(|c| c.is_ascii_alphanumeric()) {
|
mappings.push(i);
|
||||||
// If the character is alphanumeric, we can keep it.
|
}
|
||||||
normalized.push_str(&unidecoded);
|
|
||||||
mappings.insert((last_i, i), Some((ui_start, ui_end)));
|
|
||||||
} else {
|
|
||||||
mappings.insert((last_i, i), None);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
last_i = i;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NormalizedMessage {
|
NormalizedMessage {
|
||||||
@@ -170,10 +163,20 @@ pub fn analyze_message(message: &str) -> Vec<(Vec<String>, Arc<dyn Rule>)> {
|
|||||||
results
|
results
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn censor_message(original: &str, normalized: NormalizedMessage, analysis: &[(Vec<String>, Arc<dyn Rule>)]) -> String {
|
pub fn censor_message(
|
||||||
let mut censored = String::with_capacity(original.len());
|
original: &str,
|
||||||
|
normalized: NormalizedMessage,
|
||||||
|
analysis: &[(Vec<String>, Arc<dyn Rule>)],
|
||||||
|
) -> String {
|
||||||
|
let mut censored = original.to_string();
|
||||||
|
|
||||||
todo!();
|
for (findings, rule) in analysis {
|
||||||
|
for finding in findings {
|
||||||
|
while let Some(start) = normalized.message.find(finding) {
|
||||||
|
let end = start + finding.len();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
censored
|
censored
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user