Skip to content

Commit

Permalink
Add sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Hafsaoui committed Nov 9, 2024
1 parent 8f60acc commit 78f7aaf
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/core/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package core

import (
"log/slog"
"sort"
"strings"
)

type CVService struct{}
Expand Down Expand Up @@ -32,3 +34,20 @@ func (g *CVService) GenerateTemplates(root string, source Source, templateReader
}
return nil
}

//Sorte a slice of items by the number of keyword
func sortByScore(items []string, keywords []string){
sort.Slice(items, func(i, j int) bool {
return getScore(items[i],keywords)<getScore(items[j],keywords)
})
}


//take and item and a list of keyword and return the number of keyword inside the item
func getScore(item string, keywords []string)int{
score := 0
for _,keyword := range keywords{
score += strings.Count(item, keyword)
}
return score
}
61 changes: 61 additions & 0 deletions internal/core/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,64 @@ func TestGenerateTemplates(t *testing.T) {
t.Errorf("expected generated template content %v, got %v", expectedContent, generatedTemplate)
}
}

func TestGetScore(t *testing.T) {
tests := []struct {
item string
keywords []string
expected int
}{
{item: "foo bar baz", keywords: []string{"foo", "bar", "baz"}, expected: 3},
{item: "foo qux", keywords: []string{"foo", "bar", "baz"}, expected: 1},
{item: "patate douce", keywords: []string{"foo", "bar", "baz"}, expected: 0},
{item: "Foo Bar Baz", keywords: []string{"foo", "bar", "baz"}, expected: 0},
{item: "foo bar baz", keywords: []string{}, expected: 0},
{item: "", keywords: []string{"foo", "bar", "baz"}, expected: 0},
{item: "foo bar foo baz", keywords: []string{"foo", "foo bar"}, expected: 3},
}

for _, tt := range tests {
t.Run(tt.item, func(t *testing.T) {
score := getScore(tt.item, tt.keywords)
if score != tt.expected {
t.Errorf("for item=%q and keywords=%v, expected %d, got %d", tt.item, tt.keywords, tt.expected, score)
}
})
}
}


func TestSortByScore(t *testing.T) {
keywords := []string{"foo", "bar"}
tests := []struct {
items []string
expected []string
}{
{
items: []string{" with foo", " with foo and bar", " with neither", " with bar"},
expected: []string{" with neither", " with foo", " with bar", " with foo and bar"},
},

{
items: []string{" with foo bar foo bar", " with foo", " with bar bar bar", " with foo bar"},
expected: []string{" with foo", " with foo bar", " with bar bar bar", " with foo bar foo bar"},
},
{
items: []string{" without keywords", " with bar", "Another without keywords"},
expected: []string{" without keywords", "Another without keywords", " with bar"},
},

}

for _, tt := range tests {
items := make([]string, len(tt.items))
copy(items, tt.items)
sortByScore(items, keywords)

for i := range items {
if items[i] != tt.expected[i] {
t.Errorf("after sorting, expected %v but got %v", tt.expected, items)
}
}
}
}

0 comments on commit 78f7aaf

Please sign in to comment.