-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
main.go
66 lines (63 loc) · 2.57 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
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
func count(r io.Reader) (map[string](int), string, int) {
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanWords)
words := map[string](int){}
largestWord := ""
numWords := 0
for scanner.Scan() {
wordsOrWord := strings.ToLower(scanner.Text())
wordsOrWord = strings.Replace(wordsOrWord, "-", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "/", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "*", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, ".", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, ",", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "\"", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "'", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "!", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "?", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "_", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "=", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "+", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "&", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "%", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "^", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "(", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, ")", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "$", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "<", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, ">", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "{", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "}", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "[", " ", -1)
wordsOrWord = strings.Replace(wordsOrWord, "]", " ", -1)
for _, word := range strings.Fields(wordsOrWord) {
size := len(word)
words[word]++
numWords++
if size > len(largestWord) {
largestWord = word
}
}
}
return words, largestWord, numWords
}
func main() {
message, _ := os.Open("moby10b.txt")
words, largestWord, numWords := count(message)
fmt.Println("Number of the word \"the\":", words["the"])
fmt.Println("Number of the word \"and\":", words["and"])
fmt.Println("Number of the word \"whale\":", words["whale"])
fmt.Println("Number of the word \"try\":", words["try"])
fmt.Println("Number of the word \"succeed\":", words["succeed"])
fmt.Printf("There are %d unique words in the document\n", len(words))
fmt.Printf("There are %d total words in the document\n", numWords)
fmt.Printf("The largest word is \"%s\", with a length of %d which appears %d times\n", largestWord, len(largestWord), words[largestWord])
}