swearfilter

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2021 License: MIT Imports: 6 Imported by: 0

README

gofuckyourself

GoDoc Go Report Card cover.run

A sanitization-based swear filter for Go.

Installing

Just import github.com/boosh/swearfilter if using go modules.

Example

package main

import (
	"fmt"

	"github.com/boosh/swearfilter"
)

var message = "This is a fûçking message with shitty swear words asswipe."
var swears = []string{"fuck", "shit", "^ass"}

func main() {
	filter := NewSwearFilter(false, swears...)
	swearFound, swearsFound, err := filter.Check(message)
	fmt.Println("Swear found: ", swearFound)
	fmt.Println("Swears tripped: ", swearsFound)
	fmt.Println("Error: ", err)
}
Output
> go run main.go
Swear found:  true
Swears tripped:  [fuck shit ^ass]
Error:  <nil>

Options

By default substring testing is performed, e.g. so abc will match any of 1abc, 1abc2 and abc2.

To help keep word lists concise but performance good, simple (simulated) regex matching is supported. The only control characters supported are ^ and $. These will perform prefix/suffix string match tests respectively. E.g. so ^ass will match asses but not pass. These simple regexes aren't compiled to regexes internally so may be faster (but they haven't been benchmarked).

Full regex support can be enabled by passing the relevant parameter when calling swearfilter.New. In this case, each swear word will be compiled to a regex and tested with regex matching.

License

The source code for gofuckyourself is released under the MIT License. See LICENSE for more details.

Original Author

JoshuaDoes

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SwearFilter

type SwearFilter struct {
	//Options to tell the swear filter how to operate
	DisableNormalize                bool //Disables normalization of alphabetic characters if set to true (ex: à -> a)
	DisableSpacedTab                bool //Disables converting tabs to singular spaces (ex: [tab][tab] -> [space][space])
	DisableMultiWhitespaceStripping bool //Disables stripping down multiple whitespaces (ex: hello[space][space]world -> hello[space]world)
	DisableZeroWidthStripping       bool //Disables stripping zero-width spaces
	DisableSpacedBypass             bool //Disables testing for spaced bypasses (if hell is in filter, look for occurrences of h and detect only alphabetic characters that follow; ex: h[space]e[space]l[space]l[space] -> hell)
	DisableSimpleRegex              bool //Disables using strings.HasPrefix if the string starts with ^ and strings.HasSuffix if it ends with $. Only strings.Contains will be used
	EnableFullRegex                 bool //Enables treating each word in the wordlist as a regex

	//A list of words to check against the filters
	BadWords       map[string]struct{}
	BadWordRegexps map[string]*regexp.Regexp
	// contains filtered or unexported fields
}

SwearFilter contains settings for the swear filter

func NewSwearFilter

func NewSwearFilter(enableFullRegex bool, uhohwords ...string) (filter *SwearFilter)

NewSwearFilter returns an initialized SwearFilter struct to check messages against

func (*SwearFilter) Add

func (filter *SwearFilter) Add(badWords ...string)

Add appends the given word to the uhohwords list

func (*SwearFilter) Check

func (filter *SwearFilter) Check(msg string) (trippedWords []string, err error)

Check will return any words that trip an enabled swear filter, an error if any, or nothing if you've removed all the words for some reason

func (*SwearFilter) Delete

func (filter *SwearFilter) Delete(badWords ...string)

Delete deletes the given word from the uhohwords list

func (*SwearFilter) Load

func (filter *SwearFilter) Load() (activeWords []string)

Load return the uhohwords list

Jump to

Keyboard shortcuts

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