Created
February 11, 2012 21:56
-
-
Save mugyu/1804489 to your computer and use it in GitHub Desktop.
N-gram
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
class String | |
def ngram(n) | |
characters = self.each_char.to_a | |
return [self] if characters.size <= n | |
return characters.each_cons(n).map(&:join) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment