Skip to content

Commit 2701eb6

Browse files
committed
Encoding
1 parent 4c26dc5 commit 2701eb6

7 files changed

Lines changed: 503 additions & 321 deletions

File tree

secretary/utils/encode/freq_test.go

Lines changed: 78 additions & 226 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,63 @@ import (
88
"os"
99
"path/filepath"
1010
"sort"
11-
"unicode"
11+
"testing"
12+
13+
"github.com/codeharik/secretary/utils"
14+
)
15+
16+
var (
17+
SECKEYMAP []SECKEY
18+
ASCIIINDEX [256]byte
19+
)
20+
21+
type FreqType int
22+
23+
const (
24+
SNo FreqType = 0
25+
S16 = 16
26+
S32 = 32
27+
S64 = 64
28+
)
29+
30+
const (
31+
freq FreqType = S32
32+
SERVER = false
1233
)
1334

35+
func TestFreq(t *testing.T) {
36+
if freq == S16 {
37+
SECKEYMAP = SEC16KeyMap[:]
38+
ASCIIINDEX = ASCII16Index
39+
} else if freq == S32 {
40+
SECKEYMAP = SEC32KeyMap[:]
41+
ASCIIINDEX = ASCII32Index
42+
} else if freq == S64 {
43+
SECKEYMAP = SEC64KeyMap[:]
44+
ASCIIINDEX = ASCII64Index
45+
}
46+
47+
sortedMap()
48+
49+
if SERVER {
50+
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
51+
w.Write([]byte(html))
52+
})
53+
54+
http.HandleFunc("/frequency", func(w http.ResponseWriter, r *http.Request) {
55+
w.Header().Set("Content-Type", "application/json")
56+
json.NewEncoder(w).Encode(sortedMap())
57+
})
58+
59+
fmt.Println("Server running at http://localhost:8080")
60+
http.ListenAndServe(":8080", nil)
61+
}
62+
}
63+
1464
// Count frequencies with phonetic grouping
15-
func countASCII(root string) map[byte]int {
16-
freq := make(map[byte]int)
65+
func countASCII(root string) ([]int, []int) {
66+
groupfreq := make([]int, freq)
67+
charfreq := make([]int, 256)
1768

1869
_ = filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
1970
if err != nil {
@@ -40,23 +91,25 @@ func countASCII(root string) map[byte]int {
4091
if n == 0 {
4192
break
4293
}
43-
for _, b := range buf[:n] {
44-
if b < 128 { // ASCII only
45-
char := unicode.ToLower(rune(b))
94+
for _, char := range buf[:n] {
95+
if char < 128 { // ASCII only
4696

47-
freq[ASCII32Index[char]]++
97+
group := ASCIIINDEX[char]
98+
groupfreq[group]++
99+
100+
charfreq[char]++
48101
}
49102
}
50103
}
51104
return nil
52105
})
53106

54-
return freq
107+
return groupfreq, charfreq
55108
}
56109

57110
func sortedMap() map[string]int {
58111
root := "../../"
59-
frequencies := countASCII(root)
112+
groupfreq, charfreq := countASCII(root)
60113

61114
// Convert map to slice for sorting
62115
type kv struct {
@@ -67,9 +120,21 @@ func sortedMap() map[string]int {
67120

68121
totalChar := 0
69122

70-
for k, v := range frequencies {
71-
sortedFreq = append(sortedFreq, kv{Char: fmt.Sprintf("%2d %16q", k, string(SEC32KeyMap[k])), Count: v})
72-
totalChar += v
123+
for _, groupcount := range groupfreq {
124+
totalChar += groupcount
125+
}
126+
127+
for groupkey, groupcount := range groupfreq {
128+
129+
mmm := utils.Map(SECKEYMAP[groupkey].keys, func(a byte) string {
130+
return fmt.Sprintf("%q,%d,%d", a, 100*charfreq[a]/groupcount, 100*charfreq[a]/totalChar)
131+
})
132+
133+
sortedFreq = append(sortedFreq,
134+
kv{
135+
Char: fmt.Sprintf("%8d %4d %8v", groupcount, groupkey, mmm),
136+
Count: groupcount,
137+
})
73138
}
74139

75140
// Sort by frequency (descending)
@@ -86,28 +151,11 @@ func sortedMap() map[string]int {
86151

87152
charCount += kv.Count
88153

89-
fmt.Printf("%4d %7v %9v %4d %4d\n", i, kv.Char, kv.Count, 100*charCount/totalChar, 100*kv.Count/totalChar)
154+
fmt.Printf("%4d %4d %4d %10v \n", i, 100*charCount/totalChar, 100*kv.Count/totalChar, kv.Char)
90155
}
91156
return sortedMap
92157
}
93158

94-
func frequencyHandler(w http.ResponseWriter, r *http.Request) {
95-
w.Header().Set("Content-Type", "application/json")
96-
json.NewEncoder(w).Encode(sortedMap())
97-
}
98-
99-
// func TestMain(t *testing.T) {
100-
// sortedMap()
101-
102-
// http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
103-
// w.Write([]byte(html))
104-
// }) // API endpoint
105-
// http.HandleFunc("/frequency", frequencyHandler) // API endpoint
106-
107-
// fmt.Println("Server running at http://localhost:8080")
108-
// http.ListenAndServe(":8080", nil)
109-
// }
110-
111159
var html = `<!DOCTYPE html>
112160
<html lang="en">
113161
@@ -152,199 +200,3 @@ var html = `<!DOCTYPE html>
152200
</body>
153201
154202
</html>`
155-
156-
/*
157-
158-
0 ' ' 32 224434 9
159-
1 ',' 44 115891 15
160-
2 'e' 101 90954 19
161-
3 '0' 48 88983 23
162-
4 '\n' 10 79910 26
163-
5 't' 116 77762 30
164-
6 '2' 50 74192 33
165-
7 'i' 105 68819 36
166-
8 '\t' 9 65162 39
167-
9 '3' 51 64568 42
168-
10 's' 115 64344 45
169-
11 'n' 110 61169 47
170-
12 '1' 49 60069 50
171-
13 'r' 114 56912 53
172-
14 'a' 97 56274 55
173-
15 'l' 108 48418 57
174-
16 '5' 53 46883 59
175-
17 'o' 111 46422 61
176-
18 'c' 99 45621 64
177-
19 '4' 52 44040 65
178-
20 'd' 100 41524 67
179-
21 '6' 54 40700 69
180-
22 'f' 102 40551 71
181-
23 '_' 95 37853 73
182-
24 '8' 56 36760 74
183-
25 '7' 55 35389 76
184-
26 '9' 57 34599 77
185-
27 'u' 117 30030 79
186-
28 ')' 41 29131 80
187-
29 '(' 40 29079 81
188-
30 'p' 112 28625 83
189-
31 '=' 61 26891 84
190-
32 'x' 120 25945 85
191-
33 ';' 59 25008 86
192-
34 '*' 42 24708 87
193-
35 'm' 109 23479 88
194-
36 'h' 104 21306 89
195-
37 '.' 46 17628 90
196-
38 'b' 98 17628 91
197-
39 'g' 103 16661 91
198-
40 '-' 45 16113 92
199-
41 '\x00' 0 14173 93
200-
42 'y' 121 11987 93
201-
43 '/' 47 11130 94
202-
44 '>' 62 9455 94
203-
45 'v' 118 9052 95
204-
46 'w' 119 9034 95
205-
47 '+' 43 8586 95
206-
48 '#' 35 7752 96
207-
49 'k' 107 7298 96
208-
50 '{' 123 7285 96
209-
51 '}' 125 7281 97
210-
52 '"' 34 5979 97
211-
53 '<' 60 5758 97
212-
54 'z' 122 5574 98
213-
55 '\\' 92 5341 98
214-
56 '[' 91 5131 98
215-
57 ']' 93 5129 98
216-
58 '&' 38 4880 98
217-
59 ':' 58 3563 99
218-
60 '\'' 39 3369 99
219-
61 '|' 124 3135 99
220-
62 '%' 37 3087 99
221-
63 'q' 113 2212 99
222-
64 '!' 33 2083 99
223-
65 'j' 106 1850 99
224-
66 '$' 36 1618 99
225-
67 '?' 63 601 99
226-
68 '^' 94 573 99
227-
69 '@' 64 519 99
228-
70 '~' 126 260 99
229-
71 '\x01' 1 138 99
230-
72 '\b' 8 133 99
231-
73 '`' 96 123 99
232-
74 '\x04' 4 71 99
233-
75 '\x06' 6 58 99
234-
76 '\x05' 5 40 99
235-
77 '\x02' 2 34 99
236-
78 '\x03' 3 32 99
237-
79 '\a' 7 31 99
238-
80 '\x10' 16 25 99
239-
81 '\v' 11 9 99
240-
82 '\x14' 20 4 99
241-
83 '\f' 12 4 99
242-
84 '\r' 13 3 99
243-
85 '\x1d' 29 2 99
244-
86 '\x1b' 27 2 99
245-
87 '\x1f' 31 2 99
246-
88 '\x1a' 26 2 99
247-
89 '\x0f' 15 1 99
248-
90 '\x15' 21 1 99
249-
91 '\x18' 24 1 99
250-
92 '\x11' 17 1 99
251-
93 '\x16' 22 1 99
252-
94 '\x19' 25 1 100
253-
254-
255-
256-
257-
0 ' ' 32 15827074 17
258-
1 'e' 101 8521608 26
259-
2 't' 116 5995059 33
260-
3 'a' 97 5634185 39
261-
4 'o' 111 4959336 44
262-
5 'n' 110 4620222 49
263-
6 'h' 104 4470892 54
264-
7 'i' 105 4354779 59
265-
8 's' 115 4164566 63
266-
9 'r' 114 4047746 68
267-
10 'd' 100 3386714 72
268-
11 'l' 108 2931460 75
269-
12 'u' 117 1862109 77
270-
13 'w' 119 1662748 79
271-
14 'm' 109 1623024 80
272-
15 'g' 103 1581262 82
273-
16 'c' 99 1525031 84
274-
17 'f' 102 1410620 85
275-
18 'y' 121 1278989 87
276-
19 '\x00' 0 1242402 88
277-
20 ',' 44 1172181 89
278-
21 '.' 46 1122071 91
279-
22 'b' 98 1053480 92
280-
23 'p' 112 1048503 93
281-
24 '\n' 10 901311 94
282-
25 '\r' 13 805623 95
283-
26 'k' 107 687854 95
284-
27 'v' 118 596802 96
285-
28 '"' 34 589817 97
286-
29 '\'' 39 266935 97
287-
30 '-' 45 146654 97
288-
31 '_' 95 137890 97
289-
32 'x' 120 125445 97
290-
33 '1' 49 108131 98
291-
34 'z' 122 103677 98
292-
35 '0' 48 99303 98
293-
36 '?' 63 93497 98
294-
37 '\x01' 1 90709 98
295-
38 '2' 50 86841 98
296-
39 'j' 106 86222 98
297-
40 '\t' 9 78048 98
298-
41 '3' 51 70327 98
299-
42 ':' 58 67016 98
300-
43 ';' 59 61595 98
301-
44 '!' 33 61315 99
302-
45 'q' 113 55944 99
303-
46 '5' 53 54857 99
304-
47 '4' 52 51946 99
305-
48 '6' 54 48144 99
306-
49 '8' 56 44632 99
307-
50 '7' 55 44525 99
308-
51 '9' 57 43274 99
309-
52 '(' 40 37249 99
310-
53 '=' 61 36726 99
311-
54 ')' 41 35620 99
312-
55 '*' 42 33342 99
313-
56 '>' 62 29395 99
314-
57 '<' 60 25531 99
315-
58 '\x02' 2 23263 99
316-
59 '$' 36 21742 99
317-
60 '\x05' 5 16554 99
318-
61 '\x03' 3 16129 99
319-
62 '/' 47 14509 99
320-
63 '\x04' 4 14109 99
321-
64 '\b' 8 12232 99
322-
65 '\x13' 19 11834 99
323-
66 '#' 35 10697 99
324-
67 '+' 43 10663 99
325-
68 '\x10' 16 10592 99
326-
69 '@' 64 9604 99
327-
70 '}' 125 9503 99
328-
71 '{' 123 9050 99
329-
72 '\\' 92 8428 99
330-
73 ']' 93 8132 99
331-
74 '\x12' 18 7844 99
332-
75 '\x06' 6 7762 99
333-
76 '[' 91 7626 99
334-
77 '&' 38 7145 99
335-
78 '\x0e' 14 7035 99
336-
79 '|' 124 6258 99
337-
80 '\v' 11 6181 99
338-
81 '\x0f' 15 5676 99
339-
82 '%' 37 5664 99
340-
83 '`' 96 5166 99
341-
84 '\a' 7 4880 99
342-
85 '^' 94 4647 99
343-
86 '\x14' 20 4531 99
344-
87 '\x18' 24 4347 99
345-
88 '\x1c' 28 4162 99
346-
89 '\x1a' 26 3624 99
347-
90 '\x11' 17 3385 99
348-
91 '\x19' 25 3336 99
349-
350-
*/

0 commit comments

Comments
 (0)