Skip to content

Commit

Permalink
search: normalize in calculateTypeScore
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jan 15, 2025
1 parent f7a32b0 commit 8d2a894
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
13 changes: 5 additions & 8 deletions internal/prepare/prepare_gender.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ package prepare

import (
"strings"

"github.com/moov-io/watchman/pkg/search"
)

func NormalizeGender(input string) search.Gender {
func NormalizeGender(input string) string {
v := strings.ToLower(strings.TrimSpace(input))

// returned values need to match pkg/search.Gender values
switch v {
case "m", "male", "man", "guy":
return search.GenderMale

return "male"
case "f", "female", "woman", "gal", "girl":
return search.GenderFemale
return "female"
}

return search.GenderUnknown
return "unknown"
}
7 changes: 4 additions & 3 deletions internal/prepare/prepare_gender_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package prepare
package prepare_test

import (
"testing"

"github.com/moov-io/watchman/internal/prepare"
"github.com/moov-io/watchman/pkg/search"

"github.com/stretchr/testify/require"
Expand All @@ -22,8 +23,8 @@ func TestPrepare_NormalizeGender(t *testing.T) {
}
for _, tc := range cases {
t.Run(tc.input, func(t *testing.T) {
got := NormalizeGender(tc.input)
require.Equal(t, tc.expected, got)
got := prepare.NormalizeGender(tc.input)
require.Equal(t, string(tc.expected), got)
})
}
}
2 changes: 1 addition & 1 deletion internal/search/api_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func readSearchRequest(r *http.Request) (search.Entity[search.Value], error) {
req.Person = &search.Person{
Name: req.Name,
AltNames: q["altNames"],
Gender: prepare.NormalizeGender(q.Get("gender")),
Gender: search.Gender(prepare.NormalizeGender(q.Get("gender"))),
BirthDate: readDate(q.Get("birthDate")),
DeathDate: readDate(q.Get("deathDate")),
Titles: q["titles"],
Expand Down
7 changes: 4 additions & 3 deletions pkg/search/similarity_fuzzy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"unicode"

"github.com/moov-io/watchman/internal/prepare"
"github.com/moov-io/watchman/internal/stringscore"
)

Expand Down Expand Up @@ -587,8 +588,8 @@ func calculateNameScore(queryName, indexName string) float64 {

// calculateTypeScore determines how well affiliation types match
func calculateTypeScore(queryType, indexType string) float64 {
queryType = strings.ToLower(strings.TrimSpace(queryType))
indexType = strings.ToLower(strings.TrimSpace(indexType))
queryType = prepare.LowerAndRemovePunctuation(queryType)
indexType = prepare.LowerAndRemovePunctuation(indexType)

// Exact type match
if queryType == indexType {
Expand All @@ -610,7 +611,7 @@ func calculateTypeScore(queryType, indexType string) float64 {
func getTypeGroup(affType string) string {
for group, types := range affiliationTypeGroups {
for _, t := range types {
if strings.Contains(t, affType) {
if strings.EqualFold(affType, t) {
return group
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/search/similarity_fuzzy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func TestCompareAffiliationsFuzzy(t *testing.T) {
},
expectedScore: 1.0,
shouldMatch: true,
exact: true,
exact: false,
},
{
name: "no matching affiliations",
Expand Down Expand Up @@ -611,7 +611,7 @@ func TestCalculateTypeScore(t *testing.T) {
name: "with extra spaces",
queryType: " owned by ",
indexType: "owned by",
expectedScore: 0.8,
expectedScore: 1.0,
},
{
name: "unknown types",
Expand Down

0 comments on commit 8d2a894

Please sign in to comment.