-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordsplitter.go
152 lines (127 loc) · 3.01 KB
/
wordsplitter.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
package wordsplitter
import (
"bufio"
"fmt"
"math"
"os"
"path"
"runtime"
"strings"
)
var (
wordCost *CostMap
)
type CostMap struct {
mp map[string]float64
maxLengthKey int
}
func (cm *CostMap) Get(key string, default_ float64) float64 {
val, ok := cm.mp[key]
if !ok {
return default_
}
return val
}
func init() {
_, fp, _, _ := runtime.Caller(0)
languageWords, err := readWordFile(path.Join(path.Dir(fp), "words.txt"))
if err != nil {
fmt.Println("Could not load word frequency list - " + err.Error())
os.Exit(1)
}
wordCost = NewCostMap(languageWords, func(weight int, words []string) float64 {
return math.Log(float64(weight)) * math.Log(float64(len(words)))
})
}
func NewCostMap(words []string, Cost func(int, []string) float64) *CostMap {
mp := make(map[string]float64)
maxLengthKey := 0
for i := 0; i < len(words); i++ {
c := Cost(i+1, words)
mp[words[i]] = c
if len(words[i]) > maxLengthKey {
maxLengthKey = len(words[i])
}
}
cm := &CostMap{mp, maxLengthKey}
return cm
}
func readWordFile(filepath string) ([]string, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer f.Close()
words := []string{}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
words = append(words, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return words, nil
}
func ReverseFloatSlice(s []float64) []float64 {
reversedSlice := make([]float64, 0, len(s))
j := len(s) - 1
for j >= 0 {
reversedSlice = append(reversedSlice, s[j])
j -= 1
}
return reversedSlice
}
func ReverseStringSlice(s []string) []string {
reversedSlice := make([]string, 0, len(s))
j := len(s) - 1
for j >= 0 {
reversedSlice = append(reversedSlice, s[j])
j -= 1
}
return reversedSlice
}
func FindBestMatch(s string, costIdx int, cost []float64) (float64, int) {
startIdx := int(math.Max(0, float64(costIdx-wordCost.maxLengthKey)))
candidatesForBestMatch := cost[startIdx:costIdx]
candidatesForBestMatch = ReverseFloatSlice(candidatesForBestMatch)
optimalCost := math.Inf(1)
optimalCostIdx := -1
for k, c := range candidatesForBestMatch {
key := strings.ToLower(s[costIdx-k-1 : costIdx])
newCost := c + wordCost.Get(key, 9.0e99)
if newCost < optimalCost {
optimalCost = newCost
optimalCostIdx = k + 1
}
}
return optimalCost, optimalCostIdx
}
func isInteger(ch uint8) bool {
return ch >= 48 && ch <= 57
}
func Split(s string) []string {
cost := []float64{0.0}
for i := 1; i < len(s)+1; i++ {
c, _ := FindBestMatch(s, i, cost)
cost = append(cost, c)
}
var output []string
for i := len(s); i > 0; {
_, k := FindBestMatch(s, i, cost)
currSubstring := s[i-k : i]
nT := true
if currSubstring != "'" {
if len(output) > 0 {
if output[len(output)-1] == "'s" || (isInteger(s[i-1]) && isInteger(output[len(output)-1][0])) {
output[len(output)-1] = currSubstring + output[len(output)-1]
nT = false
}
}
}
if nT {
output = append(output, currSubstring)
}
i -= k
}
return ReverseStringSlice(output)
}