-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/server: filter results based on phonetic similarly
This greatly reduces the number of jaro-winkler comparisons performed. │ before.txt │ after2.txt │ │ sec/op │ sec/op vs base │ JaroWinkler/bestPairsJaroWinkler-16 4.535µ ± ∞ ¹ 3.161µ ± ∞ ¹ ~ (p=1.000 n=1) ² │ before.txt │ after2.txt │ │ B/op │ B/op vs base │ JaroWinkler/bestPairsJaroWinkler-16 862.0 ± ∞ ¹ 359.0 ± ∞ ¹ ~ (p=1.000 n=1) ² │ before.txt │ after2.txt │ │ allocs/op │ allocs/op vs base │ JaroWinkler/bestPairsJaroWinkler-16 32.00 ± ∞ ¹ 15.00 ± ∞ ¹ ~ (p=1.000 n=1) ²
- Loading branch information
Showing
11 changed files
with
147 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main | ||
|
||
import ( | ||
"unicode" | ||
) | ||
|
||
var soundexMap = map[rune]rune{ | ||
'A': 'A', 'E': 'A', 'I': 'A', 'O': 'A', 'U': 'A', 'Y': 'A', // vowels | ||
'B': 'B', 'F': 'B', 'P': 'B', 'V': 'B', // similar sounds | ||
'C': 'C', 'G': 'C', 'J': 'C', 'K': 'C', 'Q': 'C', 'S': 'C', 'X': 'C', 'Z': 'C', // sibilants | ||
'D': 'D', 'T': 'D', // dental sounds | ||
'L': 'L', // liquids | ||
'M': 'M', 'N': 'M', // nasal sounds | ||
'R': 'R', // trills | ||
'H': 'H', 'W': 'H', // breathy sounds | ||
} | ||
|
||
// getPhoneticClass returns the phonetic class of the first letter in a string | ||
func getPhoneticClass(s string) rune { | ||
if s == "" { | ||
return ' ' | ||
} | ||
// Return the first rune mapped with partial soundex | ||
for _, r := range s { | ||
firstLetter := unicode.ToUpper(r) | ||
if phonetic, ok := soundexMap[firstLetter]; ok { | ||
return phonetic | ||
} | ||
return firstLetter | ||
} | ||
return ' ' | ||
} | ||
|
||
func firstCharacterSoundexMatch(s1, s2 string) bool { | ||
if s1 == "" || s2 == "" { | ||
return false | ||
} | ||
return getPhoneticClass(s1) == getPhoneticClass(s2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFirstCharacterSoundexMatch(t *testing.T) { | ||
require.True(t, firstCharacterSoundexMatch("a", "A")) | ||
require.True(t, firstCharacterSoundexMatch("Catherine", "Katherine")) | ||
require.True(t, firstCharacterSoundexMatch("Fone", "Phone")) | ||
require.True(t, firstCharacterSoundexMatch("Vibe", "Bribe")) | ||
require.True(t, firstCharacterSoundexMatch("mine", "nine")) | ||
|
||
require.False(t, firstCharacterSoundexMatch("a", "")) | ||
require.False(t, firstCharacterSoundexMatch("", "A")) | ||
require.False(t, firstCharacterSoundexMatch("Dave", "Eve")) | ||
} | ||
|
||
func TestDisablePhoneticFiltering(t *testing.T) { | ||
search := strings.Fields("ian mckinley") | ||
indexed := "tian xiang 7" | ||
|
||
t.Setenv("DISABLE_PHONETIC_FILTERING", "no") | ||
score := bestPairsJaroWinkler(search, indexed) | ||
require.InDelta(t, 0.00, score, 0.01) | ||
|
||
// Disable filtering (force the compare) | ||
t.Setenv("DISABLE_PHONETIC_FILTERING", "yes") | ||
|
||
score = bestPairsJaroWinkler(search, indexed) | ||
require.InDelta(t, 0.544, score, 0.01) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters