Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/encoding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -437,13 +437,15 @@ and shift operations.
# References

- [Torchhd documentation](https://torchhd.readthedocs.io/en/stable/generated/torchhd.ngrams.html)

"""
function ngrams(vs::AbstractVector{<:AbstractHV}, n::Int = 3)
l = length(vs)
p = l - n + 1
@assert 1 <= n <= length(vs) "`n` must be 1 ≤ n ≤ $l"
return bundle([bind([shift(vs[i + j], j) for j in 0:(n - 1)]) for i in 1:p])
return map(
s -> bindsequence(s),
(vs[f:(f + (n - 1))] for f in 1:p)
) |> multiset
end

"""
Expand Down
3 changes: 2 additions & 1 deletion test/encoding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
end

@testset "ngrams" begin
@test ngrams(hvs) == Bool.([0, 1, 0, 0, 1])
@test ngrams(hvs).v == Bool.([0, 1, 0, 0, 1])
@test ngrams(hvs) == bundle([hvs[1] * ρ(hvs[2]) * ρ(hvs[3], 2), hvs[2] * ρ(hvs[3]) * ρ(hvs[4], 2), hvs[3] * ρ(hvs[4]) * ρ(hvs[5], 2)])
@test_throws AssertionError ngrams(hvs, 0)
@test_throws AssertionError ngrams(hvs, length(hvs) + 1)
end
Expand Down