Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use K-nearest neighbor algorithm to detect porn in novels in GO language

Shulou Source: shulou.com Published: 2022-06-03 11:54:27 09月13日 Update

This article shows you how GO language uses K-nearest neighbor algorithm to realize porn detection in novels. The content is concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Usuage:

Go run kNN.go-file= "data.txt"

The key is the selection of vector points and the determination of threshold.

Package main import ("bufio"flag"fmt"io"log"math"os"path"path/filepath") var debug bool = falsevar data_dir string = ". / moyan" / / File storage directory var limen float64 = 0.1159203888322267 / / threshold const (MIN_HANZI rune = 0x3400 MAX_HANZI rune = 0x9fbb) var labels [] rune = [] rune {0x817f, 0x80f8, 0x4e73, 0x81c0, 0x5c41, 0x80a1, 0x88f8, 0x6deb } func errHandle (err error) {if err! = nil {log.Fatal (err)}} func load (name string) (m map [rune] int, err error) {f, err: = os.Open (name) if err! = nil {return nil, err} defer f.Close () buf: = bufio.NewReader (f) m = make (map [rune] int) var r rune for {r, _ Err = buf.ReadRune () if err! = nil {if err = = io.EOF {break} return nil, err} if r > = MIN_HANZI & & r = limen: fmt.Println (fp, dis, "pornographic") case dis = = 1.0: fmt.Println (fp, dis, "are you cheating") case dis = = 0: fmt.Println (fp, dis) "check whether the file character encoding is in utf8 format") default: fmt.Println (fp, dis, "normal")}} func walkFunc (fp string, info os.FileInfo, err error) error {if path.Ext (fp) = = ".txt" {m, err: = load (fp) errHandle (err) _, dis: = classify (m) check (fp, dis)} return err} var file string func init () {_ Err: = os.Stat (data_dir) if err! = nil {err = os.Mkdir (data_dir, os.ModePerm) errHandle (err)} flag.StringVar (& file, "file", "", "file read in,if you don't give the file read in," + "it will create a data dictionary,just pust your files in it")} func main () {flag.Parse () if file = "" {filepath.Walk (data_dir) WalkFunc) return} m, err: = load (file) errHandle (err) _, dis: = classify (m) check (file, dis)} the above is how GO language uses K-nearest neighbor algorithm to realize novel porn detection. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Novels algorithms languages nearest neighbors content skills documents knowledge thresholds conciseness brevity keys vectors characters that is articles more formats directories articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Information OPPO Reno Shulou Technology MariaDB