-
Notifications
You must be signed in to change notification settings - Fork 0
/
approxNaive.go
57 lines (53 loc) · 1.25 KB
/
approxNaive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package approx
// This file contains functions meant for learning
// various string matching algorithms
// A naive find algorithm
func naiveFind(subsequence string, sequence string) []Match {
// Check for empty strings first
if subsequence == "" || sequence == "" {
return []Match{}
}
subseq := []rune(subsequence)
seq := []rune(sequence)
matches := []Match{}
for i := 0; i <= len(seq)-len(subseq); i++ {
match := true
for j := 0; j <= len(subseq)-1; j++ {
if seq[i+j] != subseq[j] {
match = false
break
}
}
if match {
matches = append(matches, Match{i, i + len(subseq), 0})
}
}
return matches
}
// Naive Fuzzy find allowing mismatches
func naiveFuzzyFind(subsequence string, sequence string, maxMM int) []Match {
// Check for empty strings first
if subsequence == "" || sequence == "" {
return []Match{}
}
subseq := []rune(subsequence)
seq := []rune(sequence)
matches := []Match{}
for i := 0; i <= len(seq)-len(subseq); i++ {
match := true
mm := 0 // Initialize the mismatch counter
for j := 0; j <= len(subseq)-1; j++ {
if seq[i+j] != subseq[j] {
mm++
if mm > maxMM {
match = false
break
}
}
}
if match {
matches = append(matches, Match{i, i + len(subseq), mm})
}
}
return matches
}