-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
80 lines (68 loc) · 1.55 KB
/
main.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
//go get github.com/huichen/sego
"github.com/huichen/sego"
"io"
"net/http"
"log"
"os"
"encoding/json"
"runtime"
)
var segmenter sego.Segmenter
type response struct {
Status int
Msg string
Response string
}
// hello world, the web server
func handler(w http.ResponseWriter, req *http.Request) {
//获取客户端通过GET/POST方式传递的参数
req.ParseForm()
content, found := req.Form["content"]
_, deep_search_found := req.Form["deep_search"]
//fmt.Println(time.Now(), " found:", found)
res := response{1, "", ""}
if found {
if len(content[0]) > 10000 {
res.Status = 0
res.Msg = "超过最大处理内容长度"
} else {
var text []byte = []byte(content[0])
segments := segmenter.Segment(text)
if deep_search_found {
res.Response = sego.SegmentsToString(segments, true)
} else {
res.Response = sego.SegmentsToString(segments, false)
}
}
} else {
res.Status = 0
res.Msg = "请传入正确的参数"
}
rjson, errs := json.Marshal(res)
if errs != nil {
fmt.Println(errs.Error())
}
io.WriteString(w, string(rjson))
rjson = nil
}
func main() {
// 将线程数设置为CPU数
runtime.GOMAXPROCS(runtime.NumCPU())
fmt.Println("run with ", runtime.NumCPU(), " cores");
var port string;
if len(os.Args) >= 2 {
port = os.Args[1]
} else {
println("port not found")
return
}
segmenter.LoadDictionary("dictionary.txt")
http.HandleFunc("/segment", handler)
err := http.ListenAndServe("127.0.0.1:"+port, nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}