-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
157 lines (128 loc) · 3.11 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
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
package main
import (
"bufio"
"fmt"
"io/fs"
"io/ioutil"
"log"
"os"
"sort"
"strings"
"unicode"
)
func main() {
// Open the folder and read its contents
files, err := ioutil.ReadDir("files")
if err != nil {
log.Fatalln("Error reading directory:", err)
return
}
// Read the words in each file
m, err := readWords(files)
if err != nil {
log.Fatalln("Error reading files:", err)
return
}
// Print the quantity of words from all of the files
fmt.Println(len(m))
// Sort the words
xw := sortWords(m)
// Write the words to a txt file
writeWords(xw)
}
// readWords reads all of the words
func readWords(ff []fs.FileInfo) (map[string]int, error) {
// Create a map for this specific file
words := make(map[string]int)
// Loop through each file in the folder
for _, file := range ff {
filename := file.Name()
// Process only .txt files
if strings.HasSuffix(filename, ".txt") {
// Open the file for reading
f, err := os.Open("files/" + filename)
if err != nil {
fmt.Println("Error opening file:", err)
return words, err
}
// Create a new scanner and scan the file line by line
scanner := bufio.NewScanner(f)
// Split all of the text into words
scanner.Split(bufio.ScanWords)
// Loop through all of the words
for scanner.Scan() {
word := scanner.Text()
xw := strings.Split(word, ",")
for _, w := range xw {
w = cleanWord(w)
// Add word to map
if w != "" {
words[w]++
}
}
}
// Check for errors during scanning
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
return words, err
}
// Close the file
f.Close()
}
}
return words, nil
}
// SortWords puts the words into a slice and sorts them
func sortWords(m map[string]int) []string {
// Extract words into a slice
words := make([]string, 0, len(m))
for k := range m {
words = append(words, k)
}
// Sort the words
sort.Strings(words)
return words
}
func writeWords(xw []string) {
// Create or open a file
file, err := os.Create("all-words.txt")
if err != nil {
fmt.Println("Error:", err)
return
}
defer file.Close()
// Create a buffered writer for better performance
writer := bufio.NewWriter(file)
// Write each word to the file
for _, word := range xw {
_, err := writer.WriteString(word + "\n")
if err != nil {
fmt.Println("Error:", err)
return
}
}
// Flush the buffer to ensure all data gets written to file
writer.Flush()
}
// CleanWord removes digits from the word
func cleanWord(word string) string {
word = strings.ToLower(word)
// Remove leading and trailing undesired characters
word = strings.Trim(word, "*.,!?\"'()[]{}:;#&+-/=$%<>@_|~")
// Remove an leading and trailing spaces
word = strings.TrimSpace(word)
// Discard word with undesired characters
xs := []string{"\a", "\b", "\f", "\n", "\r", "\t", "\v", "\\", "\"", "-", ".", "=", "'", "`", "?", "$", "’", "“", "‘", "–"}
for _, v := range xs {
if strings.Contains(word, v) {
return ""
}
}
// Discard word with numeric digits
for _, r := range word {
if unicode.IsDigit(r) {
return ""
}
}
return word
}