-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuman_numbers.go
217 lines (192 loc) · 5.52 KB
/
human_numbers.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
209
210
211
212
213
214
215
216
217
package humannumbers
import (
"fmt"
"strconv"
"strings"
)
// Parse takes a string containing numbers in the form
// of words, currently only English, and converts it
// to float64. Examples:
// two
// forty three
// eight thousand
// eight hundred and six
// one thousand six hundred and forty
// two thousand three hundred and eighty seven
// two hundred and forty six thousand three hundred and eighty seven.
func Parse(humanString string) (float64, error) {
// some linting
humanString = strings.ToLower(humanString)
humanString = strings.ReplaceAll(humanString, " and ", " ")
// handle negatives
var negative = strings.Contains(humanString, "negative")
humanString = strings.ReplaceAll(humanString, "negative", " ")
// handle decimals
var base = humanString
var decimal float64
var err error
if strings.Contains(humanString, "point") {
var arr = strings.Split(base, "point")
base = arr[0]
decimal, err = handleDecimals(arr[1])
if err != nil {
return 0, err
}
}
baseArr, err := convertHumanStringToNumberSlice(base)
if err != nil {
return 0, err
}
baseTotal, err := compressNumberSliceToInt(baseArr)
if err != nil {
return 0, err
}
if decimal != 0.0 {
baseTotal += decimal
}
if negative {
baseTotal *= -1
}
return baseTotal, nil
}
// handleDecimals is pretty simple, due to the language, it just
// smashes the digits together behind the decimal point
func handleDecimals(humanString string) (float64, error) {
var decimalArr, err = convertHumanStringToNumberSlice(humanString)
if err != nil {
return 0, err
}
var total float64
var multiplier = 0.1
for _, digit := range decimalArr {
total += float64(digit) * multiplier
multiplier *= .10
}
return total, nil
}
// convertHumanStringToNumberSlice loops through the give string and places the
// numeric equivalent to each word in an array of ints
// e.g. input: "two hundred and forty seven thousand six hundred and twenty four
//
// output: []int{2, 100, 40, 7, 1000, 6, 100, 20, 4}
func convertHumanStringToNumberSlice(humanString string) ([]int, error) {
var humanArr = strings.Fields(humanString)
var numbers = make([]int, len(humanArr))
for i, word := range humanArr {
if num, has := base[word]; has {
numbers[i] = num
} else if num, has := decades[word]; has {
numbers[i] = num
} else if word == "hundred" {
numbers[i] = 100
} else if num, has := largeMagnitudes[word]; has {
numbers[i] = num
} else {
return nil, fmt.Errorf("unknown word '%s'", word)
}
}
return numbers, nil
}
// compressNumberSliceToInt takes an int slice of numbers
// and either adds or multiplies them depending on their
// placement in the slice until there is only one
// element in the slice.
// e.g. input: []int{2, 100, 40, 7, 1000, 6, 100, 20, 4}
//
// output: 247624
func compressNumberSliceToInt(numbers []int) (float64, error) {
if len(numbers) == 1 {
return float64(numbers[0]), nil
}
// calculate decades
for i := 0; i < len(numbers)-1; i++ {
if numbers[i] >= 20 && numbers[i] <= 90 {
if numbers[i+1] > 0 && numbers[i+1] < 10 {
numbers[i] += numbers[i+1]
numbers = remove(numbers, i+1)
}
}
}
// calculate hundreds
for i, num := range numbers {
if num == 100 && i > 0 {
if numbers[i-1] > 0 && numbers[i-1] < 10 {
numbers[i] = num * numbers[i-1]
numbers = remove(numbers, i-1)
i -= 1
}
}
if i < len(numbers)-1 && numbers[i] >= 100 && numbers[i] < 1000 {
if numbers[i+1] > 0 && numbers[i+1] < 100 {
numbers[i] += numbers[i+1]
numbers = remove(numbers, i+1)
}
}
}
// calculate large multiples i.e. the numbers before the large one
for i := 0; i < len(numbers); i++ {
if i > 0 && numbers[i] >= 1000 && numbers[i-1] < 1000 {
numbers[i] *= numbers[i-1]
numbers = remove(numbers, i-1)
i -= 1
}
}
// add on larger number specificity i.e. the numbers after the large one
for i := len(numbers) - 1; i > 0; i-- {
if numbers[i] < numbers[i-1] {
numbers[i-1] += numbers[i]
numbers = remove(numbers, i)
}
}
if len(numbers) != 1 {
return 0.0, fmt.Errorf("number array was no fully reduced: %+v", numbers)
}
return float64(numbers[0]), nil
}
// floatToString is a work in progress, its intention is to turn floats into human text
func floatToString(number float64) string {
var numArr = strings.Split(strconv.FormatFloat(number, 'f', -1, 64), ".")
var minorMultiple = 1
var majorMultiple = 1
var wordsArr []string
// take care of the decimals
if len(numArr) == 2 {
var decimalArr = numArr[1]
for i := len(decimalArr) - 1; i >= 0; i-- {
wordsArr = append([]string{baseReverse[int(decimalArr[i]-'0')]}, wordsArr...)
}
wordsArr = append([]string{"dot"}, wordsArr...)
}
var nextIsMajor bool
var wholeNumber = numArr[0]
for i := len(wholeNumber) - 1; i >= 0; i-- {
if nextIsMajor {
var largeMag = largeMagToString(int(wholeNumber[i]-'0') * majorMultiple)
if largeMag != "" {
wordsArr = append([]string{largeMag}, wordsArr...)
}
nextIsMajor = false
}
switch minorMultiple {
case 1:
wordsArr = append([]string{baseReverse[int(wholeNumber[i]-'0')]}, wordsArr...)
case 10:
decade, has := decadesReverse[int(wholeNumber[i]-'0')*minorMultiple]
if has {
wordsArr = append([]string{decade}, wordsArr...)
}
case 100:
wordsArr = append([]string{baseReverse[int(wholeNumber[i]-'0')], "hundred"}, wordsArr...)
nextIsMajor = true
}
minorMultiple *= 10
majorMultiple *= 10
if minorMultiple == 1000 {
minorMultiple = 1
}
}
return strings.Join(wordsArr, " ")
}
func remove(slice []int, s int) []int {
return append(slice[:s], slice[s+1:]...)
}