Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add k-gram shingle to Jaccard/Cosine sim (#11)
* feat: add Shingle function * test: update unit tests for Cosine/Jaccard with shingle Co-authored-by: Shriprajwal K <the_daemon_lord@Shriprajwals-MacBook-Air.local> Co-authored-by: hbollon <hugo.bollon@gmail.com>
- Loading branch information
1 parent
4f1acf0
commit 24d61a6
Showing
8 changed files
with
177 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package edlib | ||
|
||
// Shingle Find the k-gram of a string for a given k | ||
// Takes a string and an integer as parameters and return a map. | ||
// Returns an empty map if the string is empty or if k is 0 | ||
func Shingle(s string, k int) map[string]int { | ||
m := make(map[string]int) | ||
if s != "" && k != 0 { | ||
runeS := []rune(s) | ||
|
||
for i := 0; i < len(runeS)-k+1; i++ { | ||
m[string(runeS[i:i+k])]++ | ||
} | ||
} | ||
return m | ||
} | ||
|
||
// ShingleSlice Find the k-gram of a string for a given k | ||
// Takes a string and an integer as parameters and return a slice. | ||
// Returns an empty slice if the string is empty or if k is 0 | ||
func ShingleSlice(s string, k int) []string { | ||
var out []string | ||
m := make(map[string]int) | ||
if s != "" && k != 0 { | ||
runeS := []rune(s) | ||
for i := 0; i < len(runeS)-k+1; i++ { | ||
m[string(runeS[i:i+k])]++ | ||
} | ||
for k := range m { | ||
out = append(out, k) | ||
} | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package edlib | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestShingle(t *testing.T) { | ||
type args struct { | ||
str string | ||
k int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want map[string]int | ||
}{ | ||
{"shingle 1", args{"Radiohead", 2}, map[string]int{"Ra": 1, "ad": 2, "di": 1, "ea": 1, "he": 1, "io": 1, "oh": 1}}, | ||
{"shingle 1-1", args{"Radiohead", 3}, map[string]int{"Rad": 1, "adi": 1, "dio": 1, "ead": 1, "hea": 1, "ioh": 1, "ohe": 1}}, | ||
{"shingle 2", args{"I love horror movies", 2}, map[string]int{" h": 1, " l": 1, " m": 1, "I ": 1, "e ": 1, "es": 1, "ho": 1, "ie": 1, "lo": 1, "mo": 1, "or": 2, "ov": 2, "r ": 1, "ro": 1, "rr": 1, "ve": 1, "vi": 1}}, | ||
{"shingle 3", args{"私の名前はジョンです", 2}, map[string]int{"です": 1, "の名": 1, "はジ": 1, "ジョ": 1, "ョン": 1, "ンで": 1, "前は": 1, "名前": 1, "私の": 1}}, | ||
{"shingle 4", args{"🙂😄🙂😄 😄🙂😄", 2}, map[string]int{" 😄": 1, "😄 ": 1, "😄🙂": 2, "🙂😄": 3}}, | ||
{"shingle 5", args{"", 100}, make(map[string]int)}, | ||
{"shingle 6", args{"hello", 0}, make(map[string]int)}, | ||
{"shingle 7", args{"四畳半神話大系", 7}, map[string]int{"四畳半神話大系": 1}}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := Shingle(tt.args.str, tt.args.k) | ||
eq := reflect.DeepEqual(got, tt.want) | ||
if !eq { | ||
t.Errorf("Shingle() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters