-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexemePath.go
208 lines (191 loc) · 3.81 KB
/
LexemePath.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
201
202
203
204
205
206
207
208
package ikgo
import (
"fmt"
"strings"
)
/**
* Lexeme链(路径)
*/
type LexemePath struct {
set QuickSortSet
pathBegin, pathEnd, payloadLength int // 起止位置以及词元链的有效字符长度
}
func NewLexemePath() (l *LexemePath) {
l = &LexemePath{pathBegin: -1, pathEnd: -1, payloadLength: 0}
return
}
/**
* 检测词元位置交叉(有歧义的切分)
* @param lexeme
* @return
*/
func (lp *LexemePath) checkCross(l *Lexeme) bool {
return (l.begin >= lp.pathBegin && l.begin < lp.pathEnd) ||
(lp.pathBegin >= l.begin && lp.pathBegin < l.begin+l.length)
}
/**
* 向LexemePath追加相交的Lexeme
* @param lexeme
* @return
*/
func (lp *LexemePath) addCrossLexeme(l *Lexeme) bool {
if lp.set.size == 0 {
lp.set.addLexeme(l)
lp.pathBegin = l.begin
lp.pathEnd = l.begin + l.length
lp.payloadLength += l.length
return true
}
if lp.checkCross(l) {
lp.set.addLexeme(l)
if l.begin+l.length > lp.pathEnd {
lp.pathEnd = l.begin + l.length
}
lp.payloadLength = lp.pathEnd - lp.pathBegin
return true
}
return false
}
/**
* 向LexemePath追加不相交的Lexeme
* @param lexeme
* @return
*/
func (lp *LexemePath) addNotCrossLexeme(l *Lexeme) bool {
if lp.set.size == 0 {
lp.set.addLexeme(l)
lp.pathBegin = l.begin
lp.pathEnd = l.begin + l.length
lp.payloadLength += l.length
return true
}
if lp.checkCross(l) {
return false
}
lp.set.addLexeme(l)
lp.payloadLength += l.length
head := lp.set.peekFirst()
tail := lp.set.peekLast()
lp.pathBegin = head.begin
lp.pathEnd = tail.begin + tail.length
return true
}
/**
* 移除尾部的Lexeme
* @return
*/
func (lp *LexemePath) removeTail() (l *Lexeme) {
l = lp.set.pollLast()
if lp.set.size == 0 {
lp.pathBegin = -1
lp.pathEnd = -1
lp.payloadLength = 0
} else {
lp.payloadLength -= l.length
tail := lp.set.peekLast()
lp.pathEnd = tail.begin + tail.length
}
return
}
/**
* 获取LexemePath的路径长度
* @return
*/
func (lp *LexemePath) getPathLength() int {
return lp.pathEnd - lp.pathBegin
}
/**
* X权重(词元长度积)
* @return
*/
func (lp *LexemePath) getXWeight() (product int) {
product = 1
c := lp.set.head
for c != nil && c.lexeme != nil {
product *= c.lexeme.length
c = c.next
}
return
}
/**
* 词元位置权重
* @return
*/
func (lp *LexemePath) getPWeight() (product int) {
product = 0
p := 0
c := lp.set.head
for c != nil && c.lexeme != nil {
p++
product += p * c.lexeme.length
c = c.next
}
return
}
func (lp *LexemePath) deepCopy() *LexemePath {
nlp := &LexemePath{}
nlp.pathBegin = lp.pathBegin
nlp.pathEnd = lp.pathEnd
nlp.payloadLength = lp.payloadLength
c := lp.set.head
for c != nil && c.lexeme != nil {
nlp.set.addLexeme(c.lexeme)
c = c.next
}
return nlp
}
func (lp *LexemePath) compare(nlp *LexemePath) int {
if lp.payloadLength > nlp.payloadLength {
return -1
}
if lp.payloadLength < nlp.payloadLength {
return 1
}
if lp.set.size < nlp.set.size {
return -1
}
if lp.set.size > nlp.set.size {
return 1
}
if lp.getPathLength() > nlp.getPathLength() {
return -1
}
if lp.getPathLength() < nlp.getPathLength() {
return 1
}
if lp.pathEnd > nlp.pathEnd {
return -1
}
if lp.pathEnd < nlp.pathEnd {
return 1
}
xw1 := lp.getXWeight()
xw2 := nlp.getXWeight()
if xw1 > xw2 {
return -1
}
if xw1 < xw2 {
return 1
}
pw1 := lp.getPWeight()
pw2 := nlp.getPWeight()
if pw1 > pw2 {
return -1
}
if pw1 < pw2 {
return 1
}
return 0
}
func (lp *LexemePath) toString() string {
sb := fmt.Sprintf("pathBegin: %d", lp.pathBegin)
se := fmt.Sprintf("pathEnd : %d", lp.pathEnd)
sp := fmt.Sprintf("pathPayload: %d", lp.payloadLength)
sl := []string{sb, se, sp}
head := lp.set.head
for head != nil {
sl = append(sl, fmt.Sprintf("lexme: %+v", head.lexeme))
head = head.next
}
return strings.Join(sl, "\n")
}