Skip to content

Commit

Permalink
Merge pull request #310 from facchinm/lexicographic_distance_as_last_…
Browse files Browse the repository at this point in the history
…chance

Use lexicographic distance as last chance to spot the right library
  • Loading branch information
facchinm authored Mar 7, 2019
2 parents 69b6665 + eff1eeb commit 1096e48
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
27 changes: 27 additions & 0 deletions resolve_library.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/arduino/arduino-builder/constants"
"github.com/arduino/arduino-builder/types"
"github.com/arduino/arduino-builder/utils"
"github.com/schollz/closestmatch"
)

func ResolveLibrary(ctx *types.Context, header string) *types.Library {
Expand Down Expand Up @@ -211,6 +212,10 @@ func findBestLibraryWithHeader(header string, libraries []*types.Library) *types
if library != nil {
return library
}
library = findLibWithNameBestDistance(headerName, libraries)
if library != nil {
return library
}
}

return nil
Expand Down Expand Up @@ -252,6 +257,28 @@ func findLibWithNameContaining(name string, libraries []*types.Library) *types.L
return nil
}

func findLibWithNameBestDistance(name string, libraries []*types.Library) *types.Library {
// create closestmatch DB
var wordsToTest []string

for _, library := range libraries {
wordsToTest = append(wordsToTest, simplifyName(library.Name))
}
// Choose a set of bag sizes, more is more accurate but slower
bagSizes := []int{2}
// Create a closestmatch object
cm := closestmatch.New(wordsToTest, bagSizes)
closest_name := cm.Closest(name)

for _, library := range libraries {
if (closest_name == simplifyName(library.Name)) {
return library
}
}

return nil
}

func simplifyName(name string) string {
return strings.ToLower(strings.Replace(name, "_", " ", -1))
}
25 changes: 17 additions & 8 deletions resolve_library_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,38 @@ func TestFindBestLibraryWithHeader(t *testing.T) {
l3 := &types.Library{Name: "Calculus Lib Improved"}
l4 := &types.Library{Name: "Another Calculus Lib"}
l5 := &types.Library{Name: "Yet Another Calculus Lib Improved"}
l6 := &types.Library{Name: "AnotherLib"}
l6 := &types.Library{Name: "Calculus Unified Lib"}
l7 := &types.Library{Name: "AnotherLib"}

// Test exact name matching
res := findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l6, l5, l4, l3, l2, l1})
res := findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l7, l6, l5, l4, l3, l2, l1})
require.Equal(t, l1.Name, res.Name)

// Test exact name with "-master" postfix matching
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l6, l5, l4, l3, l2})
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l7, l6, l5, l4, l3, l2})
require.Equal(t, l2.Name, res.Name)

// Test prefix matching
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l6, l5, l4, l3})
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l7, l6, l5, l4, l3})
require.Equal(t, l3.Name, res.Name)

// Test postfix matching
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l6, l5, l4})
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l7, l6, l5, l4})
require.Equal(t, l4.Name, res.Name)

// Test "contains"" matching
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l6, l5})
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l7, l6, l5})
require.Equal(t, l5.Name, res.Name)

// Test lexicographic similarity matching
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l7, l6})
require.Equal(t, l6.Name, res.Name)

// Test lexicographic similarity matching (2)
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l6, l7})
require.Equal(t, l6.Name, res.Name)

// Test none matching
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l6})
require.Nil(t, res)
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l7})
require.Equal(t, l7.Name, res.Name)
}

0 comments on commit 1096e48

Please sign in to comment.