#filter #word #string #censor

no-std word_filter

A Word Filter for filtering text

17 releases (7 breaking)

0.8.1 Aug 29, 2021
0.7.0 Aug 1, 2021
0.6.0 Jun 23, 2021
0.1.8 Mar 8, 2021

#1237 in Text processing

Download history 201/week @ 2024-03-13 228/week @ 2024-03-20 104/week @ 2024-03-27 38/week @ 2024-04-03 31/week @ 2024-04-10 52/week @ 2024-04-17 16/week @ 2024-04-24 14/week @ 2024-05-01 44/week @ 2024-05-08 22/week @ 2024-05-15 59/week @ 2024-05-22 75/week @ 2024-05-29 50/week @ 2024-06-05 65/week @ 2024-06-12 92/week @ 2024-06-19 57/week @ 2024-06-26

276 downloads per month
Used in word_filter_codegen

MIT/Apache

99KB
1.5K SLoC

word_filter

GitHub Workflow Status codecov.io crates.io docs.rs MSRV License

A Word Filter for filtering text.

A Word Filter is a system for identifying and censoring specific words or phrases in strings. Common usage includes censoring vulgar or profane language and preventing spam or vandelism in user-provided content.

The Word Filter implementation provided here allows for advanced filtering functionality, including:

  • Finding and censoring filtered words.
  • Ignoring words that are considered "exceptions".
  • Allowing specification of "aliases", i.e. strings that can replace other strings (for example, an alias could be created to replace the letter "a" with the character "@").
  • Ignoring specified separators (such as spaces or other characters) between letters of filtered words.

A Word Filter is useful for checking and censoring user-generated text in chat applications, online games, and many other contexts.

Usage

The most common usage for this crate is to generate a WordFilter at compile time using a build script using the codegen module. See the following simple example and the documentation for further details.

Example

For example, a simple WordFilter can be generated by the following.

First, add the word_filter crate to both the Cargo.toml [dependencies] and [build-dependencies] lists.

[dependencies]
word_filter = "0.7.0"

[build-dependencies]
word_filter = "0.7.0"

Next, generate the WordFilter in the build.rs file.

use std::{
    env,
    fs::File,
    io::{BufWriter, Write},
    path::Path,
};
use word_filter::codegen::{Visibility, WordFilterGenerator};

fn main() {
    let path = Path::new(&env::var("OUT_DIR").unwrap()).join("codegen.rs");
    let mut file = BufWriter::new(File::create(&path).unwrap());

    writeln!(
        &mut file,
        "{}",
        WordFilterGenerator::new()
            .visibility(Visibility::Pub)
            .word("foo")
            .generate("FILTER")
        );
}

And finally, include the generated code in the lib.rs file.

include!(concat!(env!("OUT_DIR"), "/codegen.rs"));

assert!(FILTER.censor("Should censor foo."), "Should censor ***.");

Minimum Supported Rust Version

This crate is guaranteed to compile on stable rustc 1.51.0 and up.

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~2MB
~29K SLoC