-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_words.go
61 lines (48 loc) · 1.03 KB
/
filter_words.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
package main
import (
"bufio"
"log"
"os"
"github.com/anna-osipova/go-wordle/errorcheck"
)
func isPlural(list []string, item string) bool {
substr := item[:len(item)-1]
if substr+"s" != item {
return false
}
for _, listItem := range list {
if listItem == substr {
return true
}
}
return false
}
func filterWords() {
file, err := os.Open("./words.txt")
errorcheck.Check(err)
defer file.Close()
write_file, err := os.Create("./simple_words_5.txt")
errorcheck.Check(err)
defer write_file.Close()
writer := bufio.NewWriter(write_file)
scanner := bufio.NewScanner(file)
var words []string
for scanner.Scan() {
word := scanner.Text()
words = append(words, word)
}
var filteredWords []string
for _, word := range words {
if len(word) == 5 && !isPlural(words, word) {
filteredWords = append(filteredWords, word)
}
}
for _, word := range filteredWords {
_, err := writer.WriteString(word + "\n")
errorcheck.Check(err)
}
writer.Flush()
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}