Skip to content

Commit

Permalink
search: prevent NaN from leaking out of DebugSimilarity
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jan 13, 2025
1 parent 14c3d73 commit 1876899
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/search/similarity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/search/similarity_test.go
Original file line number Diff line number Diff line change
@@ -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)

}

0 comments on commit 1876899

Please sign in to comment.