-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsegmentor.go
189 lines (165 loc) · 4.1 KB
/
segmentor.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
package darkseg
import (
"bufio"
"github.com/3xian/darkseg/hmm"
"github.com/3xian/darkseg/trie"
"github.com/3xian/darkseg/util"
"log"
"os"
"strings"
"unicode"
"unicode/utf8"
)
type Segmentor struct {
hmm *hmm.Hmm
forwardTrie *trie.Trie
backwardTrie *trie.Trie
debugFlag bool
}
func NewSegmentor(dictPath string, hmmModelPath string) (segmentor *Segmentor, err error) {
segmentor = &Segmentor{}
segmentor.forwardTrie = trie.NewTrie()
segmentor.backwardTrie = trie.NewTrie()
dictFile, err := os.Open(dictPath)
if err != nil {
return nil, err
}
defer dictFile.Close()
dictWordCounter := 0
for scanner := bufio.NewScanner(dictFile); scanner.Scan(); {
word := []rune(scanner.Text())
segmentor.forwardTrie.Insert(word)
segmentor.backwardTrie.Insert(util.ReverseRuneSlice(word))
dictWordCounter++
}
log.Println("dictWordCount:", dictWordCounter)
if segmentor.hmm, err = hmm.NewHmm(hmmModelPath); err != nil {
return nil, err
}
return segmentor, nil
}
func (s *Segmentor) SetDebug(flag bool) {
s.debugFlag = flag
}
func (s *Segmentor) Segment(text string) (terms []string) {
if s.debugFlag {
log.Println("text:", text)
}
textInRune := []rune(text)
forward := s.maxMatch(textInRune)
if s.debugFlag {
log.Println("forward:", FormatTerms(offsetsToTerms(textInRune, forward)))
}
backward := s.reversedMaxMatch(textInRune)
if s.debugFlag {
log.Println("backward:", FormatTerms(offsetsToTerms(textInRune, backward)))
}
if util.IsIntSliceSame(forward, backward) {
return offsetsToTerms(textInRune, forward)
}
return s.combineSegmentResults(textInRune, forward, backward)
}
func FormatTerms(terms []string) (str string) {
return strings.Join(terms, "|")
}
func (s *Segmentor) combineSegmentResults(textInRune []rune, forward []int, backward []int) (terms []string) {
var (
subStart, subEnd int
fi, fj, bi, bj int
)
termEnd := func(offsets []int, index int) int {
if index+1 >= len(offsets) {
return len(textInRune)
}
return offsets[index+1]
}
for fi < len(forward) {
fj = fi
bj = bi
for {
forwardTermEnd := termEnd(forward, fj)
for termEnd(backward, bj) < forwardTermEnd {
bj++
}
if forwardTermEnd == termEnd(backward, bj) {
break
}
fj++
}
fj++
bj++
subStart = forward[fi]
if fj >= len(forward) {
subEnd = len(textInRune)
} else {
subEnd = forward[fj]
}
if fi+1 == fj && bi+1 == bj {
terms = append(terms, string(textInRune[subStart:subEnd]))
} else {
hmmResult := s.hmm.Segment(textInRune[subStart:subEnd])
if s.debugFlag {
log.Println("sub hmm:", FormatTerms(hmmResult))
}
terms = append(terms, hmmResult...)
}
fi = fj
bi = bj
}
return
}
func (s *Segmentor) maxMatch(textInRune []rune) (offsets []int) {
for i := 0; i < len(textInRune); {
offsets = append(offsets, i)
matchedWord := s.forwardTrie.LongestMatchedPrefix(textInRune[i:])
if len(matchedWord) > 0 {
i += len(matchedWord)
} else {
matchedWord = alphabetsAndDigitsPrefix(textInRune[i:])
if len(matchedWord) > 0 {
i += len(matchedWord)
} else {
i++
}
}
}
return
}
func (s *Segmentor) reversedMaxMatch(textInRune []rune) (offsets []int) {
textInRune = util.ReverseRuneSlice(textInRune)
for i := 0; i < len(textInRune); {
matchedWord := s.backwardTrie.LongestMatchedPrefix(textInRune[i:])
if len(matchedWord) > 0 {
i += len(matchedWord)
} else {
matchedWord = alphabetsAndDigitsPrefix(textInRune[i:])
if len(matchedWord) > 0 {
i += len(matchedWord)
} else {
i++
}
}
offsets = append(offsets, len(textInRune)-i)
}
return util.ReverseIntSlice(offsets)
}
func offsetsToTerms(textInRune []rune, offsets []int) (terms []string) {
for i := 0; i < len(offsets); i++ {
if i+1 < len(offsets) {
terms = append(terms, string(textInRune[offsets[i]:offsets[i+1]]))
} else {
terms = append(terms, string(textInRune[offsets[i]:]))
}
}
return
}
func alphabetsAndDigitsPrefix(textInRune []rune) (prefix []rune) {
for _, r := range textInRune {
if utf8.RuneLen(r) <= 2 && (unicode.IsLetter(r) || unicode.IsDigit(r)) {
prefix = append(prefix, r)
} else {
break
}
}
return
}