Go language using K-nearest neighbor algorithm to realize the novel Jian Huang _golang

Source: Internet
Author: User

Usuage:
Go run knn.go--file= "Data.txt"

The key is the selection of vector points and the determination of threshold value
Sample data from the national press and publication Administration issued a notice published in the "40 Obscene Pornography network novel list"

Package main import ("Bufio" "Flag" "FMT" "io" "Log" "Math" "OS" "Path" "Path/filepath") var Debu g bool = False var Data_dir string = "./moyan"//File Store directory var limen float64 = 0.1159203888322267//Threshold const (Min_han ZI rune = 0x3400 Max_hanzi rune = 0x9fbb) var labels []rune = []rune{0x817f, 0x80f8, 0x4e73, 0x81c0, 0x5c41, 0x8 0a1, 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 <= Max_hanzi {M[r] + + 1}} return M, nil} func classify (M map[rune]int) (IdV []float64, dis float64) {len_m: = Len (m) for I, V: = RangE Labels {if debug {fmt.
  Println (i, M[v], string (v), float64 (m[v))/float64 (Len_m))} IdV = Append (IdV, float64 (m[v))/float64 (Len_m))} For _, V: = range IdV {dis + math. Pow (V, 2)} dis = Math. SQRT (DIS) return} func check (FP string, dis float64) {switch {case dis >= limen:fmt. PRINTLN (FP, Dis, "yellow-related") case dis = = 1.0:fmt. PRINTLN (FP, dis, "Are you cheating") case dis = = 0:fmt. PRINTLN (FP, dis, "Check the file character encoding is not UTF8 format bar") 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 ER R} 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'll 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 the entire contents of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.