Skip to content

Commit 62acbe9

Browse files
committed
Add lsh_family, which returns the LSHFunction subtype and/or constructor used when calling LSHFunction. This makes it a little easier to find the documentation for a given similarity function.
1 parent fc30604 commit 62acbe9

File tree

5 files changed

+97
-36
lines changed

5 files changed

+97
-36
lines changed

src/LSH.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export SimHash, L1Hash, L2Hash, MIPSHash, SignALSH, MinHash,
5252
LSHFunction, MonteCarloHash, ChebHash
5353

5454
# Helper / utility functions for LSHFunctions
55-
export index_hash, query_hash, n_hashes, hashtype, similarity,
55+
export index_hash, query_hash, n_hashes, hashtype, similarity, lsh_family,
5656
embedded_similarity
5757

5858
# Hash tables and related functions

src/LSHBase.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ LSHFunction API
3838
========================#
3939

4040
macro register_similarity! end
41+
function lsh_family end
4142

4243
#=
4344
The following functions must be defined for all LSHFunction subtypes

src/hashes/lshfunction.jl

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ true
3636
```
3737
"""
3838
macro register_similarity!(similarity, hashfn)
39-
fn = :(LSH.LSHFunction)
39+
lshfn = :(LSH.LSHFunction)
40+
lshfam = :(LSH.lsh_family)
4041

4142
quote
4243
local similarity = $(esc(similarity))
@@ -57,17 +58,66 @@ macro register_similarity!(similarity, hashfn)
5758
union!(available_similarities, Set([similarity]))
5859

5960
# Define LSHFunction(similarity, args...; kws...)
60-
$(esc(fn))(::typeof($(esc(similarity))), args...; kws...) =
61+
$(esc(lshfn))(::typeof($(esc(similarity))), args...; kws...) =
6162
$(esc(hashfn))(args...; kws...)
63+
64+
# Define lsh_family(similarity)
65+
$(esc(lshfam))(::typeof($(esc(similarity)))) = $(esc(hashfn))
6266
end
6367
end
6468

6569
#========================
6670
Associate similarity functions with LSHFunction subtypes
6771
========================#
6872

69-
@register_similarity!(cossim, SimHash)
70-
@register_similarity!(ℓ1, L1Hash)
71-
@register_similarity!(ℓ2, L2Hash)
72-
@register_similarity!(jaccard, MinHash)
73-
@register_similarity!(inner_prod, SignALSH)
73+
macro reset_similarities!()
74+
quote
75+
intersect!(available_similarities, Set())
76+
77+
methods(LSHFunction).ms .|> Base.delete_method
78+
methods(lsh_family).ms .|> Base.delete_method
79+
80+
@register_similarity!(cossim, SimHash)
81+
@register_similarity!(ℓ1, L1Hash)
82+
@register_similarity!(ℓ2, L2Hash)
83+
@register_similarity!(jaccard, MinHash)
84+
@register_similarity!(inner_prod, SignALSH)
85+
end
86+
end
87+
88+
@reset_similarities!()
89+
90+
#========================
91+
Documentation
92+
========================#
93+
94+
@doc """
95+
lsh_family(similarity)
96+
97+
Return the default constructor or `LSHFunction` subtype used to construct a hash function for the similarity function `similarity`.
98+
99+
The main use of `lsh_family` is to make it easier to find the documentation for the hash function that's constructed when you call `LSHFunction`. For instance, if you want to know more about the arguments and keyword parameters that can be given to `LSHFunction(inner_prod)`, you can run
100+
101+
```
102+
julia> lsh_family(inner_prod)
103+
SignALSH
104+
105+
help?> SignALSH
106+
```
107+
108+
# Examples
109+
110+
```jldoctest; setup = :(using LSH)
111+
julia> lsh_family(cossim)
112+
SimHash
113+
114+
julia> lsh_family(ℓ1)
115+
$(
116+
if L1Hash |> methods |> length == 1
117+
"L1Hash (generic function with 1 method)"
118+
else
119+
"L1Hash (generic function with $(L1Hash |> methods |> length) methods)"
120+
end
121+
)
122+
```
123+
""" lsh_family

test/hashes/test_lshfunction.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,44 @@ include(joinpath("..", "utils.jl"))
1212
Tests
1313
==================#
1414

15+
@testset "Test similarity function registration" begin
16+
Random.seed!(RANDOM_SEED)
17+
18+
@testset "Register a custom similarity" begin
19+
### Test 1: register a function with @register_similarity!
20+
mysim(x,y) = dot(x,y) / (norm(x) * norm(y))
21+
22+
@test_throws MethodError LSHFunction(mysim)
23+
@test_throws MethodError lsh_family(mysim)
24+
25+
LSH.@register_similarity!(mysim, SimHash)
26+
27+
hashfn = LSHFunction(mysim)
28+
@test isa(hashfn, SimHash)
29+
@test lsh_family(mysim) == SimHash
30+
31+
### Test 2: register a function-like object with @register_similarity!
32+
struct mytype end
33+
34+
@test_throws MethodError LSHFunction(mytype())
35+
@test_throws MethodError lsh_family(mytype())
36+
@test_throws ErrorException LSH.@register_similarity!(mytype(), SimHash)
37+
38+
(::mytype)(x,y) = cossim(x,y)
39+
40+
LSH.@register_similarity!(mytype(), SimHash)
41+
hashfn = LSH.LSHFunction(mytype())
42+
@test isa(hashfn, SimHash)
43+
@test lsh_family(mysim) == SimHash
44+
45+
### Test 3: reset the available similarity functions
46+
LSH.@reset_similarities!()
47+
48+
@test_throws MethodError LSHFunction(mysim)
49+
@test_throws MethodError LSHFunction(mytype())
50+
end
51+
end
52+
1553
@testset "Test LSHFunction()" begin
1654
Random.seed!(RANDOM_SEED)
1755

test/test_similarities.jl

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,6 @@ include("utils.jl")
77
Tests
88
==================#
99

10-
@testset "Test similarity function API" begin
11-
Random.seed!(RANDOM_SEED)
12-
13-
@testset "Register a custom similarity" begin
14-
# Test 1: register a function with @register_similarity!
15-
mysim(x,y) = dot(x,y) / (norm(x) * norm(y))
16-
17-
@test_throws(MethodError, LSH.LSHFunction(mysim))
18-
19-
LSH.@register_similarity!(mysim, SimHash)
20-
21-
hashfn = LSHFunction(mysim)
22-
@test isa(hashfn, SimHash)
23-
24-
# Test 2: register a function-like object with @register_similarity!
25-
struct mytype end
26-
27-
@test_throws(MethodError, LSH.LSHFunction(mytype()))
28-
@test_throws(ErrorException, LSH.@register_similarity!(mytype(), SimHash))
29-
30-
(::mytype)(x,y) = cossim(x,y)
31-
32-
LSH.@register_similarity!(mytype(), SimHash)
33-
hashfn = LSH.LSHFunction(mytype())
34-
@test isa(hashfn, SimHash)
35-
end
36-
end
37-
3810
@testset "ℓ^p distance and norm tests" begin
3911
Random.seed!(RANDOM_SEED)
4012

0 commit comments

Comments
 (0)