-
Notifications
You must be signed in to change notification settings - Fork 7
/
mmseg.go
200 lines (173 loc) · 4.46 KB
/
mmseg.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package gommseg
import (
"bufio"
"bytes"
"os"
"path"
"runtime"
"strconv"
)
// const (
// ChineseCharLength = 3
// )
var Ana *Segment
func init() {
_, fileName, _, _ := runtime.Caller(1)
dataPath := path.Join(path.Dir(fileName), "d/data.txt")
Ana = NewSegment(dataPath)
}
type Segment struct {
WordMap map[string]*Word
}
// "/Users/raquelken/Desktop/mini.txt"
func NewSegment(fileName string) *Segment {
file, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer file.Close()
reader := bufio.NewReader(file)
var (
er error = nil
line []byte
)
var wordMap map[string]*Word
wordMap = make(map[string]*Word)
for er == nil {
line, _, er = reader.ReadLine()
a := bytes.Split(line, []byte("\t"))
if len(a) >= 2 {
text := string(a[0])
freq, e := strconv.Atoi(string(a[1]))
if e == nil {
wordMap[text] = NewWord(text, freq)
}
}
}
return &Segment{wordMap}
}
func (ana *Segment) Get(text string) (*Word, bool) {
word, ok := ana.WordMap[text]
return word, ok
}
func (ana *Segment) MatchWords(text string) []*Word {
var (
matchWords []*Word
matchString string = ""
firstChar string
isFirstChar bool = false
)
for _, char := range text {
matchString += string(char)
if !isFirstChar {
firstChar = matchString
isFirstChar = false
}
if word, ok := ana.Get(matchString); ok {
matchWords = append(matchWords, word)
}
}
if len(matchWords) == 0 {
// matchWords = append(matchWords, NewWord(string(text[0:ChineseCharLength]), 0))
matchWords = append(matchWords, NewWord(firstChar, 0))
}
return matchWords
}
func (ana *Segment) Chunks(text string) []*Chunk {
var chunks []*Chunk
for _, word1 := range ana.MatchWords(text) {
textLength := len(text)
wordLength1 := len(word1.Text)
if wordLength1 < textLength {
text1 := string([]byte(text)[wordLength1:textLength])
for _, word2 := range ana.MatchWords(text1) {
wordLength2 := len(word2.Text)
if wordLength1+wordLength2 < textLength {
text2 := string([]byte(text)[wordLength1+wordLength2 : textLength])
for _, word3 := range ana.MatchWords(text2) {
chunks = append(chunks, NewChunk([]*Word{word1, word2, word3}))
}
} else {
chunks = append(chunks, NewChunk([]*Word{word1, word2}))
}
}
} else {
chunks = append(chunks, NewChunk([]*Word{word1}))
}
}
return chunks
}
func (ana *Segment) Filter(chunks []*Chunk) *Chunk {
var lengthFilterChunks []*Chunk
var maxLength int = 0
for _, chunk := range chunks {
if chunk.Length() > maxLength {
lengthFilterChunks = []*Chunk{chunk}
maxLength = chunk.Length()
} else if chunk.Length() == maxLength {
lengthFilterChunks = append(lengthFilterChunks, chunk)
}
}
if len(lengthFilterChunks) == 1 {
return lengthFilterChunks[0]
}
var averageLengthFilterChunks []*Chunk
var maxAverageLength float64 = 0
for _, chunk := range lengthFilterChunks {
if chunk.AverageLength() > maxAverageLength {
averageLengthFilterChunks = []*Chunk{chunk}
maxAverageLength = chunk.AverageLength()
} else if chunk.AverageLength() == maxAverageLength {
averageLengthFilterChunks = append(averageLengthFilterChunks, chunk)
}
}
if len(averageLengthFilterChunks) == 1 {
return averageLengthFilterChunks[0]
}
var varianceFilterChunks []*Chunk
var minVariance float64 = 0.0
for idx, chunk := range averageLengthFilterChunks {
if idx == 1 {
varianceFilterChunks = []*Chunk{chunk}
minVariance = chunk.Variance()
} else if chunk.Variance() < minVariance {
varianceFilterChunks = []*Chunk{chunk}
minVariance = chunk.Variance()
} else if chunk.Variance() == minVariance {
varianceFilterChunks = append(varianceFilterChunks, chunk)
}
}
if len(varianceFilterChunks) == 1 {
return varianceFilterChunks[0]
}
var freqFilterChunks []*Chunk
var maxFreq int = 0
for _, chunk := range varianceFilterChunks {
if chunk.Freq() > maxFreq {
freqFilterChunks = []*Chunk{chunk}
maxFreq = chunk.Freq()
} else if chunk.Freq() == maxFreq {
freqFilterChunks = append(freqFilterChunks, chunk)
}
}
return freqFilterChunks[0]
}
func (ana *Segment) firstWord(text string) string {
chunks := ana.Chunks(text)
chunk := ana.Filter(chunks)
return chunk.Words[0].Text
}
func (ana *Segment) Cut(text string) []string {
var (
pos int = 0
textLength int = len(text)
result []string
)
for pos < textLength {
str := string([]byte(text)[pos:textLength])
word := ana.firstWord(str)
result = append(result, word)
pos += len(word)
}
return result
}