Skip to content
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

Panic when calling AccuracyMutatingLetters() #9

Open
douglasm opened this issue Nov 22, 2018 · 0 comments
Open

Panic when calling AccuracyMutatingLetters() #9

douglasm opened this issue Nov 22, 2018 · 0 comments

Comments

@douglasm
Copy link

When testing some data and calling cm.AccuracyMutatingLetters() I am getting a panic of:
panic: invalid argument to Intn

math/rand.(*Rand).Intn(0xc000080180, 0x0, 0x2)
/usr/local/go/src/math/rand/rand.go:169 +0x9c
math/rand.Intn(0x0, 0x2)
/usr/local/go/src/math/rand/rand.go:329 +0x37
github.com/schollz/closestmatch.(*ClosestMatch).AccuracyMutatingLetters(0xc0005354d0, 0x10)
/Users/imac/go/src/github.com/schollz/closestmatch/closestmatch.go:309 +0x2b2

My call is:

	cm := closestmatch.New(possible, subsetSize)
	result := cm.Closest("Rose Villa")

where:

	subsetSize := []int{2, 3, 4}
	possible := []string{"Chris Horsburgh's B&B", "R & R Boutique and Breakfast", 
                          "Oaklands Guest House", "Guthrie Bed and Breakfast",
		          "Arndean Bed & Breakfast", "Duntrune House B & B",
                           "Cortachy House Bed & Breakfast", "Brucefield Boutique Bed and Breakfast",
		          "Rose Villa B&B", "Crepto Bed and Breakfast", "Newton Farm Holidays",
                           "West Adamston Farmhouse", "Liscara Bed & Breakfast",
		          "Chapel House B & B", "Kinnaber B & B", "Maulesbank Guest House",
                           "Fallady Bank Bed and Breakfast", "Church House Bed & Breakfast",
		          "Bridgelee Bed and Breakfast", "Croftsmuir B & B", "Denbrae Cottage B&B",
                           "Bubbly Oak (The) ", "Purgavie Farmhouse Bed and Breakfast ",
		          "Muirhouses Farm Bed and Breakfast", "Five Gables House B&B"}

On examining the code for AccuracyMutatingLetters(), at line 283 of closestmatch.go i is being incremented before being compared to the random integer returned from rand.IntN. When the returned random number is 0 the i != testStringNum will never pass and as such the testString is always blank. When you get to line 309 where a rand.IntN is returned using the length of the testString as the parameter you get the panic as rand.IntN does not accept 0 as a valid value.

I got the code to run by moving the i++ line to after the i != testStringNum compare, but I do not know if this fix will cause the function to return inaccurate results.

There is also a i++ before a comparison in the AccuracyMutatingWords function at line 208.

Hope this all helps, let me know if you need more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant