forked from ardanlabs/practical-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreq.go
161 lines (136 loc) · 2.84 KB
/
freq.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
package main
// Q: What is the most common word (ignoring case) in sherlock.txt?
// Word frequency
import (
"bufio"
"fmt"
"io"
"log"
"os"
"regexp"
"strings"
)
func main() {
file, err := os.Open("sherlock.txt")
// goland: freq/sherlock.txt
if err != nil {
log.Fatalf("error: %s", err)
}
defer file.Close()
w, err := mostCommon(file)
if err != nil {
log.Fatalf("error: %s", err)
}
fmt.Println(w)
// mapDemo()
/*
// path := "C:\to\new\report.csv"
// `s` is a "raw" string, \ is just a \
path := `C:\to\new\report.csv`
fmt.Println(path)
*/
}
func mostCommon(r io.Reader) (string, error) {
freqs, err := wordFrequency(r)
if err != nil {
return "", err
}
return maxWord(freqs)
}
/* You can also use raw strings to create mutli lines strings
var request = `GET /ip HTTP/1.1
Host: httpbin.org
Connection: Close
`
*/
// "Who's on first?" -> [Who s on first]
var wordRe = regexp.MustCompile(`[a-zA-Z]+`)
/* Will run before main
func init() {
// ...
}
*/
func mapDemo() {
var stocks map[string]float64 // symbol -> price
sym := "TTWO"
price := stocks[sym]
fmt.Printf("%s -> $%.2f\n", sym, price)
if price, ok := stocks[sym]; ok {
fmt.Printf("%s -> $%.2f\n", sym, price)
} else {
fmt.Printf("%s not found\n", sym)
}
/*
stocks = make(map[string]float64)
stocks[sym] = 136.73
*/
stocks = map[string]float64{
sym: 137.73,
"AAPL": 172.35,
}
if price, ok := stocks[sym]; ok {
fmt.Printf("%s -> $%.2f\n", sym, price)
} else {
fmt.Printf("%s not found\n", sym)
}
for k := range stocks { // keys
fmt.Println(k)
}
for k, v := range stocks { // key & value
fmt.Println(k, "->", v)
}
for _, v := range stocks { // values
fmt.Println(v)
}
delete(stocks, "AAPL")
fmt.Println(stocks)
delete(stocks, "AAPL") // no panic
}
func maxWord(freqs map[string]int) (string, error) {
if len(freqs) == 0 {
return "", fmt.Errorf("empty map")
}
maxN, maxW := 0, ""
for word, count := range freqs {
if count > maxN {
maxN, maxW = count, word
}
}
return maxW, nil
}
func TryScanner(r io.Reader) {
freqs := make(map[string]int) // word -> count
err := FluentReader{r}.Scan(func(s string) bool {
words := wordRe.FindAllString(s, -1) // current line
for _, w := range words {
w = strings.ToLower(w)
freqs[w]++
}
return true
})
if err != nil {
log.Fatal(err)
}
}
type FluentReader struct{ io.Reader }
func (I FluentReader) Scan(consumer func(string) bool) error {
s := bufio.NewScanner(I.Reader)
for s.Scan() {
consumer(s.Text())
}
return s.Err()
}
func wordFrequency(r io.Reader) (map[string]int, error) {
s := bufio.NewScanner(r)
freqs := make(map[string]int) // word -> count
for s.Scan() {
words := wordRe.FindAllString(s.Text(), -1) // current line
for _, w := range words {
freqs[strings.ToLower(w)]++
}
}
if err := s.Err(); err != nil {
return nil, err
}
return freqs, nil
}