Created
November 7, 2015 15:30
-
-
Save matthieugomez/50d3c95e97f97701ded8 to your computer and use it in GitHub Desktop.
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
immutable QGramIterator{S, T <: Integer} | |
s::S # grapheorstring | |
l::Int # length of string | |
q::T # length of q-grams | |
end | |
function Base.start(qgram::QGramIterator) | |
(1, qgram.l < qgram.q ? endof(qgram.s) + 1 : chr2ind(qgram.s, qgram.q)) | |
end | |
function Base.next(qgram::QGramIterator, state) | |
istart, iend = state | |
element = SubString(qgram.s, istart, iend) | |
nextstate = nextind(qgram.s, istart), nextind(qgram.s, iend) | |
element, nextstate | |
end | |
function Base.done(qgram::QGramIterator, state) | |
istart, idend = state | |
done(qgram.s, idend) | |
end | |
Base.eltype{S <: AbstractString, T}(qgram::QGramIterator{S, T}) = SubString{S} | |
Base.eltype{S, T}(qgram::QGramIterator{GraphemeIterator{S}, T}) = SubString{S} | |
Base.length(qgram::QGramIterator) = max(qgram.l - qgram.q + 1, 0) | |
function Base.collect(qgram::QGramIterator) | |
x = Array(eltype(qgram), length(qgram)) | |
i = 0 | |
for q in qgram | |
i += 1 | |
@inbounds x[i] = q | |
end | |
x | |
end | |
Base.sort(qgram::QGramIterator) = sort!(collect(qgram)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment