-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbayes.go
43 lines (36 loc) · 1.21 KB
/
bayes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
/*
#cgo CFLAGS: -I/opt/lisp/newlisp
#cgo LDFLAGS: -lnewlisp
#include <stdlib.h>
char* newlispEvalStr(char *cmd);
*/
import "C"
import (
"fmt"
"unsafe"
)
const lisp = `[cmd]
(load "/usr/share/newlisp/modules/zlib.lsp")
(bayes-train (parse (lower-case (zlib:gz-read-file "Doyle.txt.gz")) "[^a-z]+" 0) '() '() 'DDB)
(bayes-train '() (parse (lower-case (zlib:gz-read-file "Dowson.txt.gz")) "[^a-z]+" 0) '() 'DDB)
(bayes-train '() '() (parse (lower-case (zlib:gz-read-file "Beowulf.txt.gz")) "[^a-z]+" 0) 'DDB)
(println "Doyle test : "
(bayes-query (parse "adventures of sherlock holmes") 'DDB true)
(bayes-query (parse "adventures of sherlock holmes") 'DDB))
(println "Downson test: "
(bayes-query (parse "comedy of masks") 'DDB true)
(bayes-query (parse "comedy of masks") 'DDB))
(println "Beowulf test: "
(bayes-query (parse "hrothgar and beowulf") 'DDB true)
(bayes-query (parse "hrothgar and beowulf") 'DDB))
[/cmd]`
func main() {
lib := C.CString("newlispEvalStr")
defer C.free(unsafe.Pointer(lib))
code := C.CString(lisp)
defer C.free(unsafe.Pointer(code))
res := C.GoString(C.newlispEvalStr(code))
fmt.Printf("Received %+v characters\n", len(res))
fmt.Printf("Output:\n%+v\n", res)
}