Last active
December 11, 2020 11:36
-
-
Save recoilme/ca0ae9528f93d52db150a04f41e1aaa2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"net" | |
) | |
// mac: brew install vowpal-wabbit | |
// start daemon: | |
// vw --daemon --foreground --port 58009 --num_children 1 --oaa 2 --loss_function=logistic | |
func main() { | |
conn, err := net.Dial("tcp", "127.0.0.1:58009") | |
if err != nil { | |
log.Fatal(err) | |
} | |
r := bufio.NewReader(conn) | |
for i := 0; i < 5; i++ { | |
fmt.Println("-----") | |
s := "" | |
if i%2 == 0 { | |
s = "1 | anal\n" | |
} else { | |
s = "2 | oral\n" | |
} | |
_, err := conn.Write([]byte(s)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
bin, err := r.ReadBytes(byte(10)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
if len(bin) > 0 { | |
bin = bin[:len(bin)-1] | |
} | |
fmt.Println("send", s, "recv", string(bin)) | |
} | |
result, err := predict(conn, r, "| anal\n") | |
fmt.Println("predict anal", result) | |
result, err = predict(conn, r, "| oral\n") | |
fmt.Println("predict oral", result) | |
err = saveModel(conn, r) | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = conn.Close() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func saveModel(conn net.Conn, r *bufio.Reader) error { | |
_, err := conn.Write([]byte("save_filemodel.vw")) | |
if err != nil { | |
return err | |
} | |
return err | |
} | |
func predict(conn net.Conn, r *bufio.Reader, line string) (string, error) { | |
_, err := conn.Write([]byte(line)) | |
if err != nil { | |
return "", err | |
} | |
bin, err := r.ReadBytes(byte(10)) | |
if err != nil { | |
return "", err | |
} | |
if len(bin) > 0 { | |
bin = bin[:len(bin)-1] | |
} | |
return string(bin), nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment