Skip to content

Instantly share code, notes, and snippets.

@miwarin
Created April 30, 2014 11:14
Show Gist options
  • Save miwarin/c940a5bfbb2c83502b0f to your computer and use it in GitHub Desktop.
Save miwarin/c940a5bfbb2c83502b0f to your computer and use it in GitHub Desktop.
N-gram
#!/usr/bin/ruby -Ku
require 'pp'
def ngram(gram = 2, text)
len = text.length - 1
0.upto(len) {|i|
if i + gram > text.length
return
end
puts text[i, gram]
}
end
def main(argv)
gram = 2
text = "hoge"
ngram(gram, text)
gram = 3
text = "日本語です"
ngram(gram, text)
end
main(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment