-
Notifications
You must be signed in to change notification settings - Fork 3
/
cut.go
184 lines (162 loc) · 3.86 KB
/
cut.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
package wordninja
import (
"bufio"
"embed"
"errors"
"io"
"math"
"regexp"
"strings"
"unicode"
)
var maxLenWord int
var wordCost map[string]float64
//go:embed dict
var wordFile embed.FS
func init() {
words := loadWords()
generateCutWordMap(words)
}
// load the english cut words from file, return list of word.
func loadWords() []string {
words, err := readFileByLine()
if err != nil {
panic("load english cut word failed," + err.Error())
}
return words
}
// generateCutWordMap initialize wordCost map and the length of the longest word.
// the cost of a word is calculated by `log(log(len(words))*idx)`.
func generateCutWordMap(words []string) {
wordCost = make(map[string]float64)
var wordLen int
logLen := math.Log(float64(len(words)))
for idx, word := range words {
wordLen = len(word)
if wordLen > maxLenWord {
maxLenWord = wordLen
}
wordCost[word] = math.Log(logLen * float64(idx+1))
}
}
type match struct {
cost float64
idx int
}
type text struct {
s string
}
// bestMatch will return the minimal cost and its appropriate character's index.
func (s *text) bestMatch(costs []float64, i int) (match, error) {
candidates := costs[max(0, i-maxLenWord):i]
k := 0
var matchs []match
for j := len(candidates) - 1; j >= 0; j-- {
cost := getWordCost(strings.ToLower(s.s[i-k-1:i])) + float64(candidates[j])
matchs = append(matchs, match{cost: cost, idx: k + 1})
k++
}
return minCost(matchs)
}
// getWordCost return cost of word from the wordCost map.
// if the word is not exist in the map, it will return `9e99`.
func getWordCost(word string) float64 {
if v, ok := wordCost[word]; ok {
return v
}
return 9e99
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
// min return the minimal cost of the matchs.
func minCost(matchs []match) (match, error) {
if len(matchs) == 0 {
return match{}, errors.New("match.len ")
}
r := matchs[0]
for _, m := range matchs {
if m.cost < r.cost {
r = m
}
}
return r, nil
}
func Cut(s string) []string {
eng := getEnglishText(s)
return CutEnglish(eng)
}
// CutEnglish return the best matched words with cutting the English string `s`.
func CutEnglish(eng string) []string {
text := text{s: eng}
costs := []float64{0}
for i := 1; i < len(eng)+1; i++ {
if m, err := text.bestMatch(costs, i); err == nil {
costs = append(costs, m.cost)
}
}
var out []string
i := len(eng)
for i > 0 {
m, err := text.bestMatch(costs, i)
if err != nil {
continue
}
newToken := true
//ignore a lone apostrophe
if !(eng[i-m.idx:i] == "'") {
if len(out) > 0 {
//re-attach split 's and split digits or digit followed by digit.
if out[len(out)-1] == "'s" ||
(unicode.IsDigit(rune(eng[i-1])) && unicode.IsDigit(rune(out[len(out)-1][0]))) {
// combine current token with previous token.
out[len(out)-1] = eng[i-m.idx:i] + out[len(out)-1]
newToken = false
}
}
}
if newToken {
word := eng[i-m.idx : i]
out = append(out, word)
}
i -= m.idx
}
return reverse(out)
}
// reverse return reversed list of `dst`
func reverse(dst []string) []string {
length := len(dst)
for i := 0; i < length/2; i++ {
dst[i], dst[length-i-1] = dst[length-i-1], dst[i]
}
return dst
}
// getEnglishText return all the English characters of string `s`.
func getEnglishText(s string) string {
reg := regexp.MustCompile("[^a-zA-Z0-9']+")
return strings.Join(reg.Split(s, -1), "")
}
//ReadFileByLine return a list by reading file line by line.
func readFileByLine() (lines []string, err error) {
f, err := wordFile.Open("dict/wordninja_words.txt")
if err != nil {
return lines, err
}
defer f.Close()
rd := bufio.NewReader(f)
for {
line, err := rd.ReadString('\n') //以'\n'为结束符读入一行
if err != nil || io.EOF == err {
break
}
if line == "\n" {
continue
}
line = strings.Replace(line, "\n", "", -1)
lines = append(lines, line)
}
return lines, nil
}