-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
102 lines (91 loc) · 2.38 KB
/
main.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
// Count the total and unique number of words in a text, prints their frequencies.
package main
import (
"fmt"
"io/ioutil"
"runtime"
"strconv"
"strings"
"github.com/shurcooL/go/gists/gist5092053"
)
func main() {
filepath := thisGoSourceFile()
//filepath := "/Users/Dmitri/Dropbox/Work/2013/yelp/Yelp/Gen/5star_text.txt"
Process(filepath)
}
func Process(filepath string) {
b, err := ioutil.ReadFile(filepath)
if err != nil {
panic(err)
}
if true {
// Prints frequencies of individual words
//w := strings.Fields(strings.ToLower(string(b)))
w := strings.FieldsFunc(strings.ToLower(string(b)), func(r rune) bool {
if r >= 'a' && r <= 'z' {
return false
}
return true
})
fmt.Printf("Total words: %v\n", len(w))
m := map[string]int{}
for _, v := range w {
m[v]++
}
fmt.Printf("Total unique words: %v\n\n", len(m))
sm := gist5092053.SortMapByValue(m)
//for i := len(sm) - 1; i >= 0; i-- { v := sm[i]
for _, v := range sm {
x := float64(v.Value) / float64(len(w)) * 100
fmt.Printf("%v\t%v%%\t%v\n", v.Value, strconv.FormatFloat(x, 'f', 5, 64), v.Key)
}
} else if false {
// 2-rune Markov chain
runes := []rune(string(b))
fmt.Printf("Total words: %v\n", len(runes)-1)
m := map[string]int{}
for i := 0; i < len(runes)-1; i++ {
x := string(runes[i]) + string(runes[i+1])
//fmt.Println(x)
m[x]++
}
fmt.Printf("Total unique words: %v\n\n", len(m))
sm := gist5092053.SortMapByValue(m)
for _, v := range sm {
fmt.Printf("%v\t%#v\n", v.Value, v.Key)
}
} else {
// 2-rune Markov chain: Given "y", this prints the occurrence of "xy" for all "x"
runes := []rune(string(b))
fmt.Printf("Total words: %v\n\n", len(runes)-1)
m := map[string]map[string]int{}
for i := 0; i < len(runes)-1; i++ {
add(m, string(runes[i]), string(runes[i+1]))
}
target := "\n"
{
total := 0
for _, v := range m[target] {
total += v
}
fmt.Printf("Total hits for %#v: %v\n", target, total)
}
sm := gist5092053.SortMapByValue(m[target])
for _, v := range sm {
fmt.Printf("%v\t%#v\n", v.Value, v.Key)
}
}
}
func add(m map[string]map[string]int, r1, r2 string) {
mm, ok := m[r1]
if !ok {
mm = make(map[string]int)
m[r1] = mm
}
mm[r2]++
}
// thisGoSourceFile returns the full path of the Go source file where this function was called from.
func thisGoSourceFile() string {
_, file, _, _ := runtime.Caller(1)
return file
}