From 2979a9a275ec5ce5300c3a1946c2a728b699eb7c Mon Sep 17 00:00:00 2001 From: sundowndev Date: Tue, 14 Apr 2020 12:25:03 +0100 Subject: [PATCH] feat(engine): sort query results by score --- cmd/query.go | 5 ++-- engine/results.go | 14 ++++++++-- engine/results_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 engine/results_test.go diff --git a/cmd/query.go b/cmd/query.go index a54c309..e858ddf 100644 --- a/cmd/query.go +++ b/cmd/query.go @@ -41,6 +41,7 @@ var queryCmd = &cobra.Command{ for _, file := range files { score := client.GetWordScoreFromFile(file, word) + // If score is 0 then ignore it if score == 0 { continue } @@ -54,11 +55,11 @@ var queryCmd = &cobra.Command{ queryResults = append(queryResults, &engine.QueryResult{ File: file, - Count: score, + Score: score, FirstMatch: engine.GetFirstMatchingLine(text, word), }) } - engine.ShowResults(queryResults) + engine.ShowResults(engine.SortResultsByScore(queryResults)) }, } diff --git a/engine/results.go b/engine/results.go index ea75247..92b87ec 100644 --- a/engine/results.go +++ b/engine/results.go @@ -3,6 +3,7 @@ package engine import ( "fmt" "os" + "sort" "text/tabwriter" ) @@ -12,7 +13,7 @@ const tabPaddingChar = ' ' // QueryResult defines the structure of a query result type QueryResult struct { File string - Count float64 + Score float64 FirstMatch string } @@ -23,6 +24,15 @@ func ShowResults(results []*QueryResult) { fmt.Fprintln(w, "File\tCount\tFirst match\t") for _, v := range results { - fmt.Fprintf(w, "%v\t%v\t%v\t\n", v.File, v.Count, v.FirstMatch) + fmt.Fprintf(w, "%v\t%v\t%v\t\n", v.File, v.Score, v.FirstMatch) } } + +// SortResultsByScore sort the given array of results by their score. +func SortResultsByScore(results []*QueryResult) []*QueryResult { + sort.Slice(results, func(i, j int) bool { + return results[i].Score > results[j].Score + }) + + return results +} diff --git a/engine/results_test.go b/engine/results_test.go new file mode 100644 index 0000000..6b0a220 --- /dev/null +++ b/engine/results_test.go @@ -0,0 +1,63 @@ +package engine + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestResults(t *testing.T) { + assert := assert.New(t) + + t.Run("SortResultsByScore", func(t *testing.T) { + results := []*QueryResult{ + &QueryResult{ + File: "/file", + Score: 1, + FirstMatch: "", + }, + &QueryResult{ + File: "/file", + Score: 3, + FirstMatch: "", + }, + &QueryResult{ + File: "/file", + Score: 9, + FirstMatch: "", + }, + &QueryResult{ + File: "/file", + Score: 2, + FirstMatch: "", + }, + } + + expected := []*QueryResult{ + &QueryResult{ + File: "/file", + Score: 9, + FirstMatch: "", + }, + &QueryResult{ + File: "/file", + Score: 3, + FirstMatch: "", + }, + &QueryResult{ + File: "/file", + Score: 2, + FirstMatch: "", + }, + &QueryResult{ + File: "/file", + Score: 1, + FirstMatch: "", + }, + } + + sorted := SortResultsByScore(results) + + assert.Equal(expected, sorted, "should be equal") + }) +}