Skip to content

Use lexicographic distance as last chance to spot the right library #310

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
}