Skip to content

Commit

Permalink
Catch that the Q in the QGram has to be positive
Browse files Browse the repository at this point in the history
  • Loading branch information
RoadSigns committed Apr 26, 2023
1 parent 530ed7b commit 1d133a2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ Additional information for this algorithm can be found https://en.wikipedia.org/
import "github.com/roadsigns/fuzzy"

fuzzy.SorensenDice("elon musk", "colon musk")
```
```

### Q-Gram
The Q-Gram algorithm is a similarity algorithm that It does this by dividing each string into substrings of length Q and then comparing the substrings to determine how many of them are the same in both strings.
Additional information for this algorithm can be found https://en.wikipedia.org/wiki/N-gram
4 changes: 4 additions & 0 deletions qgram.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package fuzzy

func QGram(source string, target string, q int) float64 {
if q <= 0 {
return 0.0
}

qGrams1 := generateQGrams(source, q)
qGrams2 := generateQGrams(target, q)

Expand Down
10 changes: 10 additions & 0 deletions qgram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ func TestQGram(t *testing.T) {
assert.Equal(t, expected, got)
}

func TestQGramNegativeValue(t *testing.T) {
expected := 0.0
got := fuzzy.QGram("Fish", "Fish", 0)
assert.Equal(t, expected, got)

expected = 0.0
got = fuzzy.QGram("Fish", "Fish", -1)
assert.Equal(t, expected, got)
}

func BenchmarkQGram(b *testing.B) {
for i := 0; i < b.N; i++ {
fuzzy.QGram("John Jones", "Jon Jones", 3)
Expand Down

0 comments on commit 1d133a2

Please sign in to comment.