package main
import (
"fmt"
"io"
"os"
"strings"
)
func main() {
words := []string{"def", "class", "push"}
wordFreq := make(map[string]int)
readFromBigFile("/home/alex/Desktop/1.txt", words, wordFreq)
fmt.Println(wordFreq)
}
func getFreq(MainText string, words []string, wf map[string]int) {
for _, word := range words {
word = strings.ToUpper(word)
var TextForSearch string = MainText
end := true
for end {
index := strings.Index(TextForSearch, word)
if index >= 0 {
wf[word]++
TextForSearch = TextForSearch[index+len(word):]
} else {
end = false
}
}
}
}
func readFromBigFile(path string, words []string, wf map[string]int) {
initMap(words, wf)
f, err := os.Open(path)
if err != nil {
return
}
defer f.Close()
var offset int64
for {
b := make([]byte, 1024*1024*30)
n, err := f.ReadAt(b, offset)
getFreq(strings.ToUpper(string(b)), words, wf)
offset += int64(n)
if err == io.EOF {
break
}
}
}
func initMap(words []string, wf map[string]int) {
for _, word := range words {
wf[word] = 0
}
}