Skip to content

Commit 1096e48

Browse files
authored
Merge pull request #310 from facchinm/lexicographic_distance_as_last_chance
Use lexicographic distance as last chance to spot the right library
2 parents 69b6665 + eff1eeb commit 1096e48

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

Diff for: resolve_library.go

+27
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/arduino/arduino-builder/constants"
3737
"github.com/arduino/arduino-builder/types"
3838
"github.com/arduino/arduino-builder/utils"
39+
"github.com/schollz/closestmatch"
3940
)
4041

4142
func ResolveLibrary(ctx *types.Context, header string) *types.Library {
@@ -211,6 +212,10 @@ func findBestLibraryWithHeader(header string, libraries []*types.Library) *types
211212
if library != nil {
212213
return library
213214
}
215+
library = findLibWithNameBestDistance(headerName, libraries)
216+
if library != nil {
217+
return library
218+
}
214219
}
215220

216221
return nil
@@ -252,6 +257,28 @@ func findLibWithNameContaining(name string, libraries []*types.Library) *types.L
252257
return nil
253258
}
254259

260+
func findLibWithNameBestDistance(name string, libraries []*types.Library) *types.Library {
261+
// create closestmatch DB
262+
var wordsToTest []string
263+
264+
for _, library := range libraries {
265+
wordsToTest = append(wordsToTest, simplifyName(library.Name))
266+
}
267+
// Choose a set of bag sizes, more is more accurate but slower
268+
bagSizes := []int{2}
269+
// Create a closestmatch object
270+
cm := closestmatch.New(wordsToTest, bagSizes)
271+
closest_name := cm.Closest(name)
272+
273+
for _, library := range libraries {
274+
if (closest_name == simplifyName(library.Name)) {
275+
return library
276+
}
277+
}
278+
279+
return nil
280+
}
281+
255282
func simplifyName(name string) string {
256283
return strings.ToLower(strings.Replace(name, "_", " ", -1))
257284
}

Diff for: resolve_library_test.go

+17-8
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,38 @@ func TestFindBestLibraryWithHeader(t *testing.T) {
4343
l3 := &types.Library{Name: "Calculus Lib Improved"}
4444
l4 := &types.Library{Name: "Another Calculus Lib"}
4545
l5 := &types.Library{Name: "Yet Another Calculus Lib Improved"}
46-
l6 := &types.Library{Name: "AnotherLib"}
46+
l6 := &types.Library{Name: "Calculus Unified Lib"}
47+
l7 := &types.Library{Name: "AnotherLib"}
4748

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

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

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

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

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

69+
// Test lexicographic similarity matching
70+
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l7, l6})
71+
require.Equal(t, l6.Name, res.Name)
72+
73+
// Test lexicographic similarity matching (2)
74+
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l6, l7})
75+
require.Equal(t, l6.Name, res.Name)
76+
6877
// Test none matching
69-
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l6})
70-
require.Nil(t, res)
78+
res = findBestLibraryWithHeader("calculus_lib.h", []*types.Library{l7})
79+
require.Equal(t, l7.Name, res.Name)
7180
}

0 commit comments

Comments
 (0)