From 1d133a2e372b207af6a988528a6a66612ed21398 Mon Sep 17 00:00:00 2001 From: Zack Date: Wed, 26 Apr 2023 07:49:45 +0100 Subject: [PATCH] Catch that the Q in the QGram has to be positive --- README.md | 6 +++++- qgram.go | 4 ++++ qgram_test.go | 10 ++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9f9f407..9bf3e1b 100644 --- a/README.md +++ b/README.md @@ -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") -``` \ No newline at end of file +``` + +### 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 \ No newline at end of file diff --git a/qgram.go b/qgram.go index 8535777..81e16b6 100644 --- a/qgram.go +++ b/qgram.go @@ -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) diff --git a/qgram_test.go b/qgram_test.go index f7488f1..e1d3222 100644 --- a/qgram_test.go +++ b/qgram_test.go @@ -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)