-
Notifications
You must be signed in to change notification settings - Fork 0
/
statistic.go
289 lines (241 loc) · 5.42 KB
/
statistic.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2018, Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package strings
import (
"strings"
"unicode"
"github.com/shuLhan/share/lib/ints"
"github.com/shuLhan/share/lib/runes"
)
// CountAlnum return number of alpha-numeric character in text.
func CountAlnum(text string) (n int) {
if len(text) == 0 {
return
}
for _, v := range text {
if unicode.IsDigit(v) || unicode.IsLetter(v) {
n++
}
}
return
}
// CountAlnumDistribution count distribution of alpha-numeric characters in
// text.
//
// Example, given a text "abbcccddddeeeee", it will return [a b c d e] and
// [1 2 3 4 5].
func CountAlnumDistribution(text string) (chars []rune, counts []int) {
var found bool
for _, v := range text {
if !(unicode.IsDigit(v) || unicode.IsLetter(v)) {
continue
}
found = false
for y, c := range chars {
if v == c {
counts[y]++
found = true
break
}
}
if !found {
chars = append(chars, v)
counts = append(counts, 1)
}
}
return
}
// CountCharSequence given a string, count number of repeated character more
// than one in sequence and return list of characters and their counts.
func CountCharSequence(text string) (chars []rune, counts []int) {
var lastv rune
count := 1
for _, v := range text {
if v == lastv {
if !unicode.IsSpace(v) {
count++
}
} else {
if count > 1 {
chars = append(chars, lastv)
counts = append(counts, count)
count = 1
}
}
lastv = v
}
if count > 1 {
chars = append(chars, lastv)
counts = append(counts, count)
}
return
}
// CountDigit return number of digit in text.
func CountDigit(text string) (n int) {
if len(text) == 0 {
return 0
}
for _, v := range text {
if unicode.IsDigit(v) {
n++
}
}
return
}
// CountNonAlnum return number of non alpha-numeric character in text.
// If `withspace` is true, it will be counted as non-alpha-numeric, if it
// false it will be ignored.
func CountNonAlnum(text string, withspace bool) (n int) {
if len(text) == 0 {
return
}
for _, v := range text {
if unicode.IsDigit(v) || unicode.IsLetter(v) {
continue
}
if unicode.IsSpace(v) {
if withspace {
n++
}
continue
}
n++
}
return
}
// CountUniqChar count number of character in text without duplication.
func CountUniqChar(text string) (n int) {
if len(text) == 0 {
return
}
uchars := make([]rune, 0, len(text))
for _, v := range text {
yes, _ := runes.Contain(uchars, v)
if yes {
continue
}
uchars = append(uchars, v)
n++
}
return
}
// CountUpperLower return number of uppercase and lowercase in text.
func CountUpperLower(text string) (upper, lower int) {
for _, v := range text {
if !unicode.IsLetter(v) {
continue
}
if unicode.IsUpper(v) {
upper++
} else {
lower++
}
}
return
}
// MaxCharSequence return character which have maximum sequence in `text`.
func MaxCharSequence(text string) (rune, int) {
if len(text) == 0 {
return 0, 0
}
chars, counts := CountCharSequence(text)
if len(chars) == 0 {
return 0, 0
}
_, idx, _ := ints.Max(counts)
return chars[idx], counts[idx]
}
// RatioAlnum compute and return ratio of alpha-numeric within all character
// in text.
func RatioAlnum(text string) float64 {
textlen := len(text)
if textlen == 0 {
return 0
}
n := CountAlnum(text)
return float64(n) / float64(textlen)
}
// RatioDigit compute and return digit ratio to all characters in text.
func RatioDigit(text string) float64 {
textlen := len(text)
if textlen == 0 {
return 0
}
n := CountDigit(text)
if n == 0 {
return 0
}
return float64(n) / float64(textlen)
}
// RatioUpper compute and return ratio of uppercase character to all character
// in text.
func RatioUpper(text string) float64 {
if len(text) == 0 {
return 0
}
up, lo := CountUpperLower(text)
total := up + lo
if total == 0 {
return 0
}
return float64(up) / float64(total)
}
// RatioNonAlnum return ratio of non-alphanumeric character to all
// character in text.
//
// If `withspace` is true then white-space character will be counted as
// non-alpha numeric, otherwise it will be skipped.
func RatioNonAlnum(text string, withspace bool) float64 {
textlen := len(text)
if textlen == 0 {
return 0
}
n := CountNonAlnum(text, withspace)
return float64(n) / float64(textlen)
}
// RatioUpperLower compute and return ratio of uppercase with lowercase
// character in text.
func RatioUpperLower(text string) float64 {
if len(text) == 0 {
return 0
}
up, lo := CountUpperLower(text)
if lo == 0 {
return float64(up)
}
return float64(up) / float64(lo)
}
// TextSumCountTokens given a text, count how many tokens inside of it and
// return sum of all counts.
func TextSumCountTokens(text string, tokens []string, sensitive bool) (
cnt int,
) {
if len(text) == 0 {
return 0
}
if !sensitive {
text = strings.ToLower(text)
}
for _, v := range tokens {
if !sensitive {
v = strings.ToLower(v)
}
cnt += strings.Count(text, v)
}
return
}
// TextFrequencyOfTokens return frequencies of tokens by counting each
// occurrence of token and divide it with total words in text.
func TextFrequencyOfTokens(text string, tokens []string, sensitive bool) (
freq float64,
) {
if len(text) == 0 {
return 0
}
words := Split(text, false, false)
wordsLen := float64(len(words))
tokensCnt := float64(TextSumCountTokens(text, tokens, sensitive))
freq = tokensCnt / wordsLen
return
}