2020-06-15 12:42:58 +03:00

43 lines
833 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}