Posts

Showing posts with the label hamming-distance

Compare user response with string using javascript for quizz

Compare user response with string using javascript for quizz I have a input field for the user who's listening a music and try to find the correct artist or track name. <input type="search" id="search" class="form-control form-control-lg" name="search" autocomplete="off" autofocus> i would like to it compare with two javascript variables : The problem is to handle human typing errors. ex : "alicia key falin" should return true for the artist and the track name. I have already tried scripts about Hamming Distance and also the fusejs library but i'm not convainced by the result. Actual tests : function hammingDistance(str1, str2) { var dist = 0; str1 = str1.toLowerCase(); str2 = str2.toLowerCase(); for (var i = 0, j = Math.max(str1.length, str2.length); i < j; i++) { if (!str1[i] || !str2[i] || str1[i] !== str2[i]) { dist++; } } return dist; } The objective being to prevent capital, low...

How to generates a list which elements are at a fix distance from a desired list

How to generates a list which elements are at a fix distance from a desired list I have a list of possibilities and a desired input: possibles = [20, 30, 40, 50, 60, 70, 80, 100] desired = [20, 30, 40] I want to generate the close by lists. Example: # Distance of 1 (i.e. 1 element changes to a close-by) [30, 30, 40] [20, 40, 40] [20, 30, 30] [20, 30, 50] # Distance of 2: [40, 30, 40] [30, 30, 50] [30, 40, 40] ... My current version is only varying one element at a time, thus, as soon as the distance is above 1, I'm missing a lot of combination. def generate_close_by(possibles, desired): for k in range(1, 4): for i, elt in enumerate(desired): id = possibles.index(elt) new = desired[:] if id < len(possibles)-k-1: new[i] = possibles[id+k] yield (new) if id > k: new[i] = possibles[id-k] yield (new) # Output [30, 30, 40] [20, 40, 40] [20, 30, 50] [20, 30, 30]...