This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
/
words_stats.go
168 lines (143 loc) · 3.32 KB
/
words_stats.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
// Copyright 2016-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 tekstus
import (
"github.com/shuLhan/numerus"
"strings"
)
//
// WordsCountToken will return number of token occurence in words.
//
func WordsCountToken(words []string, token string, sensitive bool) (cnt int) {
if !sensitive {
token = strings.ToLower(token)
}
for _, v := range words {
if !sensitive {
v = strings.ToLower(v)
}
if v == token {
cnt++
}
}
return
}
//
// WordsCountTokens count number of occurrence of each `tokens` values in words.
// Return number of each tokens based on their index.
//
// For example, if words is "[A,A,B]" and tokens is "[A,B]", this function
// will return "[2,1]".
//
// idx cls count
// 0 : A -> 2
// 1 : B -> 1
//
func WordsCountTokens(words []string, tokens []string, sensitive bool) (
clsCnt []int,
) {
tokenslen := len(tokens)
if tokenslen <= 0 {
return
}
clsCnt = make([]int, tokenslen)
for k, v := range tokens {
clsCnt[k] = WordsCountToken(words, v, sensitive)
}
return
}
//
// WordsFrequencyOf return frequency of token in words using
//
// count-of-token / total-words
//
func WordsFrequencyOf(words []string, token string, sensitive bool) float64 {
wordslen := float64(len(words))
if wordslen <= 0 {
return 0
}
cnt := WordsCountToken(words, token, sensitive)
return float64(cnt) / wordslen
}
//
// WordsFrequenciesOf return total frequency of tokens in words.
//
func WordsFrequenciesOf(words, tokens []string, sensitive bool) (
sumfreq float64,
) {
if len(words) <= 0 || len(tokens) <= 0 {
return 0
}
for _, token := range tokens {
sumfreq += WordsFrequencyOf(words, token, sensitive)
}
return
}
//
// WordsProbabilitiesOf will compute each probability of token in word, and
// return it as a slice of float.
//
// Example,
//
// words: ["A", "B", "A"]
// tokens:["A", "B"]
//
// It will return: [0.6, 0.3].
//
func WordsProbabilitiesOf(words, tokens []string, sensitive bool) (
probs []float64,
) {
probs = make([]float64, len(tokens))
for x, token := range tokens {
probs[x] = WordsFrequencyOf(words, token, sensitive)
}
return probs
}
//
// WordsMaxCountOf return the string that has highest frequency.
//
// Example, given input
//
// words: [A A B A B C C]
// tokens: [A B]
//
// it will return A as the majority tokens in words.
// If tokens has equal frequency, then the first tokens in order will returned.
//
func WordsMaxCountOf(words []string, tokens []string, sensitive bool) string {
if len(words) <= 0 {
return ""
}
tokensCount := WordsCountTokens(words, tokens, sensitive)
_, maxIdx, ok := numerus.IntsFindMax(tokensCount)
if !ok {
return ""
}
return tokens[maxIdx]
}
//
// WordsCountMissRate given two slice of string, count number of string that is
// not equal with each other, and return the miss rate as
//
// number of not equal / number of data
//
// missing count, and length of input `src`.
//
func WordsCountMissRate(src []string, target []string) (
missrate float64,
nmiss, length int,
) {
// find minimum length
length = len(src)
targetlen := len(target)
if targetlen < length {
length = targetlen
}
for x := 0; x < length; x++ {
if src[x] != target[x] {
nmiss++
}
}
return float64(nmiss) / float64(length), nmiss, length
}