Giter Site home page Giter Site logo

roshanrahman / profanity_filter Goto Github PK

View Code? Open in Web Editor NEW
16.0 2.0 12.0 66 KB

Simple Dart class to create filters with methods to check and censor strings against profanity. A default English words list is provided (from LDNOOBW on GitHub).

License: BSD 2-Clause "Simplified" License

Dart 100.00%
profanity dart profanity-filter censor flutter package

profanity_filter's Introduction

profanity_filter

Dart CI

Pub Version

Simple Dart class to create filters with methods to check and censor strings against profanity. A default English words list is provided (from LDNOOBW on GitHub).

You can also use the filters to filter out a custom set of words, by using the ProfanityFilter.filterOnly() constructor.

Usage

To use this plugin, add profanity_filter as a dependency in your pubspec.yaml file.

Note: The latest version of the package is null-safe. The last package version that wasn't null-safe is 1.0.4

Example

import 'package:profanity_filter/profanity_filter.dart';

void main() {
  //Create the filter with the default constructor.
  //The default constructor uses the default list of words provided by [LDNOOBW on GitHub](
  //https://github.com/LDNOOBW/List-of-Dirty-Naughty-Obscene-and-Otherwise-Bad-Words)
  //Other constructors are available, see [README.md](https://pub.dev/packages/profanity_filter).
  final filter = ProfanityFilter();

  //This string contains the profanity 'ass'
  String badString = 'You are an ass';

  //Check for profanity - returns a boolean (true if profanity is present)
  bool hasProfanity = filter.hasProfanity(badString);
  print('The string $badString has profanity: $hasProfanity');

  //Get the profanity used - returns a List<String>
  List<String> wordsFound = filter.getAllProfanity(badString);
  print('The string contains the words: $wordsFound');

  //Censor the string - returns a 'cleaned' string.
  String cleanString = filter.censor(badString);
  print('Censored version of "$badString" is "$cleanString"');
}

Methods

hasProfanity - Detect if a string contains profanity

Use the hasProfanity() method of the filter instance. Pass in the string to be tested.

Example

final filter = ProfanityFilter();
String badString = 'you are an ass';
filter.hasProfanity(badString); //Returns true.

getAllProfanity - Get a list of all profane words found

Use the getAllProfanity() method of the filter instance. Pass in the string to be tested.

Example

final filter = ProfanityFilter();
String badString = 'you are an ass';
List<String> wordsFound = filter.getAllProfanity(badString); //Returns ['ass']

censor - Censor a string (replace profanity with something clean)

Use the censor() method of the filter instance. Pass in the string to be censored.

Example

final filter = ProfanityFilter();
String badString = 'you are an ass';
String cleanString = filter.censor('you are an ass'); //cleanString: 'you are an ***'

Optionally, you can provide your own clean replacement word to the replaceWith named parameter. filter.censor(string, replaceWith:'[censored]') will replace any profanity in string with [censored]

Default Profanity List

The default profanity list is taken from the LDNOOBW list, licensed under a Creative Commons Attribution 4.0 International License.

To create a filter with the default list, use the default constructor ProfanityFilter()

Variations

To include custom words alongside default list

Use ProfanityFilter.filterAdditionally() constructor and pass your list of custom words.

This will use a list which is equivalent to Default List + Custom List

To filter only custom words (default list is ignored)

Use ProfanityFilter.filterOnly() constructor and pass a list containing your custom set of words.

This will use a list which is equivalent to Custom List

To filter with a few exclusions from default list

Use ProfanityFilter.ignore() constructor and pass your list of custom words. Those words will be removed from the default list if they exist there.

This will use a list which is equivalent to Default List - Custom List

profanity_filter's People

Contributors

roshanrahman avatar samlythemanly avatar towner-10 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

profanity_filter's Issues

Non case sensitive

Hi @roshanrahman ,

Could you please make the censor words non case sensitive?
For now, it will censor word f*** but not F***.

Thank you!

Feature: Null safety

Thanks a lot for this awesome library. It would be really helpful if you can make it compatible with Null safety.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.