4 releases (2 breaking)

0.3.0 Nov 16, 2022
0.2.0 Nov 24, 2020
0.1.1 Sep 24, 2019
0.1.0 Sep 24, 2019

#1091 in Algorithms

Download history 477/week @ 2024-05-12 525/week @ 2024-05-19 694/week @ 2024-05-26 549/week @ 2024-06-02 614/week @ 2024-06-09 578/week @ 2024-06-16 639/week @ 2024-06-23 546/week @ 2024-06-30 472/week @ 2024-07-07 474/week @ 2024-07-14 620/week @ 2024-07-21 637/week @ 2024-07-28 638/week @ 2024-08-04 738/week @ 2024-08-11 707/week @ 2024-08-18 517/week @ 2024-08-25

2,650 downloads per month
Used in 3 crates

MIT license

18KB
406 lines

Description

This is a simple profanity filter library for Rust.

For more information, check out the documentation.


lib.rs:

This crate implements a simple but powerful profanity filter.

While this filter can still be technically subverted, the goal is that by the time a profanity gets past the filter, it barely resembles the original word. This is done by subverting common profanity filter workarounds, such as inserting spaces or special characters in between letters (F_U_C_K) or using similar-looking characters in the place of others (SH!T).

Keep in mind though, that this filter is far from perfect. If people really want to swear, they can get through this filter.

Usage

The Censor enum is the main object used for censoring strings. It is essentially a set of words to be filtered out. The Standard variant contains words that most people consider to be swear words, and is meant to be a good baseline for a filter. More sets and individual words can be added with the + and += operators, and sets and words can be removed with the - and -= operators.

use censor::*;

let censor = Censor::Standard;

// Use `Censor::check` to check if a string contains a profanity
assert!(censor.check("fuck"));
assert!(censor.check("FUCK"));
assert!(censor.check("FuCk"));
assert!(censor.check("fμ¢κ"));
assert!(censor.check("f!u!c!k"));
assert!(censor.check("F_u c_K"));
assert!(censor.check("fuuuuuuuck"));

assert!(!censor.check("fluff truck"));
assert!(!censor.check("fukushima"));

// Use `Censor::censor` to censor a string with asterisks
assert_eq!("*_*_*_*_*", censor.censor("₱_û_$_$_¥"));
assert_eq!("**** that ****, dude", censor.censor("fuck that shit, dude"));
assert_eq!("******* yoouuu", censor.censor("fuuuuck yoouuu"));

// Use `Censor::replace` to replace censored words with any grawlix string
assert_eq!("What the !@#$?", censor.replace("What the fuck?", "!@#$%"));

// You can combine `Censor`s and add your own words
let censor = Standard + Zealous + Sex + "dong";

assert_eq!(
"Woops, I dropped my monster ******, that I use for my magnum ****",
censor.censor("Woops, I dropped my monster condom, that I use for my magnum dong")
);

// You can remove words from `Censor`s too
let censor = Standard - "ass";
assert!(!censor.check("I don't care if people say 'ass'"));

// Overlapping censored words are fully censored
let censor = Standard + Sex;
assert_eq!("**********", censor.censor("shititties"));
assert_eq!("*************", censor.censor("blowjoboobies"))

Dependencies

~47KB