From 71830a62d650c55bce08899d3a90c715835b4023 Mon Sep 17 00:00:00 2001 From: DimVlas Date: Tue, 23 Apr 2024 21:58:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D1=8B=D0=B9=20=D0=BA=D0=BE=D0=BC=D0=BC?= =?UTF-8?q?=D0=B8=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw03_frequency_analysis/top.go | 59 +++++++++++++++++------------ hw03_frequency_analysis/top_test.go | 48 ++++++++++++----------- 2 files changed, 60 insertions(+), 47 deletions(-) diff --git a/hw03_frequency_analysis/top.go b/hw03_frequency_analysis/top.go index 6e17769..d55554d 100644 --- a/hw03_frequency_analysis/top.go +++ b/hw03_frequency_analysis/top.go @@ -7,7 +7,7 @@ import ( ) // Разбивает текст на слова, возвращает срез слов. -func GetWords(text string) []string { +func splitWords(text string) []string { if len(text) < 1 { return []string{} } @@ -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 @@ -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 diff --git a/hw03_frequency_analysis/top_test.go b/hw03_frequency_analysis/top_test.go index 684559d..c8a9dab 100644 --- a/hw03_frequency_analysis/top_test.go +++ b/hw03_frequency_analysis/top_test.go @@ -7,7 +7,7 @@ import ( ) // Change to true if needed. -var taskWithAsteriskIsCompleted = false +var taskWithAsteriskIsCompleted = true var text = `Как видите, он спускается по лестнице вслед за своим другом Кристофером Робином, головой вниз, пересчитывая @@ -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{ "Предложения", "складываются", "в", "абзацы", - "—", + "-", "и", "вы", "наслаждетесь", @@ -108,42 +110,42 @@ 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}, }, }, } @@ -151,7 +153,7 @@ func TestCountOfElements(t *testing.T) { 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) }) }