-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.go
109 lines (94 loc) · 2.08 KB
/
base.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
package main
import (
"errors"
"io/ioutil"
"os"
"sort"
"strings"
"unicode/utf8"
)
type wordsBase struct {
items []string
charsFreq map[rune]int // в скольки словах встречается каждая буква
itemFreqIndexes map[string]int // сумма частот букв для каждого слова
}
func loadBase(fileName string) (*wordsBase, error) {
b, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
text := normalizeWord(string(b))
words := strings.Split(text, "\n")
for i := range words {
words[i] = strings.TrimSpace(words[i])
if wlen := utf8.RuneCountInString(words[i]); wlen != wordLen {
return nil, wordLenError(words[i])
}
}
base := &wordsBase{
items: words,
charsFreq: make(map[rune]int),
itemFreqIndexes: make(map[string]int),
}
if len(base.items) == 0 {
return nil, errors.New("в файле нет ни одного слова")
}
wordChars := newCharSet()
for _, word := range base.items {
wordChars.clear()
for _, c := range word {
if !wordChars.has(c) {
base.charsFreq[c]++
wordChars.add(c)
}
}
}
for _, word := range base.items {
wordChars.clear()
for _, c := range word {
if !wordChars.has(c) {
base.itemFreqIndexes[word] += base.charsFreq[c]
wordChars.add(c)
}
}
}
return base, nil
}
func (b *wordsBase) count() int {
return len(b.items)
}
func (b *wordsBase) hasWord(word string) bool {
for _, w := range b.items {
if w == word {
return true
}
}
return false
}
func (b *wordsBase) removeWord(word string) bool {
for i := range b.items {
if b.items[i] == word {
l := len(b.items)
copy(b.items[i:l-1], b.items[i+1:l])
b.items = b.items[:l-1]
return true
}
}
return false
}
func (b *wordsBase) save(fileName string) error {
f, err := os.Create(fileName)
if err != nil {
return err
}
defer f.Close()
words := make([]string, b.count())
copy(words, b.items)
sort.Strings(words)
_, err = f.WriteString(strings.Join(words, "\r\n"))
if err != nil {
os.Remove(fileName)
return err
}
return nil
}