-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
102 lines (83 loc) · 1.84 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
package main
import (
"encoding/csv"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
"stopwords/stopwords"
)
//Word type struct defined.
type Word struct {
key string
value int
}
func wordCount(str string) map[string]int {
wordList := strings.Fields(str)
wordCounts := make(map[string]int)
for _, word := range wordList {
cleanWord := cleanText(strings.ToLower(word))
found := checkStopWords(cleanWord)
if !found {
wordCounts[cleanWord]++
}
}
return wordCounts
}
func getText(url string) string {
resp, err := http.Get(url)
checkError("Cannot get url content", err)
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
return string(body)
}
func cleanText(text string) string {
reg, err := regexp.Compile("[^a-zA-Z0-9]+")
checkError("Cannot clean text", err)
proccessedWord := reg.ReplaceAllString(text, "")
return proccessedWord
}
func checkStopWords(word string) bool {
for _, item := range stopwords.StopWords() {
if item == word {
return true
}
}
return false
}
func sortedWords(words map[string]int) []Word {
var sorted []Word
for k, v := range words {
sorted = append(sorted, Word{k, v})
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].value > sorted[j].value
})
return sorted
}
func saveCsvResults(words map[string]int) {
file, err := os.Create("./word_frequencies_report.csv")
checkError("Cannot create file", err)
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
for _, word := range sortedWords(words) {
row := []string{word.key, strconv.Itoa(word.value)}
err := writer.Write(row)
checkError("Cannot write to file", err)
}
}
func checkError(message string, err error) {
if err != nil {
log.Fatal(message, err)
}
}
func main() {
content := getText(os.Args[1])
words := wordCount(content)
saveCsvResults(words)
}