Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Rework api, various tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
finnbear committed Dec 30, 2020
1 parent 68353b4 commit 5043ef3
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 51 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ func main() {

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

func isInappropriate(phrase string) bool {
return moderation.Analyze(phrase).IsInappropriate()
}
```

```console
Expand All @@ -55,7 +52,7 @@ $ go run hello_world.go
Accuracy was evaluated based on a [dataset of moderated comments](https://raw.githubusercontent.com/vzhou842/profanity-check/master/profanity_check/data/clean_data.csv).
**Package**|**Accuracy**|**Comment**
:-----:|:-----:|:-----:
https://github.com/finnbear/moderation|90.50%|Current version is not stable
https://github.com/finnbear/moderation|90.49%|Current version is not stable
https://github.com/TwinProduction/go-away|74.83%|Many false positives from combined words like "push it"


Expand Down
18 changes: 0 additions & 18 deletions analysis.go

This file was deleted.

12 changes: 4 additions & 8 deletions comparison/comparison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ import (
"os"
"testing"

"github.com/finnbear/moderation"
"github.com/TwinProduction/go-away"
"github.com/TwinProduction/go-away"
"github.com/finnbear/moderation"
)

func moderationIsInappropriate(phrase string) bool {
return moderation.Analyze(phrase).IsInappropriate()
}

func TestModerationWikipedia(t *testing.T) {
testWikipedia(t, moderationIsInappropriate)
testWikipedia(t, moderation.IsInappropriate)
}

func TestGoAwayWikipedia(t *testing.T) {
testWikipedia(t, goaway.IsProfane)
testWikipedia(t, goaway.IsProfane)
}

func testWikipedia(t *testing.T, isInappropriate func(string) bool) {
Expand Down
2 changes: 2 additions & 0 deletions comparison/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module comparison

go 1.15

replace github.com/finnbear/moderation => ../

require (
github.com/TwinProduction/go-away v1.0.1
github.com/finnbear/moderation v0.5.0
Expand Down
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
moderation.prof
6 changes: 1 addition & 5 deletions examples/hello_world.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,8 @@ func main() {

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

func isInappropriate(phrase string) bool {
return moderation.Analyze(phrase).IsInappropriate()
}
6 changes: 3 additions & 3 deletions examples/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func main() {
defer pprof.StopCPUProfile()

for i := 0; i < 100000; i++ {
moderation.Analyze("hello")
moderation.Analyze("sh1t")
moderation.Analyze("Hello John Doe, I hope you're feeling well, as I come today bearing shitty news regarding your favorite chocolate chip cookie brand")
moderation.IsInappropriate("hello")
moderation.IsInappropriate("sh1t")
moderation.IsInappropriate("Hello John Doe, I hope you're feeling well, as I come today bearing shitty news regarding your favorite chocolate chip cookie brand")
}
}
14 changes: 10 additions & 4 deletions moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (
"unicode"
)

var tree *radix.Tree
var (
// The threshold where a phrase is considered inappropriate
InappropriateThreshold int = 1

tree *radix.Tree
)

func init() {
tree = radix.New()
Expand Down Expand Up @@ -41,14 +46,15 @@ var replacements = [...]string{
var removeAccentsTransform = transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)

// Analyze analyzes a given phrase for moderation purposes
func Analyze(text string) (analysis Analysis) {
func IsInappropriate(text string) bool {
buf := make([]byte, 0, len(text))
_, n, _ := transform.Append(removeAccentsTransform, buf, []byte(text))
text = string(buf[:n])
lastSepMin := 0
lastSepMax := 0

var matches radix.Queue
inappropriateLevel := 0

var lastMatchable byte
for _, textRune := range text {
Expand Down Expand Up @@ -120,7 +126,7 @@ func Analyze(text string) (analysis Analysis) {
if next != nil {
if next.Word() {
if next.Depth() > 4 || (next.Depth() > 3 && next.Start() != 's') || (next.Depth() >= lastSepMin && next.Depth() <= lastSepMax) {
analysis.InappropriateLevel += int(next.Data())
inappropriateLevel += int(next.Data())
}
}

Expand All @@ -136,5 +142,5 @@ func Analyze(text string) (analysis Analysis) {
}
}
}
return
return inappropriateLevel >= InappropriateThreshold
}
4 changes: 1 addition & 3 deletions moderation_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import (
"testing"
)

func IsProfane(phrase string) bool {
return Analyze(phrase).IsInappropriate()
}
var IsProfane = IsInappropriate

// from https://github.com/TwinProduction/go-away/blob/master/goaway_bench_test.go
func BenchmarkIsProfaneWhenShortStringHasNoProfanity(b *testing.B) {
Expand Down
9 changes: 4 additions & 5 deletions moderation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ func TestAnalyze(t *testing.T) {
{"We were seated at breakfast one morning, my wife and I, when the maid brought in a telegram. It was from Sherlock Holmes and ran in this way", false},
}
for _, testCase := range testCases {
analysis := Analyze(testCase.phrase)
if analysis.IsInappropriate() != testCase.offensive {
t.Errorf("phrase=\"%s\" analysis offensive=%v actual offensive=%v", testCase.phrase, analysis.IsInappropriate(), testCase.offensive)
inappropriate := IsInappropriate(testCase.phrase)
if inappropriate != testCase.offensive {
t.Errorf("phrase=\"%s\" analysis offensive=%v actual offensive=%v", testCase.phrase, inappropriate, testCase.offensive)
}
}
}
Expand All @@ -89,8 +89,7 @@ func TestAnalyzeWikipedia(t *testing.T) {
}
phrase := fields[1]
offensive := fields[0] == "1"
analysis := Analyze(phrase)
if analysis.IsInappropriate() == offensive {
if IsInappropriate(phrase) == offensive {
correct++
} else {
//fmt.Printf("phrase=\"%s\" analysis offensive=%v actual offensive=%v", phrase, analysis.IsInappropriate(), offensive)
Expand Down

0 comments on commit 5043ef3

Please sign in to comment.