Skip to content

Commit

Permalink
renaming, its slowly taking shape
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynom committed Jul 11, 2018
1 parent f86a617 commit 38f36ba
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
32 changes: 23 additions & 9 deletions keyboard/distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ import (
"math"
)

type KeyboardLayout string

const (
Default KeyboardLayout = QwertyUS
QwertyUS KeyboardLayout = "qwerty-us"
)

type keyGrid map[string]coordinates

var (
keyGrid map[string]coordinates

// @todo this design currently ignores the possibility of pressing the shift key while typing
// we might want to allow printable symbols with the same coordinates as their un-shifted counterfeit
keyboardLayouts = map[string][]string{
"qwerty-us": {
keyboardLayouts = map[KeyboardLayout][]string{
QwertyUS: {
"`1234567890-=",
" qwertyuiop[]\\",
" asdfghjkl;'",
Expand All @@ -32,11 +40,17 @@ type coordinates struct {
Y float64
}

func init() {
keyGrid = generateKeyGrid(keyboardLayouts["qwerty-us"])
type KeyDist struct {
grid keyGrid
}

func New(layout KeyboardLayout) KeyDist {
return KeyDist{
grid: generateKeyGrid(keyboardLayouts[layout]),
}
}

func FindNearest(input string, list []string) (string, float64) {
func (kd KeyDist) FindNearest(input string, list []string) (string, float64) {
var bestScore = math.Inf(1)
var result string

Expand All @@ -54,7 +68,7 @@ func FindNearest(input string, list []string) (string, float64) {

if input[i] != ref[i] {
left, right := string(input[i]), string(ref[i])
score += getDistance(keyGrid[left], keyGrid[right])
score += getDistance(kd.grid[left], kd.grid[right])
}
}

Expand All @@ -73,8 +87,8 @@ func getDistance(a, b coordinates) float64 {
)
}

func generateKeyGrid(rows []string) map[string]coordinates {
var keyMap = make(map[string]coordinates, len(rows))
func generateKeyGrid(rows []string) keyGrid {
var keyMap = make(keyGrid, len(rows))

for rowIndex, row := range rows {
for column, char := range row {
Expand Down
9 changes: 6 additions & 3 deletions keyboard/distance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ func TestGetBestMatch(t *testing.T) {
{Input: "bee5", List: []string{"beef", "beer", "ben"}, Expect: "beer"},
}

kd := New(Default)
for _, td := range testData {
result, distance := FindNearest(td.Input, td.List)
result, distance := kd.FindNearest(td.Input, td.List)
if td.Expect != result {
t.Errorf("Expected '%s' to match '%s', instead I got '%s' with distance %f, %+v",
td.Input, td.Expect, result, distance, td)
Expand Down Expand Up @@ -81,14 +82,16 @@ func BenchmarkGetBestMatch(b *testing.B) {
smallList := generateList(10)
bigList := generateList(1000)
b.Run("small-list", func(b *testing.B) {
kd := New(Default)
for i := 0; i < b.N; i++ {
FindNearest("minkey", smallList)
kd.FindNearest("minkey", smallList)
}
})

b.Run("big-list", func(b *testing.B) {
kd := New(Default)
for i := 0; i < b.N; i++ {
FindNearest("minkey", bigList)
kd.FindNearest("minkey", bigList)
}
})
}
Expand Down

0 comments on commit 38f36ba

Please sign in to comment.