Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report