-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathdict_1.16.go
143 lines (118 loc) · 3 KB
/
dict_1.16.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2016 The go-ego Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://github.com/go-ego/gse/blob/master/LICENSE
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//go:build go1.16
// +build go1.16
package gse
import (
_ "embed"
"strings"
)
//go:embed data/dict/dictionary.txt
var dataDict string
//go:embed data/dict/stop_tokens.txt
var stopDict string
// NewEmbed return new gse segmenter by embed dictionary
func NewEmbed(dict ...string) (seg Segmenter, err error) {
if len(dict) > 1 && (dict[1] == "alpha" || dict[1] == "en") {
seg.AlphaNum = true
}
err = seg.LoadDictEmbed(dict...)
return
}
// LoadDictEmbed load dictionary by embed file
func (seg *Segmenter) LoadDictEmbed(dict ...string) (err error) {
if len(dict) > 0 {
d := dict[0]
if strings.Contains(d, ", ") {
begin := 0
s := strings.Split(d, ", ")
if strings.Contains(d, "zh,") {
begin = 1
err = seg.LoadDictStr(dataDict)
}
for i := begin; i < len(s); i++ {
err = seg.LoadDictStr(s[i])
}
return
}
err = seg.LoadDictStr(d)
return
}
return seg.LoadDictStr(dataDict)
}
// LoadDictStr load dictionary from string
func (seg *Segmenter) LoadDictStr(dict string) error {
if seg.Dict == nil {
seg.Dict = NewDict()
seg.Init()
}
arr := strings.Split(dict, "\n")
for i := 0; i < len(arr); i++ {
s1 := strings.Split(arr[i], " ")
size := len(s1)
if size == 0 {
continue
}
text := strings.TrimSpace(s1[0])
freqText := ""
if len(s1) > 1 {
freqText = strings.TrimSpace(s1[1])
}
frequency := seg.Size(size, text, freqText)
if frequency == 0.0 {
continue
}
pos := ""
if size > 2 {
pos = strings.TrimSpace(strings.Trim(s1[2], "\n"))
}
// 将分词添加到字典中
words := seg.SplitTextToWords([]byte(text))
token := Token{text: words, frequency: frequency, pos: pos}
seg.Dict.AddToken(token)
}
seg.CalcToken()
return nil
}
// LoadStopEmbed load stop dictionary from embed file
func (seg *Segmenter) LoadStopEmbed(dict ...string) (err error) {
if len(dict) > 0 {
d := dict[0]
if strings.Contains(d, ", ") {
begin := 0
s := strings.Split(d, ", ")
if strings.Contains(d, "zh,") {
begin = 1
err = seg.LoadStopStr(stopDict)
}
for i := begin; i < len(s); i++ {
err = seg.LoadStopStr(s[i])
}
return
}
err = seg.LoadStopStr(d)
return
}
return seg.LoadStopStr(stopDict)
}
// LoadDictStr load the stop dictionary from string
func (seg *Segmenter) LoadStopStr(dict string) error {
if seg.StopWordMap == nil {
seg.StopWordMap = make(map[string]bool)
}
arr := strings.Split(dict, "\n")
for i := 0; i < len(arr); i++ {
key := strings.TrimSpace(arr[i])
if key != "" {
seg.StopWordMap[key] = true
}
}
return nil
}