Skip to content

Commit

Permalink
Промежуточный коммит
Browse files Browse the repository at this point in the history
  • Loading branch information
DimVlas authored and DimVlas committed Apr 23, 2024
1 parent 17b5da1 commit 71830a6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 47 deletions.
59 changes: 35 additions & 24 deletions hw03_frequency_analysis/top.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Разбивает текст на слова, возвращает срез слов.
func GetWords(text string) []string {
func splitWords(text string) []string {
if len(text) < 1 {
return []string{}
}
Expand All @@ -16,22 +16,43 @@ func GetWords(text string) []string {
}

// Считает частоту элементов в строковом срезе.
// Возвращает map, где key элемент среза,
// а value сколько раз встречался элемент.
func CountOfElements(s []string) map[string]int {
m := make(map[string]int)
// Возвращает slice структур wordWidth - слова с их весами.
// Slice отсортирован в зависимости от веса
func wordsWidthsSort(s []string, isNotCaseSens bool) []wordWidth {
if len(s) < 1 {
return []wordWidth{}
}

m := make(map[string]wordWidth)

for _, w := range s {
if cnt, ok := m[w]; ok {
cnt++
m[w] = cnt
word := w
if isNotCaseSens {
word = strings.ToUpper(word)
}

if width, ok := m[word]; ok {
width.Width++
m[word] = width
} else {
m[w] = 1
m[word] = wordWidth{Word: w, Width: 1}
}
}
return m

ww := make([]wordWidth, 0, len(m))

for _, v := range m {
ww = append(ww, v)
}

sort.Slice(ww, func(i, j int) bool {
return ww[i].Width > ww[j].Width ||
(ww[i].Width == ww[j].Width && ww[i].Word < ww[j].Word)
})
return ww
}

// Слово с его весом
type wordWidth struct {
Word string
Width int
Expand All @@ -46,31 +67,21 @@ func Top10(text string) []string {
return []string{}
}

words := GetWords(text)
words := splitWords(text)
if len(words) < 1 {
return []string{}
}

countMap := CountOfElements(words)
countWords := make([]wordWidth, 0, len(countMap))

for k, v := range countMap {
countWords = append(countWords, wordWidth{Word: k, Width: v})
}

sort.Slice(countWords, func(i, j int) bool {
return countWords[i].Width > countWords[j].Width ||
(countWords[i].Width == countWords[j].Width && countWords[i].Word < countWords[j].Word)
})
widths := wordsWidthsSort(words, false)

res := make([]string, 0, 10)

ln := len(countWords)
ln := len(widths)
if ln > 10 {
ln = 10
}

for _, ww := range countWords[:ln] {
for _, ww := range widths[:ln] {
res = append(res, ww.Word)
}
return res
Expand Down
48 changes: 25 additions & 23 deletions hw03_frequency_analysis/top_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Change to true if needed.
var taskWithAsteriskIsCompleted = false
var taskWithAsteriskIsCompleted = true

var text = `Как видите, он спускается по лестнице вслед за своим
другом Кристофером Робином, головой вниз, пересчитывая
Expand Down Expand Up @@ -81,22 +81,24 @@ func TestTop10(t *testing.T) {
})
}

func TestGetWords(t *testing.T) {
func TestSplitWords(t *testing.T) {
tests := []struct {
name string
input string
expected []string
}{
{input: "", expected: []string{}},
{input: "alfa beta gamma ", expected: []string{"alfa", "beta", "gamma"}},
{name: "empty", input: "", expected: []string{}},
{name: "en", input: "alfa beta gamma ", expected: []string{"alfa", "beta", "gamma"}},
{
input: `Предложения складываются в абзацы —
name: "ru",
input: `Предложения складываются в абзацы -
и вы наслаждетесь очередным бредошедевром.`,
expected: []string{
"Предложения",
"складываются",
"в",
"абзацы",
"",
"-",
"и",
"вы",
"наслаждетесь",
Expand All @@ -108,50 +110,50 @@ func TestGetWords(t *testing.T) {

for _, tc := range tests {
tc := tc
t.Run(tc.input, func(t *testing.T) {
result := GetWords(tc.input)
t.Run(tc.name, func(t *testing.T) {
result := splitWords(tc.input)
require.Equal(t, tc.expected, result)
})
}
}

func TestCountOfElements(t *testing.T) {
func TestWordsWidthsSort(t *testing.T) {
tests := []struct {
name string
input []string
expected map[string]int
expected []wordWidth
}{
{
name: "empty",
input: []string{},
expected: map[string]int{},
expected: []wordWidth{},
},
{
name: "all_one",
name: "en. all_one",
input: []string{"alfa", "beta", "gamma"},
expected: map[string]int{
"alfa": 1,
"beta": 1,
"gamma": 1,
expected: []wordWidth{
{Word: "alfa", Width: 1},
{Word: "beta", Width: 1},
{Word: "gamma", Width: 1},
},
},
{
name: "second_two",
input: []string{"Мама", "мыла", "раму,", "раму", "мыла", "мама"},
expected: map[string]int{
"Мама": 1,
"мыла": 2,
"раму,": 1,
"раму": 1,
"мама": 1,
expected: []wordWidth{
{"мыла", 2},
{"Мама", 1},
{"мама", 1},
{"раму", 1},
{"раму,", 1},
},
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
result := CountOfElements(tc.input)
result := wordsWidthsSort(tc.input, !taskWithAsteriskIsCompleted)
require.Equal(t, tc.expected, result)
})
}
Expand Down

0 comments on commit 71830a6

Please sign in to comment.