Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: cmd/server: return the maximum between soundex and jaro-winkler in search #153

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
cmd/server: return the maximum between soundex and jaro-winkler in se…
…arch
  • Loading branch information
adamdecaf committed Oct 3, 2019
commit 7e618591caa63e69addfb639c645e74df6aa11a5
54 changes: 42 additions & 12 deletions cmd/server/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -69,9 +70,13 @@ var (
// topAddressesAddress is a compare method for TopAddressesFn to extract and rank .Address
topAddressesAddress = func(needleAddr string) func(*Address) *item {
return func(add *Address) *item {
needle := precompute(needleAddr)
return &item{
value: add,
weight: jaroWinkler(add.address, precompute(needleAddr)),
value: add,
weight: math.Max(
soundex(add.address, needle),
jaroWinkler(add.address, needle),
),
}
}
}
Expand All @@ -81,19 +86,27 @@ var (
// search criteria.
topAddressesCityState = func(needleCityState string) func(*Address) *item {
return func(add *Address) *item {
needle := precompute(needleCityState)
return &item{
value: add,
weight: jaroWinkler(add.citystate, precompute(needleCityState)),
value: add,
weight: math.Max(
soundex(add.citystate, needle),
jaroWinkler(add.citystate, needle),
),
}
}
}

// topAddressesCountry is a compare method for TopAddressesFn to extract and rank .Country
topAddressesCountry = func(needleCountry string) func(*Address) *item {
return func(add *Address) *item {
needle := precompute(needleCountry)
return &item{
value: add,
weight: jaroWinkler(add.country, precompute(needleCountry)),
value: add,
weight: math.Max(
soundex(add.country, needle),
jaroWinkler(add.country, needle),
),
}
}
}
Expand Down Expand Up @@ -180,8 +193,11 @@ func (s *searcher) TopAltNames(limit int, alt string) []Alt {

for i := range s.Alts {
xs.add(&item{
value: s.Alts[i],
weight: jaroWinkler(s.Alts[i].name, alt),
value: s.Alts[i],
weight: math.Max(
soundex(s.Alts[i].name, alt),
jaroWinkler(s.Alts[i].name, alt),
),
})
}

Expand Down Expand Up @@ -252,8 +268,11 @@ func (s *searcher) TopSDNs(limit int, name string) []SDN {

for i := range s.SDNs {
xs.add(&item{
value: s.SDNs[i],
weight: jaroWinkler(s.SDNs[i].name, name),
value: s.SDNs[i],
weight: math.Max(
soundex(s.SDNs[i].name, name),
jaroWinkler(s.SDNs[i].name, name),
),
})
}

Expand Down Expand Up @@ -285,8 +304,11 @@ func (s *searcher) TopDPs(limit int, name string) []DP {

for _, dp := range s.DPs {
xs.add(&item{
value: dp,
weight: jaroWinkler(dp.name, name),
value: dp,
weight: math.Max(
soundex(dp.name, name),
jaroWinkler(dp.name, name),
),
})
}

Expand Down Expand Up @@ -543,6 +565,14 @@ func jaroWinkler(s1, s2 string) float64 {
return max
}

// soundex will phonetically normalize two strings and then return their jaro-winkler distance
// as a percentage.
//
// For more details see https://en.wikipedia.org/wiki/Soundex
func soundex(s1, s2 string) float64 {
return jaroWinkler(smetrics.Soundex(s1), smetrics.Soundex(s2))
}

// extractIDFromRemark attempts to parse out a National ID or similar governmental ID value
// from an SDN's remarks property.
//
Expand Down