From 1876899061501c31561ec78185ab0885d3393b73 Mon Sep 17 00:00:00 2001 From: Adam Shannon Date: Mon, 13 Jan 2025 14:12:11 -0600 Subject: [PATCH] search: prevent NaN from leaking out of DebugSimilarity --- pkg/search/similarity.go | 12 ++++++++++++ pkg/search/similarity_test.go | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 pkg/search/similarity_test.go diff --git a/pkg/search/similarity.go b/pkg/search/similarity.go index 5ce964db..a20848dc 100644 --- a/pkg/search/similarity.go +++ b/pkg/search/similarity.go @@ -44,14 +44,23 @@ func DebugSimilarity[Q any, I any](w io.Writer, query Entity[Q], index Entity[I] // Critical identifiers (highest weight) exactIdentifiers := compareExactIdentifiers(w, query, index, criticalIdWeight) if exactIdentifiers.matched && exactIdentifiers.fieldsCompared > 0 { + if math.IsNaN(exactIdentifiers.score) { + return 0.0 + } return exactIdentifiers.score } exactCryptoAddresses := compareExactCryptoAddresses(w, query, index, criticalIdWeight) if exactCryptoAddresses.matched && exactCryptoAddresses.fieldsCompared > 0 { + if math.IsNaN(exactCryptoAddresses.score) { + return 0.0 + } return exactCryptoAddresses.score } exactGovernmentIDs := compareExactGovernmentIDs(w, query, index, criticalIdWeight) if exactGovernmentIDs.matched && exactGovernmentIDs.fieldsCompared > 0 { + if math.IsNaN(exactGovernmentIDs.score) { + return 0.0 + } return exactGovernmentIDs.score } pieces = append(pieces, exactIdentifiers, exactCryptoAddresses, exactGovernmentIDs) @@ -82,6 +91,9 @@ func DebugSimilarity[Q any, I any](w io.Writer, query Entity[Q], index Entity[I] finalScore := calculateFinalScore(w, pieces, query, index) fmt.Printf("final score: %.2f\n", finalScore) + if math.IsNaN(finalScore) { + return 0.0 + } return finalScore } diff --git a/pkg/search/similarity_test.go b/pkg/search/similarity_test.go new file mode 100644 index 00000000..fd6d3b2d --- /dev/null +++ b/pkg/search/similarity_test.go @@ -0,0 +1,18 @@ +package search_test + +import ( + "testing" + + "github.com/moov-io/watchman/pkg/search" + + "github.com/stretchr/testify/require" +) + +func TestSimilarity_EdgeCases(t *testing.T) { + var query search.Entity[search.Value] + index := findOFACEntity(t, "47371") + + got := search.Similarity(query, index) + require.InDelta(t, 0.0, got, 0.001) + +}