Golang_HomeWork/hw03_frequency_analysis/top.go

43 lines
833 B
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package hw03_frequency_analysis //nolint:golint,stylecheck
import (
"regexp"
"strings"
)
func Top10(str string) []string {
template := regexp.MustCompile(`[A-Za-zА-Яа-я\-]+`)
tmpArr := template.FindAllString(str, -1)
tmpMap := calculate(tmpArr)
var result []string
for i := 0; i < 10; i++ {
count := 0
word := ""
for tmpWord, tmpCount := range tmpMap {
if tmpCount > count {
count = tmpCount
word = tmpWord
}
}
if word != "" {
result = append(result, word)
}
delete(tmpMap, word)
}
return result
}
func calculate(arr []string) map[string]int {
result := make(map[string]int)
for _, v := range arr {
if v != "" && v != "-" {
_, ok := result[strings.ToLower(v)]
if ok {
result[strings.ToLower(v)]++
} else {
result[strings.ToLower(v)] = 1
}
}
}
return result
}