moderation

package module
v0.11.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2021 License: Unlicense Imports: 6 Imported by: 6

README

moderation

godocs

moderation is a profanity filter for Go.

Goals

  1. Easy to use
  2. Minimum possible allocations, processing time, and binary size
  3. Minimum false negatives (including text like h3110_w0r!d)
  4. Minimum false positives
  5. (Experimental) Provide a way to censor text
  6. (Future) Other analysis types than inappropriate, profane, offensive, sexual, mean, spam (violence, contact info, etc.)
  7. (Future) Basic support for languages other than English

Example

package main

import (
	"fmt"
	"github.com/finnbear/moderation"
)

func main() {
	printResult("hello world")
	printResult("$#1t")
	printResult("a$$")
	printResult("assassin")
}

func printResult(phrase string) {
	description := "is appropriate"
	if moderation.IsInappropriate(phrase) {
		description = "is NOT appropriate"
	}
	fmt.Printf("\"%s\" %s.\n", phrase, description)
}

$ go run hello_world.go
"hello world" is appropriate.
"$#1t" is NOT appropriate.
"a$$" is NOT appropriate.
"assassin" is appropriate.

Comparison

Accuracy was evaluated based on the first 100,000 items from this dataset of moderated comments.

Package Time Accuracy Comment
finnbear/moderation 1.63s 92.21% Current API version is not stable
TwinProduction/go-away 2.16s 82.18% Many false positives from combined words like "push it"

Acknowledgements

  1. Radix implementation based on https://gitlab.com/caibear/go-boggle/
  2. Some profanities and test cases are based on https://github.com/TwinProduction/go-away

Documentation

Overview

The package moderation implements a profanity filter.

Index

Examples

Constants

View Source
const (
	Profane Type = 0b111 << (iota * 3)
	Offensive
	Sexual
	Mean
	Spam
	Inappropriate = Profane | Offensive | Sexual | (Mean & Severe)
	Any           = Profane | Offensive | Sexual | Spam | Mean

	Mild     Type = 0b111_111_111_111_111
	Moderate      = 0b110_110_110_110_110
	Severe        = 0b100_100_100_100_100
)

Variables

View Source
var CensorReplacment rune = '*'

Functions

func Censor

func Censor(text string, types Type) (censoredText string, replaced int)

Censor returns a string with all but the first character of any inappropriate segment replaced with CensorReplacment

It is currently Experimental and not fully tested

func IsInappropriate

func IsInappropriate(text string) bool

IsInappropriate returns whether a phrase contains enough inappropriate words to meet or exceed InappropriateThreshold

Equivalent to

moderation.Scan(text).Is(moderation.Inappropriate)
Example
fmt.Println(IsInappropriate("hello"), IsInappropriate("sh1t"))
Output:

false true

Types

type Type

type Type uint32

Types and severities of inappropriateness

For compability, always reference them by name as their value may change from version to version.

Use a bitwise OR of multiple profanity classifications, and a bitwise AND to specify a severity level (default Mild). The definition of Inappropriate (mildly profane, mildly offensive, mildly sexual, or severely mean) serves as a good example.

Other operations on Type's are NOT supported.

Severities sould be interpreteted on an "at least" basis, e.g. Mild means Mild, Moderate, OR Severe.

func Scan added in v0.10.0

func Scan(text string) (scanResult Type)

Scan returns a bitmask of all types detected within given text, which can be queried with the Is function

Example
result := Scan("you're a dumbass")
fmt.Println(result.Is(Profane), result.Is(Offensive), result.Is(Sexual), result.Is(Mean), result.Is(Mean&Severe))
Output:

true false false true false

func (Type) Is added in v0.11.0

func (scanResult Type) Is(types Type) bool

Is returns whether the scan result includes a given Type or set of Type's

Example (Severity)
fmt.Println(Scan("sh1t").Is(Profane), Scan("sh1t").Is(Profane&Severe))
Output:

true false
Example (Types)
fmt.Println(Scan("shit").Is(Profane), Scan("shit").Is(Sexual|Mean))
fmt.Println(Scan("HELLO THERE").Is(Spam), Scan("duuuuuuuuumb").Is(Spam), Scan("Normal text").Is(Spam))
Output:

true false
true true false

Directories

Path Synopsis
generator module
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL