-
Notifications
You must be signed in to change notification settings - Fork 1
/
segment.go
43 lines (37 loc) · 1.36 KB
/
segment.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 tool
import (
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/admpub/nging/v5/application/handler"
"github.com/admpub/webx/application/library/search/segment"
//_ "github.com/admpub/webx/application/library/search/segment/gojieba"
//_ "github.com/admpub/webx/application/library/search/segment/jiebago"
_ "github.com/admpub/webx/application/library/search/segment/sego"
)
var SegmentMode = echo.NewKVData()
func init() {
SegmentMode.Add(`all`, `完整模式`)
SegmentMode.Add(`new`, `新词识别`)
SegmentMode.Add(`search`, `搜索引擎模式`)
SegmentMode.Add(`tag`, `词性标注`)
SegmentMode.Add(`keywords`, `关键词提取`)
}
func Segment(ctx echo.Context) error {
var err error
if ctx.IsPost() {
keywords := ctx.Form(`keywords`)
if len(keywords) == 0 {
return ctx.JSON(ctx.Data().SetInfo(ctx.T(`关键词不能为空`), code.InvalidParameter.Int()))
}
mode := ctx.Form(`mode`)
if !SegmentMode.Has(mode) {
return ctx.JSON(ctx.Data().SetInfo(ctx.T(`模式不正确`), code.InvalidParameter.Int()))
}
splitedWords := segment.SplitWordsBy(com.Str2bytes(keywords), mode)
return ctx.JSON(ctx.Data().SetData(splitedWords))
}
ctx.Set(`modes`, SegmentMode.Slice())
ctx.Set(`isInitialized`, segment.IsInitialized())
return ctx.Render(`official/tool/segment/index`, handler.Err(ctx, err))
}